DataGridView CellFormating event problem ?

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
 
J

jmd.msdn

Problem solved.
It was my fault.
I used dgvLabels.Columns["colID"].Index before setting the
dgvLabels.DataSource !!!
jmd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top