无法以编程方式将行添加到DataGridView [英] Unable to add Rows to DataGridView programically

查看:80
本文介绍了无法以编程方式将行添加到DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个采购订单表格,并将所有数据添加到表格中。现在,我想通过向 DataGridView 添加新项目来更新采购订单项目。

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.

我已经用旧项目填充了 DataGridView ,现在我想添加新项目,但是它不起作用

I have filled DataGridView with old items and now I wanted to add new items but it is not working

错误:当控件绑定数据时,无法以编程方式将行添加到 DataGridView 行集合中。

Error: Rows cannot be programmatically added to the DataGridView rows collection when the control is data-bound.

这是我要添加的代码

    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中添加新行。

Expected Results: i just want to add new rows in updateMode.

谢谢。

推荐答案

这很简单:与其直接使用< a href = https://docs.microsoft.com/zh-cn/dotnet/api/system.data.datatable?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev15.query%3FappId% 3DDev15IDEF1%26l%3DEN-US%26k%3Dk(System.Data.DataTable); k(TargetFrameworkMoniker-.NETFramework%26f%3D255%26MSPPError%3D-2147217396& view = netframework-4.7.2 rel = nofollow noreferrer > DataTable 作为 DataGridView.DataSource ,您可以使用 BindingSource 并设置 DataTa ble 作为 BindingSource.DataSource

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.

BindingSource 随后将成为 DataGridView.DataSource

private BindingSource dgvBindingSource = null;

使用所需的<$创建 DataTable c $ c>列或使用 DataAdapter ,然后:

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();

当您需要添加新的 DataRow BindingSource ,您可以直接使用 BindingSource.DataSource -必须以正确的顺序提供字段:

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]
});

或添加新的 DataRow 并使用 Columns 名称分配字段值:

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);
}

这两种方法都会立即更新DataGridView。

Both methods will updated the DataGridView immediately.

这篇关于无法以编程方式将行添加到DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆