问题
I'm have created a purchase order form and added all data to tables. now I wanted to update purchase order items by adding new items to DataGridView
.
I have filled DataGridView
with old items and now I wanted to add new items but it is not working
Error: Rows cannot be programmatically added to the DataGridView
rows collection when the control is data-bound.
Here is my code which I'm using to add a new row in 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);
}
Expected Results: i just want to add new rows in updateMode.
Thank You.
回答1:
Can you try it like this?
using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.ColumnCount = 3;
dataGridView1.Columns[0].Name = "Product ID";
dataGridView1.Columns[1].Name = "Product Name";
dataGridView1.Columns[2].Name = "Product Price";
string[] row = new string[] { "1", "Product 1", "1000" };
dataGridView1.Rows.Add(row);
row = new string[] { "2", "Product 2", "2000" };
dataGridView1.Rows.Add(row);
row = new string[] { "3", "Product 3", "3000" };
dataGridView1.Rows.Add(row);
row = new string[] { "4", "Product 4", "4000" };
dataGridView1.Rows.Add(row);
}
}
}
http://csharp.net-informations.com/datagridview/csharp-datagridview-add-column.htm
Or, simply.
private void InsertNewRowToDGV()
{
//we have to get the index of a last row id:
int index = this.dataGridView1.Rows.Count;
//and count +1 to get a new row id:
index++;
this.dataGridView1.Rows.Add();
}
回答2:
It's pretty simple: instead of directly using a DataTable as the DataGridView.DataSource
, you can use a BindingSource and set the DataTable
as the BindingSource.DataSource
.
The BindingSource
will then become the DataGridView.DataSource
.
private BindingSource dgvBindingSource = null;
Create a DataTable
with the required Columns
or fill it using a DataAdapter, then:
DataTable dt = new DataTable("TableName");
//Set the Columns or fill it with a DataAdapter
dgvBindingSource = new BindingSource();
dgvBindingSource.DataSource = dt;
dataGridView1.DataSource = dgvBindingSource;
dt.Dispose();
When you need to add a new DataRow to the BindingSource
, you can use the BindingSource.DataSource
directly - the Fields must be provided in the correct order:
(dgvBindingSource.DataSource as DataTable).Rows.Add(new object[] {
[Field1 Value], [Field2 Value], [Field3 Value], [Field N Value]
});
or add a new DataRow
and assign the Fields values using the Columns
names:
using (DataTable dt = (dgvBindingSource.DataSource as DataTable))
{
DataRow row = dt.NewRow();
row["Column0"] = SomeValue1;
row["Column1"] = SomeValue2;
row["Column2"] = SomeValue3;
dt.Rows.Add(row);
}
Both methods will updated the DataGridView immediately.
来源:https://stackoverflow.com/questions/54871248/unable-to-add-rows-to-datagridview-programically