2

I have a DataGridView in C# and I want to add rows in a programmatic way. There is no data bound to the grid but when I call dataGrid.Rows.Add(); it throws a System.InvalidOperationException.

I looked all over the internet and I only found this problem for people who have data bound to it. I want the grid to be controlled completely from the code.

Could anyone help me with this please?

Not sure if it makes a difference but I use .Net framework 3.5.

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
Sanctus2099
  • 1,669
  • 5
  • 22
  • 40
  • 2
    It is difficult to tell exactly without seeing some of the code. The details of the Rows.Add() Method are here: http://msdn.microsoft.com/en-us/library/x0akdhcd.aspx and it includes a list of things that will cause an InvalidOperationException. – GarethD Feb 01 '12 at 10:30

3 Answers3

5

Assuming you have created the columns, either with the designer or by code you can do:

var row = (DataGridViewRow)myDataGridView.RowTemplate.Clone();
row.CreateCells(myDataGridView, "I'm Cell 1", "I'm Cell 2", "etc.");
myDataGridView.Rows.Add(row);

Ideally if you are adding many rows you would create an array of rows upfront and call AddRange(rows); instead.

Example:

void PopulateGrid()
{
    //Consider Suspend-Resume Layout, YMMV.
    var rows = myData.Select(data => CreateRow(data)).ToArray();
    myDataGridView.Rows.AddRange(rows);
}

DataGridViewRow CreateRow(MyData data)
{
    var row = (DataGridViewRow)myDataGridView.RowTemplate.Clone();
    row.CreateCells(myDataGridView, data.Text, data.Date, date.Value);
    return row;
}
DaveShaw
  • 52,123
  • 16
  • 112
  • 141
  • Yes, I also confirm that using `AddRange` helps to speed up the population process a lot. However, to make it even faster, we also need to turn redrawing off. Unbound DataGridView has other problems - read the [Unbound DataGridView: Typical Problems](http://10tec.com/articles/unbound-datagridview.aspx) article to find out more. – TecMan Mar 04 '16 at 09:36
2

the easiest example i could give is:

/// <summary>
/// Shows example usage of Add method on Rows.
/// </summary>
void M()
{
    //
    // n is the new index. The cells must also be accessed by an index.
    // In this example, there are four cells in each row.
    //
    int n = dataGridView1.Rows.Add();

    dataGridView1.Rows[n].Cells[0].Value = title;
    dataGridView1.Rows[n].Cells[1].Value = dateTimeNow;

    //
    // The second cell is a date cell, use typeof(DateTime).
    //
    dataGridView1.Rows[n].Cells[1].ValueType = typeof(DateTime);
    dataGridView1.Rows[n].Cells[2].Value = wordCount;
}
John Woo
  • 258,903
  • 69
  • 498
  • 492
1

I usually go with answers provided by other people but this is not the case since the answers aren't really helpful.

As I stated "dataGridView1.Rows.Add();" threw an exception and so did AddRange.
I found out the answer after doing a lot of checks. Apparently .Net does not like it if I add a lot of rows/second (about 30).
I receive my rows via networking so I created a pool of rows and every second I updated the rows from the datagridview.
This seems to have fixed both the rows not showing up and the exceptions.

Thank you for the input anyway!

Sanctus2099
  • 1,669
  • 5
  • 22
  • 40