DataGridView CellFormating event problem ?

  • Thread starter Thread starter jmd.msdn
  • Start date Start date
J

jmd.msdn

Hello, the following DataGridView eventhandler poses problem :

// I am working with .NET 2.0 in Visual Studio 2005 Pro on a Windows XP SP2
computer
// dgvLabels is a DataGridView control
private void dgvLabels_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
try
{
// (1) int colID = dgvLabels.Columns["colID"].Index;
// (2) int colID = 0
if ( e.ColumnIndex != colID ) return;
// format the data in the colID column...
}
catch ( Exception exc ) {}
}

If I un-comment the line (1) above, an exception is generated : "Object
reference not set to an instance of an object."
If I un-comment the line (2) above, NO exception is generated and all works
fine

The value of dgvLabels.Columns["colID"].Index is 0
"colID" is the first column in my current table (it has 4 columns)

Can someone helps ?
Thanks
jmd
 
Problem solved.
It was my fault.
I used dgvLabels.Columns["colID"].Index before setting the
dgvLabels.DataSource !!!
jmd
 
Back
Top