DataGridView Control replacing certain values with a string

I have a datagridview control which uses an array of class objects as its data source.

Is it someway possible to set it up so that certain values in the data are shown differenty in the control.

What I actually want to do is when the value is -1 show either nothing or a dash.

Cheers,

Gareth

[322 byte] By [aliasx] at [2007-12-24]
# 1

absolutely.

One way would be to iterate through each row and each column and then set the cell value or for each row, get the specific column you are after in the cell property, check its value and set its value depending on the current value (-1).

I cant seem to find a specific event that when a row and column are added to do it in there....

I hope this helps in some way

ahmedilyas at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2

Thanks.

Being natrualy lazy I was hoping there was some little known property or method I could use, but failing that I shall give your approach a bash.

Cheers,

Gareth

aliasx at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3

Hi Gareth,

you could use the CellFormatting event for formatting your original data, something in a way of:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
// You can adjust the column/type/value checking here, according to your needs
if (e.ColumnIndex == 1 && e.Value != null && e.Value.ToString() == "-1")
{
// Change the display value
e.Value = "--";
// You can also set other cell formatting styles
e.CellStyle.BackColor = Color.Yellow;
e.FormattingApplied =
true;
}
}

Andrej

AndrejTozon at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 4

Thanks.

Worked like a charm.

cheers,

Gareth

aliasx at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...