我正在以編程方式創建一個數據網格,並且我想將已從用戶更改過的單元格的值添加到另一個數據網格中。datagrid cellvaluechanged動態對象的事件處理程序
我的代碼是這樣的:
connTo.Open();
SqlDataReader sqlFromReader1 = null;
SqlDataReader sqlFromReader2 = null;
SqlDataReader sqlFromReader3 = null;
SqlCommand myFromCommand1 = new SqlCommand("select distinct m.catid1, c.descr "+
"from material m "+
"inner join cat1 c on c.id = m.catid1", connTo);
sqlFromReader1 = myFromCommand1.ExecuteReader();
DataTable dt1 = new DataTable();
dt1.Load(sqlFromReader1);
for (i = 0; i < dt1.Rows.Count; i++)
{
//int c1 = (int)(dt1.Rows[i]["catid1"]);
TabPage tbg = new TabPage(dt1.Rows[i]["descr"].ToString());
tabControl1.Controls.Add(tbg);
TabControl tbi = new TabControl();
tbi.Name = i.ToString() + i.ToString();
SqlCommand myFromCommand2 = new SqlCommand(
"select distinct m.catid2, c.descr " +
"from material m " +
"inner join cat2 c on c.id = m.catid2 "+
"and m.catid1 = " + dt1.Rows[i]["catid1"].ToString(), connTo);
sqlFromReader2 = myFromCommand2.ExecuteReader();
DataTable dt2 = new DataTable();
dt2.Load(sqlFromReader2);
for (int k = 0; k < dt2.Rows.Count; k++)
{
//int c2 = (int)(dt2.Rows[k]["catid2"]);
TabPage tbgi = new TabPage(dt2.Rows[k]["descr"].ToString());
tbi.Controls.Add(tbgi);
tbi.Left = 1;
tbi.Top = 1;
tbi.Width = 880;
tbi.Height = 400;
DataGridView gv = new DataGridView();
gv.Width = 850;
gv.Height = 340;
gv.Left = 10;
gv.Top = 10;
gv.ColumnCount = 4;
gv.Columns[0].Name = "code";
gv.Columns[0].Width = 120;
gv.Columns[0].ReadOnly = true;
gv.Columns[1].Name = "name";
gv.Columns[1].Width = 450;
gv.Columns[1].ReadOnly = true;
gv.Columns[2].Name = "price";
gv.Columns[2].Width = 80;
gv.Columns[2].ReadOnly = true;
gv.Columns[3].Name = "qty";
gv.Columns[3].Width = 120;
SqlCommand myFromCommand3 = new SqlCommand(
"select m.code, m.descr, m.cost1 price " +
"from material m " +
"where m.catid1 = " + dt1.Rows[i]["catid1"].ToString() + " " +
"and m.catid2 = " + dt2.Rows[k]["catid2"].ToString(), connTo);
sqlFromReader3 = myFromCommand3.ExecuteReader();
DataTable dt3 = new DataTable();
dt3.Load(sqlFromReader3);
for (int l = 0; l < dt3.Rows.Count; l++)
{
gv.Rows.Add(dt3.Rows[l]["code"].ToString(), dt3.Rows[l]["descr"].ToString(), dt3.Rows[l]["price"].ToString(), "");
}
tbgi.Controls.Add(gv);
}
tbg.Controls.Add(tbi);
現在我想,當用戶改變gv.Columns的價值趕上[3],請將.Name(「數量」)來添加這個值和的休息將行數據轉換爲新的數據列表。
這怎麼辦?這可能嗎?
我不確定我是否理解你的問題,因爲你在問題標題中提到的事件正是我相信會幫助你的事情。我會回答,但如果這沒有幫助,請嘗試進一步解釋問題。另外,您可以使用dt3作爲datagridview的數據源,而不是像現在這樣創建行。 – 2012-04-25 22:21:40