0

I have a DataGridView with some text columns, combo box columns and an image column. When I try to add a blank new row by calling DataGridView1.Rows.AddCopy(0) I get the following error: System.FormatException: Formatted value of the cell has a wrong type.

The new row does not contain any data. When the user adds a new row through the GUI (i.e. not programmatically) there is no problem. Does anyone have any ideas as to what might be causing this issue?

6
  • How can someone add a row and not have it be programmatically? This is a program, it has to be added via code. Do you mean not with your code? I believe your problem is there is nothing to copy, try Rows.Insert(1,0) instead. Commented Jan 25, 2013 at 19:00
  • I mean not within my code. The user can add the new row through DataGridView's internal code. Commented Jan 25, 2013 at 19:02
  • are you using databindings? Commented Jan 25, 2013 at 19:04
  • Are you giving it a type? Commented Jan 25, 2013 at 19:04
  • @Haxx: no I'm not using databindings. @plast1K: I'm not giving it a type either, my understanding is that the new row should be already a DataGridViewRow. Commented Jan 25, 2013 at 19:06

1 Answer 1

3

Does this help?

        // Create a new row on a datagridview
        var rowId = dataGridView1.Rows.Add();

        // fill the cells in the newly created row with your data
        dataGridView1.Rows[rowId].Cells[ColumnText.Name].Value = "";
        dataGridView1.Rows[rowId].Cells[ColumnCheckbox.Name].Value = true;
        dataGridView1.Rows[rowId].Cells[ColumnImage.Name].Value = null; // add image
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, thanks that works really well. I figured AddCopy would do that automatically. I'm not sure what the difference is between Add and AddCopy.
@HH - With AddCopy() you have to have something to copy, as I said in my example... Insert would also work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.