我有一个ListView
,它绑定了数据库中的数据。这是从数据库绑定日期的代码。添加新项目后,列表视图未更新。但是在数据库中,该表被更新了。我用来绑定列表视图的代码:
public void BindIncomeExpense()
{
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-U1OP1S9SQLEXPRESS;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-U1OP1S9SQLEXPRESS;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();
}
我不明白为什么存储程序未将最后添加的数据返回到ListView。在执行SP时,在数据库中,它也返回最后一个数据。
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();
我得到了答案。我用
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-U1OP1S9SQLEXPRESS;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_getAllIncomeExpense
正确返回数据。