如何使用 C# 将行添加到 WinForms 中的 RadGridView

How to add rows to a RadGridView in WinForms using c#

我正在尝试在用户单击表单上的按钮时向 RadGridView 添加行(即 "Add"。)

加载表单时,我将列添加到 RadGridView 并将所有列设为只读,只有一个除外。

当用户单击 "Add" 按钮时,我想向此 RadGridView 添加一行。基本上,用户键入一个 UPC 代码,然后我从数据库中读取数据并在网格视图中添加包含项目名称、项目价格的新行。

这是我创建列的方法

private void Register_Load(object sender, EventArgs e) {

        GridViewTextBoxColumn UPC = new GridViewTextBoxColumn();
        UPC.Name = "UPC";
        UPC.HeaderText = "UPC";
        UPC.FieldName = "UPC";
        UPC.MaxLength = 50;
        UPC.TextAlignment = ContentAlignment.BottomRight;
        radGridView1.MasterTemplate.Columns.Add(UPC);
        radGridView1.Columns["UPC"].Width = 120;
        radGridView1.Columns["UPC"].ReadOnly = true;

        GridViewTextBoxColumn ItemName = new GridViewTextBoxColumn();
        ItemName.Name = "Item Name";
        ItemName.HeaderText = "Item Name";
        ItemName.FieldName = "ItemName";
        ItemName.MaxLength = 100;
        ItemName.TextAlignment = ContentAlignment.BottomRight;
        radGridView1.MasterTemplate.Columns.Add(ItemName);
        radGridView1.Columns["Item Name"].Width = 210;
        radGridView1.Columns["Item Name"].ReadOnly = true;

        GridViewDecimalColumn QtyColumn = new GridViewDecimalColumn();
        QtyColumn.Name = "Qty";
        QtyColumn.HeaderText = "Quantity";
        QtyColumn.FieldName = "Qty";
        QtyColumn.DecimalPlaces = 1;
        radGridView1.MasterTemplate.Columns.Add(QtyColumn);
        radGridView1.Columns["Qty"].Width = 75;

        GridViewMaskBoxColumn PriceColumn = new GridViewMaskBoxColumn();
        PriceColumn.Name = "Unit Price";
        PriceColumn.FieldName = "UnitPrice";
        PriceColumn.HeaderText = "Unit Price";
        PriceColumn.MaskType = MaskType.Numeric;
        PriceColumn.Mask = "C";
        PriceColumn.TextAlignment = ContentAlignment.BottomRight;
        PriceColumn.FormatString = "{0:C}";
        PriceColumn.DataType = typeof(decimal);
        radGridView1.MasterTemplate.Columns.Add(PriceColumn);
        radGridView1.Columns["Unit Price"].Width = 75;
        radGridView1.Columns["Unit Price"].ReadOnly = true;

        GridViewMaskBoxColumn TotalColumn = new GridViewMaskBoxColumn();
        TotalColumn.Name = "Total Price";
        TotalColumn.FieldName = "TotalPrice";
        TotalColumn.HeaderText = "Total Price";
        TotalColumn.MaskType = MaskType.Numeric;
        TotalColumn.Mask = "C";
        TotalColumn.TextAlignment = ContentAlignment.BottomRight;
        TotalColumn.FormatString = "{0:C}";
        TotalColumn.DataType = typeof(decimal);
        radGridView1.MasterTemplate.Columns.Add(TotalColumn);
        radGridView1.Columns["Total Price"].Width = 75;
        radGridView1.Columns["Total Price"].ReadOnly = true;


    }

添加行这是我正在做的“当用户单击 'Add' 按钮时)

private void ButtonAdd_Click(object sender, EventArgs e) {


            string UPC = InputUPC.Text.Trim();

            if (UPC.Length < 3) {
                return;
            }

            string sql = " SELECT p.productName, p.price "
                       + " FROM products AS p "
                       + " WHERE p.productUPC = @upc ";

            var parms = new List<MySqlParameter>();
            parms.Add(new MySqlParameter("@upc", UPC));

            var db = new dbConnetion();

            foreach (var i in db.getData(sql, parms, r =>
                                                new ProductsTable() {
                                                    _productName = r["productName"].ToString(),
                                                    _price = Convert.ToDouble(r["price"])
                                                }
                                         )
            ) {

                //radGridView1.Rows[0].Cells[0].Value = 4.3;
                radGridView1.Rows[0].Cells["UPC"].Value = UPC;
                radGridView1.Rows[0].Cells["Item Name"].Value = i._productName;
                radGridView1.Rows[0].Cells["Qty"].Value = "1";
                radGridView1.Rows[0].Cells["Unit Price"].Value = i._price;
                radGridView1.Rows[0].Cells["Total Price"].Value = (Convert.ToDouble(i._price) * Convert.ToInt32(radGridView1.Rows[0].Cells["Qty"].Value)).ToString();
            }
        }

但是添加行给我一个错误

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

如何在单击 "Add" 按钮时正确地将行添加到 RadGridView。

ButtonAdd_Click 是错误的,因为 foreach 总是试图设置不存在的第 0 行。

你没有指定你使用的是什么 GridView,所以我不能给出具体细节,但通常你应该可以调用 Rows.Add(...).