I was wondering why are there so many methods for adding a row to a DataGridView consisting of textboxes.
I have 4, if you find more let me know.
Have made a WindowsFormsApp and put on it a DataGridView, with 3 standard columns to it and a button with the following code.

private void button1_Click(object sender, EventArgs e)
        {
            AddMethod1();
            AddMethod2();
            AddMethod3();
            AddMethod4();
        }

        private void AddMethod1()
        {
            DataGridViewRow dgvR = new DataGridViewRow();

            for (int i = 0; i < 3; i++)
            {
                DataGridViewCell dgvC = new DataGridViewTextBoxCell();
                dgvC.Value = "RowCell" + i.ToString();
                dgvR.Cells.Add(dgvC);
            }
            this.dataGridView1.Rows.Add(dgvR);
        }

        private void AddMethod2()
        {
            string[] CellStrs = new string[3];
            for (int i = 0; i < 3; i++)
            {
                CellStrs[i] = "RowCell" + i.ToString();
            }
            this.dataGridView1.Rows.Add(CellStrs);
        }

        private void AddMethod3()
        {
            const int cRow = 2;
            this.dataGridView1.Rows.Add();
            for (int col = 0; col < 3; col++)
            {
                this.dataGridView1[col, cRow].Value = "RowCell" + col.ToString();
            }           
        }

        private void AddMethod4()
        {
            const int cRow = 3;
            this.dataGridView1.Rows.Add();
            for (int i = 0; i < 3; i++)
            {
                this.dataGridView1.Rows[cRow].Cells[i].Value = "RowCell" + i.ToString();
            }
        }

Don't mind the hard coding, I would like to know if some method is sometimes more preferable over another, or does it not matter?

Dani AI

Generated

Short answer: there is no single “best” API call — pick the pattern that matches the shape of your data, the volume of rows, and whether the target grid is bound or unbound. ’s scenario (an unbound target with predefined columns fed by per-column calculations from a bound source) makes the choice clearer: compute the full set of values for one output row first, then push that row to the UI in as few operations as possible.

Practical guidance and tradeoffs

  • Maintainability: use a data source (DataTable, BindingList, or a BindingSource) when you want clear separation between model and view. ’s DataTable approach is in this family; a BindingList (or a typed collection) is a lightweight alternative that updates the grid automatically and is easier to work with from code.
  • Performance and UX: for small numbers of rows it does not matter. For hundreds+ rows, avoid adding cells one-at-a-time directly to the grid. Instead build rows in memory and either bind once or add in batches while the grid is suspended.
  • Flexibility: when you need cell-level control (custom cell types, per-cell styling, or per-cell formatting) construct the row/cells before inserting so the grid receives a ready-made row rather than being mutated repeatedly.
  • Dynamic source columns: if the source column count can change, decide a mapping strategy up front — expand the target columns dynamically, compress or aggregate multiple source columns into one target cell, or reject extra columns. Programmatically adjust the target column collection before inserting rows.

Simple, practical pattern (VB.NET)
Build a small model, bind once, then add computed rows to the list so the grid updates automatically:

Public Class ResultRow
    Public Property A As String
    Public Property B As String
    Public Property C As String
End Class

Dim results As New BindingList(Of ResultRow)()
dataGridView1.AutoGenerateColumns = True
dataGridView1.DataSource = results

' After computing values for one source-column:
results.Add(New ResultRow With {.A = v1, .B = v2, .C = v3})

Troubleshooting notes

  • Do heavy calculations on a background thread and marshal only the UI update back to the UI thread.
  • Temporarily set AllowUserToAddRows = False and call SuspendLayout/ResumeLayout when doing bulk updates.
  • If the grid is data-bound, never mix direct Rows.Add manipulation with a DataSource — update the underlying data object instead.

Recommended Answers

All 3 Replies

Can't say if one is better than the other - I suppose it just gives the developer the flexibility of choosing a method based on the data he/she has in-hand. Methods 1 & 2 are closely related to each other (build an object and hand it in via a single "Add" call), while 3 & 4 are closely related (use indexes/iterate the collection to insert at a given location).

Of course, there is also the ability to load an entire data set into a DGV in one shot:

private void AddMethod5()
{
    dataGridView1.Columns.Clear();  // Remove the defined columns from the DGV

    DataTable dt = new DataTable();
    DataRow dr = dt.NewRow();
    for (int i = 0; i < 3; i++)
    {
        dt.Columns.Add("Column" + i.ToString(), typeof(String));
        dr["Column" + i.ToString()] = "RowCell" + i.ToString();
    }
    dt.Rows.Add(dr);
    dataGridView1.DataSource = dt;
}

At this point, I'm not truly in the spirit of what you originally proposed:

...adding a row to a DataGridView consisting of textboxes.

This is using the DGV as a viewing mechanism on a separate data source. As the comment indicates, it removes any column definitions in the DGV. But, it *is* yet another way to get a DGV to display data. And, of course, there are a lot of different ways to construct data sets/tables - from a database, from code, etc...

A lot of ways to skin the proverbial cat.

Let me clarify this a bit:
I have an unbound DataGridView in mind with predefined columns.
From another DGV(bound) with unknown amount of columns I do a number of calculations per column. These calculations then go in the second DGV in an added row.
During the process of doing this, this question popped up.

It is as in the saying: "All roads lead to Rome."
Solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.