我正在尝试将值从一个datagridview传递到另一个datagridview ...
但是值应该继续添加...就像堆栈......
我做错了什么?
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;
}
}
}
答案 0 :(得分:1)
由于你有2个具有相同列名的网格视图,你可以做类似的事情,
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;
}
}
希望这会有所帮助......