Wil Youmans

Wil Youmans

  • NA
  • 25
  • 0

What would be the literal value of an empty DataGridView cell? (CellFormatting)

May 22 2011 10:33 PM
Hi,

I have a datatable in the form of:

dtTimesHelper.Columns.Add("TableID", typeof(int));
dtTimesHelper.Columns.Add("ClientID", typeof(int));
dtTimesHelper.Columns.Add("First Name", typeof(string));
dtTimesHelper.Columns.Add("Initial", typeof(string));
dtTimesHelper.Columns.Add("Last Name", typeof(string));
dtTimesHelper.Columns.Add("The Date", typeof(string));
dtTimesHelper.Columns.Add("Time In", typeof(string));
dtTimesHelper.Columns.Add("Time Out", typeof(string));

I can only insert the 'TableID' after retrieving the Identity from an Insert into the database, but until then the cell is empty. When the cell contains a new int (Identity), I want to change the color of the row. I am trying to use this but the if clause always evaluates to true:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
// If the column is the TableID column, check the
// value.
if (this.dataGridView1.Columns[e.ColumnIndex].Name == "TableID")
{
if (e.Value != null)
{
foreach(DataGridViewCell dvc in dataGridView1.Rows[e.RowIndex].Cells)
dvc.Style = savedStyle;
}
}
}

It would seem e.Value is a type of Int32 and so really not null, but what literal value does it have since the cell is empty? How could I rewrite my if clause to check if an actual int is in the cell or not?

Thanks in advance.

Answers (4)