Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WinC窗体中添加新项后更新列表视图_C#_Winforms - Fatal编程技术网

C# 在WinC窗体中添加新项后更新列表视图

C# 在WinC窗体中添加新项后更新列表视图,c#,winforms,C#,Winforms,我有一个ListView,它绑定数据库中的数据。这是从数据库绑定日期的代码。添加新项目后,列表视图不会更新。但在数据库中,表会得到更新。我用于绑定列表视图的代码: public void BindIncomeExpense() { SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-U1OP1S9\SQLEXPRESS;Initial Catalog=PaintStores;Integrated Security=Tru

我有一个ListView,它绑定数据库中的数据。这是从数据库绑定日期的代码。添加新项目后,列表视图不会更新。但在数据库中,表会得到更新。我用于绑定列表视图的代码:

public void BindIncomeExpense()
{
    SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-U1OP1S9\SQLEXPRESS;Initial Catalog=PaintStores;Integrated Security=True");
    SqlCommand command = con.CreateCommand();
    command.CommandText = "sp_getAllIncomeExpense";

    SqlDataAdapter da = new SqlDataAdapter(command);
    DataTable dataTable = new DataTable();
    da.Fill(dataTable);

    for(int i = 0; i < dataTable.Rows.Count; i++)
    {
        DataRow drow = dataTable.Rows[i];
        // Only row that have not been deleted
        if(drow.RowState != DataRowState.Deleted)
        {
            // Define the list items
            ListViewItem lvi = new ListViewItem(drow["Description"].ToString());
            lvi.SubItems.Add(drow["Category"].ToString());
            lvi.SubItems.Add(drow["Amount"].ToString());
            lvi.SubItems.Add(drow["Date"].ToString());

            listView9.Items.Add(lvi);
        }
    }
    con.Close();
}

我不明白为什么StoredProcess没有将最后添加的数据返回到listview。在数据库中,当我执行sp时,它也会返回最后一个数据。

请再次绑定数据库中的数据以添加新数据,即在将数据保存到数据库中后,只需调用绑定数据的函数。

请确保sp_getAllIncomeExpense正确返回数据。

Main frm=new Main; 这可能就是问题所在。您正在创建表单的新实例。这可能与屏幕上显示的不同。使用与在屏幕上显示表单相同的实例

例如,在代码中的某个地方,您已经使用

Main frmOriginal = new Main();
frmOriginal.Show();// or ShowDialog or Application.Run
调用绑定方法时,应该可以访问frmOriginal实例。 您的新代码应该类似于:

//Main frm = new Main();//Do not use this
frmOriginal.BindIncomeExpense();//Use the instance of form that is already being displayed.
编辑:

根据您的评论,您需要将Main表单的实例传递给IncomeExpense表单。 以下代码将出现在主窗体上,用于创建IncomeExpense窗体:

在IncomeExpense表格上:

我得到了答案。 我曾经

调用主窗体的当前实例。现在它正按照我的要求工作。在IncomeExpense表单中保存数据的完整代码为

private void button1_Click(object sender, EventArgs e)
    {

        SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-U1OP1S9\SQLEXPRESS;Initial Catalog=PaintStores;Integrated Security=True");
        SqlCommand cmd = new SqlCommand("sp_saveIncomeExpense", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@id", comboBox12.SelectedValue);
        cmd.Parameters.AddWithValue("@description", textBox1.Text);
        cmd.Parameters.AddWithValue("@amount", textBox2.Text);
        cmd.Parameters.AddWithValue("@date", dateTimePicker12.Value);
        con.Open();
        int i = cmd.ExecuteNonQuery();

        con.Close();

        if (i != 0)
        {
            MessageBox.Show("Data Saved Successfully");
            this.Close();
        }

        if(System.Windows.Forms.Application.OpenForms["Main"] != null)
        {
            (System.Windows.Forms.Application.OpenForms["Main"] as Main).BindIncomeExpense();
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        this.Close();
    }
谢谢大家的帮助。
问候。

是的。在执行的同时,我执行sp。它返回数据库中的所有数据。但是在代码中它没有返回最后添加的数据;frm.bindinomeexpense;确保您从数据库中选择了数据,并且当您从数据库中获取数据时,我看不到连接字符串,即代码中的command.connection部分,但是,不仅在列表视图中,而且在下拉列表中,如果我添加了一个新项,它也没有加载。在我停止程序并重新启动后,只会显示所有内容。“button1_Click”以另一种形式写入,“BindingMeExpense”以主形式写入。但我的AddNew表单以“public partial class IncomeExpense:form”开头。这意味着AddNew表单不是从主表单创建的。那我怎么用呢?你发布的代码,是主表单的一部分,还是另一个表单?另外,如果它是另一个窗体的一部分,该窗体是否已经包含对现有主窗口的引用?我很确定我看到了这个问题,但我需要额外的信息来为您提供答案。@AmitJoshi:您的答案假设了对主窗体的引用是如何跟踪的。我要求OP提供具体的反馈,这样我就可以根据他的需要定制我的答案。您的回答排除了示例代码已经是主窗体本身的一部分的可能性。@更平坦的是,BindingMeExpense方法是主窗体。另一种代码是另一种形式。这是一种新的形式。我想在使用AddNew添加一个项目后更新主窗体中的listviewform@Coderstech:感谢您的回复,将创建一个合适的答案。AmitJoshi更新他的答案的速度比我写我的快。虽然我会以稍微不同的方式处理它;他的例子在功能上是相同的,可以解决我和阿米特都认为的问题。
IncomeExpense incomeExpense = new IncomeExpense();
incomeExpense.ShowDialog(this);
//Main frm = new Main();//Do not use this
this.Owner.BindIncomeExpense();
if(System.Windows.Forms.Application.OpenForms["Main"] != null)
        {
            (System.Windows.Forms.Application.OpenForms["Main"] as Main).BindIncomeExpense();
        }
private void button1_Click(object sender, EventArgs e)
    {

        SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-U1OP1S9\SQLEXPRESS;Initial Catalog=PaintStores;Integrated Security=True");
        SqlCommand cmd = new SqlCommand("sp_saveIncomeExpense", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@id", comboBox12.SelectedValue);
        cmd.Parameters.AddWithValue("@description", textBox1.Text);
        cmd.Parameters.AddWithValue("@amount", textBox2.Text);
        cmd.Parameters.AddWithValue("@date", dateTimePicker12.Value);
        con.Open();
        int i = cmd.ExecuteNonQuery();

        con.Close();

        if (i != 0)
        {
            MessageBox.Show("Data Saved Successfully");
            this.Close();
        }

        if(System.Windows.Forms.Application.OpenForms["Main"] != null)
        {
            (System.Windows.Forms.Application.OpenForms["Main"] as Main).BindIncomeExpense();
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        this.Close();
    }