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

I am trying to add a rows to a RadGridView when a use click a button on the form (ie. "Add".)

When the form load I add columns to the RadGridView and make all of them all ReadOnly except one .

When a user click the "Add" button, I want to add a row to this RadGridView. Basically, a user types a UPC code, then I read data from the database and add new row in a grid view with the item name, item price.

Here is what I did to create the columns

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;


    }

To add the row Here is what I am doing "when a user click the 'Add' button)

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

But the add row is giving me an error

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

How can I correctly add rows to the RadGridView when the "Add" button is clicked.

Answers


ButtonAdd_Click is wrong because the foreach is always trying to set row 0, which doesn't exist.

You don't specify what GridView you are using, so I can't give specifics, but typically you should be able to call Rows.Add(...).


Need Your Help

Do DirectX Compute Shaders support 2D arrays in shared memory?

directx compute-shader directcompute

I want to use groupshared memory in a DirectX Compute Shader to reduce global memory bandwidth and hopefully improve performance. My input data is a Texture2D and I can access it using 2D indexing ...

Resize NSArray programmatically

objective-c resize nsarray category

I have been trying to figure out if it is possible to programmatically resize an NSArray, using code similar to this:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.