private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
DataGridViewRow row = (DataGridViewRow)dataGridView1.Rows[1].Clone();
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
row.Cells[i].Value = dataGridView1.Rows[1].Cells[i].Value;
}
dataGridView2.Rows.Add(row);
}两个gridview结构要一样,,未测试。。
{
DataGridViewRow row = (DataGridViewRow)dataGridView1.Rows[1].Clone();
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
row.Cells[i].Value = dataGridView1.Rows[1].Cells[i].Value;
}
dataGridView2.Rows.Add(row);
}两个gridview结构要一样,,未测试。。
然后当点击 第一个表中的一行数据时,双击就加到第二个表中,并且要不能重复,只是把它们的数量增加
代码以下:
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server=EAABCB9752A54EE;database=MySchool;integrated security=true;");
SqlDataAdapter da = new SqlDataAdapter("select * from Test ", con);
DataTable dt = new DataTable();
da.Fill(dt);
this.dataGridView1.DataSource = dt;
} private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{ }
DataTable dt = new DataTable();
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
DataTable dt1 = this.dataGridView2.DataSource as DataTable;
if (dt1 == null)
{ //dt1.Columns.Add("商品名称", typeof(string));
//dt1.Columns.Add("商品数量", typeof(int));
//dt1.Columns.Add("单价", typeof(int));
//dt1.Columns.Add("小计", typeof(int));
DataColumn dc = new DataColumn("商品名称", typeof(string));
dt.Columns.Add(dc);
DataColumn dc1 = new DataColumn("商品数量", typeof(int));
dt.Columns.Add(dc1);
DataColumn dc2 = new DataColumn("单价", typeof(int));
dt.Columns.Add(dc2);
DataColumn dc3 = new DataColumn("小计", typeof(int));
dt.Columns.Add(dc3);
DataRow dr = dt.NewRow();
dr["商品名称"] = this.dataGridView1[1, e.RowIndex].Value.ToString();
dr["商品数量"] = this.dataGridView1[2, e.RowIndex].Value.ToString();
dr["单价"] = this.dataGridView1[3, e.RowIndex].Value.ToString();
dr["小计"] = Convert.ToInt32(dr["商品数量"]) * Convert.ToInt32(dr["单价"]);
dt.Rows.Add(dr);
this.dataGridView2.DataSource = dt;
}
else
{
foreach (DataRow dr in dt1.Rows)
{
if (dr["商品名称"].ToString().Equals(this.dataGridView1[1, e.RowIndex].Value.ToString()))
{
dr["商品数量"] = Convert.ToInt32(dr["商品数量"].ToString()) + 1;
dr["小计"] = Convert.ToInt32(dr["单价"].ToString()) * Convert.ToInt32(dr["商品数量"].ToString());
return;
}
}
DataRow dr1 = dt.NewRow();
dr1["商品名称"] = this.dataGridView1[1, e.RowIndex].Value.ToString();
dr1["商品数量"] = this.dataGridView1[2, e.RowIndex].Value.ToString();
dr1["单价"] = this.dataGridView1[3, e.RowIndex].Value.ToString();
dr1["小计"] = Convert.ToInt32(dr1["商品数量"]) * Convert.ToInt32(dr1["单价"]);
dt.Rows.Add(dr1);
}
}
不知道对不对
2. 将数据用程序添加到第二个控件上,
3 结构要相同才行。www.cndom.net