Programmatically add new rows to an unbound DataGridView control in C#

Before writing code, you need to prepare the DataGridView control. Add the control to the form and select it. Click its Smart Tag and then click the Edit Columns link.

Add the columns that you need and set their HeaderText values.

To format the values in a column, click the column’s DefaultCellStyle property and then click the ellipsis to the right. Use the CellStyle Builder to set such properties as the column’s colors, format, alignment, and wrapping.


Now when you click the program’s Add button, the following code adds the new row to the DataGridView.

private void btnAdd_Click(object sender, EventArgs e)
{
    // Create the new row.
    decimal price_each = decimal.Parse(txtPriceEach.Text);
    decimal quantity = decimal.Parse(txtQuantity.Text);
    decimal total = price_each * quantity;
    dgvValues.Rows.Add(txtItem.Text, price_each, quantity, total);

    // Get ready for the next entry.
    txtItem.Clear();
    txtPriceEach.Clear();
    txtQuantity.Clear();
    txtItem.Focus();
}

The code first gets the Price Each and Quantity values you entered and uses them to calculate total cost. It then uses the DataGridView‘s Rows.Add method to create the new row, passing in the row’s values.

It is important that this call passes decimal values for Price Each rather than the string value stored in the TextBox. The Price Each column’s cell style displays the value formatted as currency, but that only works if the value is numeric, not a string.

The code finishes by clearing the TextBoxes and setting focus to the Item TextBox so the user can easily enter the next row’s values.






This entry was posted in controls and tagged , , , , , , , , , . Bookmark the permalink.

11 Responses to Programmatically add new rows to an unbound DataGridView control in C#

  1. Chaidy says:

    Thank a lots

  2. sohaib says:

    Rows cannot be programmatically added to the DataGridView’s rows collection when the control is data-bound

  3. dobisha says:

    This method requires additional rows

  4. Deek Ward says:

    What the hell is dgvValue.rows.add () ? where did it come from and how you add a row … aaargh ..nice tutorial but you must keep beginners in mind Please

    • RodStephens says:

      Sorry if it isn’t obvious, but you need to download the example to see all of the details.

      Typically a simple program might contain dozens or hundreds of lines of code even though only a few are interesting, so the post only describes the interesting ones. Similarly the controls created at design time aren’t shown because the code to create them is really long, complex, and not very interesting.

      If you download the example, you can see additional details. In this example, dgvValue is the DataGrid control created at design time. Rows is its collection or rows and Add is a method the collection provides to add a new row to the collection.

  5. Deek Ward says:

    Actually this code does NOT work to add values !

  6. Kashan says:

    I just Wanted to Add Row in Specific Column to Get Grand Total
    Just Like
    Flour 200
    Rice 400

    and now Added Programmatically
    Grand Total 600

Leave a Reply

Your email address will not be published. Required fields are marked *