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?

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.