0

I have a DatGridView to which I add new rows. The first column of every row is an image the indicates whether the rest of the row has been completed. To keep track of this each row has a List-like object attached to it that counts the number of completed fields, then changes the image if enough fields have been completed. This works exactly as expected when the user is adding rows in the GUI, however I run into problems when I try to add new rows programmatically.

When a new row is created, the DataGridViewImageCell from the first column is added to the custom class that keeps track of the completed fields.

Here is the code to add a new row:

//initiate the new row
object[] buffer = new object[13];
buffer[0] = nullImage; //a blank image

for (int i = 1; i < buffer.Length; i++)
{
     buffer[i] = String.Empty;
}

rowID = DataGridView1.Rows.Add(buffer);

Here is the RowAdded code:

    private void DataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
    {
        DataGridViewImageCell tmp = (DataGridViewImageCell)DataGridView1.Rows[DataGridView1.RowCount - 1].Cells[0];
        DataGridView1ValidateFields.AddRow(tmp); //custom class to keep track of completed fields
    }

The custom class contains a List of DataGridViewImageCells dgvic; here is its' AddRow method:

    private List<object> dgvic = new List<object>();
    public void AddRow(DataGridViewImageCell p)
    {
        items.Add(new List<int>());//List of ints to keep track of completed fields
        dgvic.Add(p);
    }

When I enter the debugger (while adding rows programmatically) I can see that each time I add a new row the DataGridViewImageCells stored in dgvic are all from the most recent row. For instance after adding 3 rows, I'd expect the 3 DataGridViewImageCells to be from rows 0, 1 and 2; however all of them are from row 2.

It looks to me that RowCount is still tied to each DataGridViewCell, and as RowCount increments the cell changes. If this were the case though, how come it works through the GUI? Does anyone have any ideas?

9
  • Is your dgv databound?
    – Marius
    Commented Feb 5, 2013 at 16:08
  • @Marius: no, it's not. I should have mentioned that. Commented Feb 5, 2013 at 16:09
  • Are you adding multiple rows at once? Ie DataGridView1.Rows.AddRange(<list_of_rows>);?
    – gzaxx
    Commented Feb 5, 2013 at 16:16
  • @gzaxx: no, the only place I add rows is through the code above. Commented Feb 5, 2013 at 16:17
  • Try if it helps to change this line: DataGridViewImageCell tmp = (DataGridViewImageCell)DataGridView1.Rows[DataGridView1.RowCount - 1].Cells[0]; to this one: DataGridViewImageCell tmp = (DataGridViewImageCell)DataGridView1.Rows[DataGridView1.Rows.Count - 1].Cells[0];
    – gzaxx
    Commented Feb 5, 2013 at 16:21

1 Answer 1

2

Change yours AddedRows event as follows:

private void DataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
    int index = e.RowIndex;
    for (int i = 0; i < e.RowCount; i++)
    {
        DataGridViewImageCell tmp = (DataGridViewImageCell)DataGridView1.Rows[index].Cells[0];
        DataGridView1ValidateFields.AddRow(tmp);
        index++;
    }
}
2
  • That actually does work! Can you explain why this works and my previous attempt didn't? Commented Feb 5, 2013 at 17:08
  • 1
    To be honest I'm not sure. I ran into similar problem but when I was adding an array of rows to DataGridView not only one. My guess is that this event is not fired instantly after adding row (waiting for next?) and that is why RowCount was already incremented and then event fired for 3 rows added. But that is only a guess, tomorrow I will try to check this theory and will come back to you with answer (hopefully :) ).
    – gzaxx
    Commented Feb 5, 2013 at 17:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.