[英]How to pass values from datagridview1 to datagridview2 with existing values?
I am trying to pass values from one datagridview to another datagridview... 我正在尝试将值从一个datagridview传递到另一个datagridview ...
But the values should keep on adding... like a stack... 但是值应该继续增加...就像堆栈...
What am I doing wrong? 我究竟做错了什么?
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex > -1 && e.ColumnIndex > -1)
{
string ID, ItemCode, ProductName, Amount, qty = comboBox2.Text,Total;
ID = dataGridView1.Rows[e.RowIndex].Cells[0].Value + "";
ItemCode = dataGridView1.Rows[e.RowIndex].Cells[1].Value + "";
ProductName = dataGridView1.Rows[e.RowIndex].Cells[2].Value + "";
Amount = dataGridView1.Rows[e.RowIndex].Cells[3].Value + "";
Total = (Convert.ToInt32(Amount) * Convert.ToInt32(qty)).ToString();
DataTable table = new DataTable();
table.Columns.Add("ID", typeof(int));
table.Columns.Add("ItemCode", typeof(string));
table.Columns.Add("ProductName", typeof(string));
table.Columns.Add("Amount", typeof(string));
table.Rows.Add(ID, ItemCode, ProductName,Total);
if (dataGridView2.RowCount > 0)
{
string a, b, c, d;
DataTable table1 = new DataTable();
a = dataGridView2.Rows[e.RowIndex].Cells[0].Value + "";
b = dataGridView2.Rows[e.RowIndex].Cells[1].Value + "";
c = dataGridView2.Rows[e.RowIndex].Cells[2].Value + "";
d = dataGridView2.Rows[e.RowIndex].Cells[3].Value + "";
table1.Columns.Add("ID", typeof(int));
table1.Columns.Add("ItemCode", typeof(string));
table1.Columns.Add("ProductName", typeof(string));
table1.Columns.Add("Amount", typeof(string));
table1.Rows.Add(a, b, c, d);
dataGridView2.DataSource = table1;
dataGridView2.DataSource = table;
}
else
{
dataGridView2.DataSource = table;
}
}
}
Since you have 2 gridviews with same column names you could do something as like, 由于您有2个具有相同列名的gridview,因此您可以执行类似的操作,
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex > -1 && e.ColumnIndex > -1)
{
string ID, ItemCode, ProductName, Amount, qty = comboBox2.Text, Total;
ID = dataGridView1.Rows[e.RowIndex].Cells[0].Value + "";
ItemCode = dataGridView1.Rows[e.RowIndex].Cells[1].Value + "";
ProductName = dataGridView1.Rows[e.RowIndex].Cells[2].Value + "";
Amount = dataGridView1.Rows[e.RowIndex].Cells[3].Value + "";
Total = (Convert.ToInt32(Amount) * Convert.ToInt32(qty)).ToString();
DataTable table;
if (dataGridView2.RowCount > 0)
{
table = (DataTable)dataGridView2.DataSource;
}
else
{
table = new DataTable();
table.Columns.Add("ID", typeof(int));
table.Columns.Add("ItemCode", typeof(string));
table.Columns.Add("ProductName", typeof(string));
table.Columns.Add("Amount", typeof(string));
}
table.Rows.Add(ID, ItemCode, ProductName, Total);
dataGridView2.DataSource = table;
}
}
Hope this helps... 希望这可以帮助...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.