我正在以编程方式创建数据网格,我想从用户那里获取已更改的单元格的值,以便将其添加到另一个数据网格。
我的代码是:
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(“qty”)的值,将此值和其余行数据添加到新的数据列表中。
怎么做到这一点?这可能吗?
答案 0 :(得分:0)
CellValueChanged
事件应该适用于这种情况。
当您创建每个新DataGridView
实例并将其添加到选项卡时,只需添加一个事件处理程序:
dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
然后在单个位置使用方法dataGridView1_CellValueChanged
,所有事件处理程序都使用该方法。
CellValueChanged
事件处理程序的签名采用DataGridViewCellEventArgs
,其中包含已更改的列和行索引的属性。所以你有这样的代码:
void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 3)
{
// Now you can cast sender to a DataGridView and copy the row at e.RowIndex
DataGridView dgv = sender as DataGridView;
if (dgv == null)
return;
// Here you get the cell value
var cellValue = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
}
}
我还建议将dt3指定为数据源。如果你确实使用数据源而不是未绑定的网格,那么复制行将更加清晰。