DataGrid

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

On the OnItemDataBound of a datagrid i am trying to check some condition as
shown in the code below:

for(int i = 0; i < ReportDataGrid.Columns.Count; i++)
{
if(ReportDataGrid.Columns.HeaderText=="Brand/Model")
{
intIndex = i;
}
}

Now how do i access the variable 'intIndex' inside the code below:

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
e.Item.Cells[intIndex].Text = "Hello";
}
I get build error "Use of unassigend local variable 'inIndex' WHY???

Many thanks in advance
 
huzz,

The C# compiler will not let you use a variable that has not been initialised.

Although your logic *might* mean that intIndex will be assigned a value at
runtime (that is, the "if" condition in the "for" loop will eventually
succeed), it is not possible for the compiler to know this at build time,
hence the error.

If you're sure that intIndex will eventually be assigned a valid value
before it is used for accessing e.Item.Cells, then try initialising intIndex
= 0 before the "for" loop or when it is declared.

Hope this helps...

Patrick
 
Make Sense :) many thanks


P.Simpe-Asante said:
huzz,

The C# compiler will not let you use a variable that has not been initialised.

Although your logic *might* mean that intIndex will be assigned a value at
runtime (that is, the "if" condition in the "for" loop will eventually
succeed), it is not possible for the compiler to know this at build time,
hence the error.

If you're sure that intIndex will eventually be assigned a valid value
before it is used for accessing e.Item.Cells, then try initialising intIndex
= 0 before the "for" loop or when it is declared.

Hope this helps...

Patrick

huzz said:
On the OnItemDataBound of a datagrid i am trying to check some condition as
shown in the code below:

for(int i = 0; i < ReportDataGrid.Columns.Count; i++)
{
if(ReportDataGrid.Columns.HeaderText=="Brand/Model")
{
intIndex = i;
}
}

Now how do i access the variable 'intIndex' inside the code below:

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
e.Item.Cells[intIndex].Text = "Hello";
}
I get build error "Use of unassigend local variable 'inIndex' WHY???

Many thanks in advance
 
Back
Top