双击datagridview任一单元格,弹出另外一个窗体,如何修改数据呢?
我双击datagridview 事件的代码是这样的:
private void BookList_DoubleClick(object sender, EventArgs e)
{
int ID;
ID = BookList.CurrentCell.RowIndex;
Form2 form=new Form2();
DataTable dt = new DataTable();
dt = (DataTable)BookList.DataSource;
form.txtBookId.Text= dt.Rows[ID]["图书编号"].ToString();
form.txtBookName.Text = dt.Rows[ID]["书名"].ToString();
form.txtPrice.Text = dt.Rows[ID]["价格"].ToString();
form.cbType2.DataSource = (new BookCategory()).GetBookType();
form.cbType2.DisplayMember = "BookType";
form.ShowDialog();
}
之后弹出修改数据的窗体,试问怎么写代码进行修改并更新呢?这个窗体的代码是这样的:
Form1 form = new Form1();
int index = form.ID;//把form窗体的索引ID赋给index,这代码这样写可以吗??我试了,好象窗体过来的ID是0,也就是说那个索引值没有传递过来。
DataTable dt = new DataTable();
dt = (DataTable)form.BookList.DataSource;
dt.Rows[index]["图书编号"] = txtBookId.Text;
dt.Rows[index]["书名"] = txtBookName.Text;
dt.Rows[index]["价格"] = txtPrice.Text;
dt.Rows[index]["图书类型"] = cbType2.Text;
(new Dac()).UpdateData();
提示说“未将对象引用设置到对象的实例”
我很困惑,我想怎么把主窗体里的那个单元格索引ID直接传递到修改数据的窗体呢?