在C#WinForms中添加新项后更新列表视图

时间:2017-05-30 11:27:48

标签: c# winforms

我有一个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();
}

并添加新项目

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();
    }

    Main frm = new Main();
    frm.BindIncomeExpense();
}

我无法理解为什么storedprocedure没有将最后添加的数据返回到listview。在执行sp的数据库中,它也返回最后一个数据。

4 个答案:

答案 0 :(得分:2)

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

例如,在代码中的某个位置,您已使用

加载了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表单 以下代码将位于Main表单上以创建IncomeExpense表单:

IncomeExpense incomeExpense = new IncomeExpense();
incomeExpense.ShowDialog(this);

IncomeExpense表格上:

//Main frm = new Main();//Do not use this
this.Owner.BindIncomeExpense();

答案 1 :(得分:1)

我得到了答案。 我用了

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

调用Main表单的当前实例。现在它正在按照我的要求工作。在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();
    }

谢谢大家的帮助。 问候。

答案 2 :(得分:0)

请再次绑定数据库中的数据以获取新数据,即在将数据保存到数据库后简单地称为绑定数据的函数

答案 3 :(得分:0)

请确保sp_getAllIncomeExpense正确返回数据。