David
David

Reputation: 16067

Programmatically edit text in DataGridViewCell

I need to edit the text of a DataGridViewCell. To be specific, I need to change the text from -1 to "N/A"

First I tried this:

dgResults.Rows[0].Cells[0].Value = "N/A";

But it complained at runtime:

enter image description here

After that, I tried changing the datatype of the column:

dTable.Columns[0].DataType = System.Type.GetType("System.String");  

and I get this error:

Cannot change DataType of a column once it has data.

I have no access to the database and cannot modify it.

Upvotes: 1

Views: 3462

Answers (2)

Tergiver
Tergiver

Reputation: 14517

You do this by handling the CellFormatting event.

Upvotes: 1

nawfal
nawfal

Reputation: 73283

Why are u changing datatype of datatable? Why not change column type of dgv? try this:

dgResults.Columns[0].ValueType = typeof(string);
dgResults.Rows[0].Cells[0].Value = "N/A";

Use appropriate column index

Upvotes: 0

Related Questions