Home All Groups Group Topic Archive Search About

DataGridView CellFormating event problem ?

Author
2 Jul 2006 8:52 PM
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

Author
2 Jul 2006 10:49 PM
jmd.msdn
Problem solved.
It was my fault.
I used dgvLabels.Columns["colID"].Index before setting the
dgvLabels.DataSource !!!
jmd

Show quote
"jmd.msdn" <jmd.msdn@newsgroup.nospam> wrote in message
news:uusIzkhnGHA.4328@TK2MSFTNGP04.phx.gbl...
> 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
>
>
>

AddThis Social Bookmark Button