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?