【发布时间】:2019-02-25 17:05:27
【问题描述】:
我已经创建了一个采购订单表格并将所有数据添加到表格中。现在我想通过向DataGridView 添加新项目来更新采购订单项目。
我已经用旧项目填充DataGridView,现在我想添加新项目但它不起作用
错误:当控件是数据绑定时,行无法以编程方式添加到 DataGridView 行集合。
这是我用来在 updateMode 中添加新行的代码。
try
{
if (updateMode)
{
//DataTable dt = dataGridPOItems.DataSource as DataTable;
//DataRow rw = dt.NewRow();
//DataTable dt = dataGridPOItems.DataSource as DataTable;
//DataRow rw = dt.NewRow();
//var dataSource = dataGridPOItems.DataSource as BindingSource;
//var dataTable = dataSource.DataSource as DataTable;
DataTable dt = dataGridPOItems.DataSource as DataTable;
dt.Rows.Add();
dataGridPOItems.DataSource = dt;
int rowIndex = this.dataGridPOItems.RowCount;
var row = this.dataGridPOItems.Rows[rowIndex];
row.Cells["Product_ID"].Value = txtPOProdID.Text;
row.Cells["HSN"].Value = txtPOProdHSN.Text;
row.Cells["EAN"].Value = txtPOProdBarcode.Text;
row.Cells["PName"].Value = txtPOProdName.Text;
row.Cells["OrderQty"].Value = txtPOProdQtyReq.Text;
row.Cells["PMargin"].Value = txtPOProdMargin.Text;
row.Cells["MRP"].Value = txtPOProdMRP.Text;
row.Cells["GSTRate"].Value = txtPOProdGST.Text;
row.Cells["LandingCost"].Value = Math.Round(Convert.ToDecimal(row.Cells["MRP"].Value) - ((Convert.ToDecimal(row.Cells["MRP"].Value) * Convert.ToDecimal(row.Cells["PMargin"].Value) / 100)), 2);
row.Cells["BCost"].Value = Math.Round(Convert.ToDecimal(row.Cells["LandingCost"].Value) / (Convert.ToDecimal(row.Cells["GSTRate"].Value) / 100 + 1), 2);
row.Cells["BCostAmt"].Value = Convert.ToDecimal(row.Cells["BCost"].Value) * Convert.ToDecimal(row.Cells["OrderQty"].Value);
row.Cells["GSTAmt"].Value = Math.Round((Convert.ToDecimal(row.Cells["BCostAmt"].Value) * (Convert.ToDecimal(row.Cells["GSTRate"].Value) / 100)), 2);
row.Cells["NetAmt"].Value = Convert.ToDecimal(row.Cells["BCostAmt"].Value) + Convert.ToDecimal(row.Cells["GSTAmt"].Value);
txtPOProdQtyReq.Clear();
txtPOTotalItems.Text = Convert.ToString(rowIndex + 1);
foreach (DataGridViewColumn column in dataGridPOItems.Columns)
{
txtPOTotalQty.Text = dataGridPOItems.Rows.OfType<DataGridViewRow>().Sum(r => Convert.ToDecimal(r.Cells[4].Value)).ToString();
txtPOTCost.Text = dataGridPOItems.Rows.OfType<DataGridViewRow>().Sum(r => Convert.ToDecimal(r.Cells[12].Value)).ToString();
txtPOTAX.Text = dataGridPOItems.Rows.OfType<DataGridViewRow>().Sum(r => Convert.ToDecimal(r.Cells[10].Value)).ToString();
}
ClassControlsHandler.ClearAll(this.groupProdInfo);
txtPOProdID.Focus();
}
else
{
int rowIndex = this.dataGridPOItems.Rows.Add();
var row = this.dataGridPOItems.Rows[rowIndex];
row.Cells["ProductID"].Value = txtPOProdID.Text;
row.Cells["HSN"].Value = txtPOProdHSN.Text;
row.Cells["EAN"].Value = txtPOProdBarcode.Text;
row.Cells["PName"].Value = txtPOProdName.Text;
row.Cells["OrderQty"].Value = txtPOProdQtyReq.Text;
row.Cells["PMargin"].Value = txtPOProdMargin.Text;
row.Cells["MRP"].Value = txtPOProdMRP.Text;
row.Cells["GSTRate"].Value = txtPOProdGST.Text;
row.Cells["LandingCost"].Value = Math.Round(Convert.ToDecimal(row.Cells["MRP"].Value) - ((Convert.ToDecimal(row.Cells["MRP"].Value) * Convert.ToDecimal(row.Cells["PMargin"].Value) / 100)), 2);
row.Cells["BCost"].Value = Math.Round(Convert.ToDecimal(row.Cells["LandingCost"].Value) / (Convert.ToDecimal(row.Cells["GSTRate"].Value) / 100 + 1), 2);
row.Cells["BCostAmt"].Value = Convert.ToDecimal(row.Cells["BCost"].Value) * Convert.ToDecimal(row.Cells["OrderQty"].Value);
row.Cells["GSTAmt"].Value = Math.Round((Convert.ToDecimal(row.Cells["BCostAmt"].Value) * (Convert.ToDecimal(row.Cells["GSTRate"].Value) / 100)), 2);
row.Cells["NetAmt"].Value = Convert.ToDecimal(row.Cells["BCostAmt"].Value) + Convert.ToDecimal(row.Cells["GSTAmt"].Value);
txtPOProdQtyReq.Clear();
txtPOTotalItems.Text = Convert.ToString(rowIndex + 1);
foreach (DataGridViewColumn column in dataGridPOItems.Columns)
{
txtPOTotalQty.Text = dataGridPOItems.Rows.OfType<DataGridViewRow>().Sum(r => Convert.ToDecimal(r.Cells[4].Value)).ToString();
txtPOTCost.Text = dataGridPOItems.Rows.OfType<DataGridViewRow>().Sum(r => Convert.ToDecimal(r.Cells[12].Value)).ToString();
txtPOTAX.Text = dataGridPOItems.Rows.OfType<DataGridViewRow>().Sum(r => Convert.ToDecimal(r.Cells[10].Value)).ToString();
}
ClassControlsHandler.ClearAll(this.groupProdInfo);
txtPOProdID.Focus();
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
预期结果:我只想在 updateMode 中添加新行。
谢谢。
【问题讨论】:
-
消息很清楚。相反,编辑并向 DataTable 添加行,网格将自动显示它们,因为它已连接到它。
-
或者,将 DataTable 添加到
BindingSource.DatatSource并使用 BindingSource 作为 DGV DataSource。它将允许直接从 DataGridView 向 DataTable 添加行(例如,将数据添加到 New Row(最后一行))。 -
@Jimi Sir,你能显示一些代码吗?
标签: c# winforms datagridview