Accessing DataRow from inside DataBind Event

S

shamila

we are trying to pass a value to format a value in the datacolumn. But
when we pass the datacolumn , it does not give me the value in the
column, instead gives me the column name.

the following code gives me an error invalid cast .

protected void ItemDataBoundEventHandler(object
sender,DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item ||
e.Item.ItemType==ListItemType.AlternatingItem)
{
for (int i = 2; i < oDT1.Columns.Count; i++)
{
DataColumn dc = oDT1.Columns;
string s = FormatDataColumn(ref dc);
e.Item.Cells[i-2].Text= s;

}
}
}



protected string FormatDataColumn( ref DataColumn dc)
{
string s;
if(dc.DataType==System.Type.GetType("System.DateTime"))
{
DataRow oDR;
if(Convert.ToDateTime(dc) ==DateTime.MinValue)
s="";
else
s= String.Format("{0:d}",Convert.ToDateTime(dc));
}
return s;
}
 
S

Scott Roberts

shamila said:
we are trying to pass a value to format a value in the datacolumn. But
when we pass the datacolumn , it does not give me the value in the
column, instead gives me the column name.

the following code gives me an error invalid cast .

protected void ItemDataBoundEventHandler(object
sender,DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item ||
e.Item.ItemType==ListItemType.AlternatingItem)
{
for (int i = 2; i < oDT1.Columns.Count; i++)
{
DataColumn dc = oDT1.Columns;
string s = FormatDataColumn(ref dc);
e.Item.Cells[i-2].Text= s;

}
}
}


What is "oDT1"? You should probably be using e.Item.DataItem.

The type of e.Item.DataItem will vary depending on what is bound to the
DataGrid.
 
S

shamila

Scott Roberts said:
shamila said:
we are trying to pass a value to format a value in the datacolumn. But
when we pass the datacolumn , it does not give me the value in the
column, instead gives me the column name.

the following code gives me an error invalid cast .

protected void ItemDataBoundEventHandler(object
sender,DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item ||
e.Item.ItemType==ListItemType.AlternatingItem)
{
for (int i = 2; i < oDT1.Columns.Count; i++)
{
DataColumn dc = oDT1.Columns;
string s = FormatDataColumn(ref dc);
e.Item.Cells[i-2].Text= s;

}
}
}


What is "oDT1"? You should probably be using e.Item.DataItem.

The type of e.Item.DataItem will vary depending on what is bound to the
DataGrid.


oDT1 is a a datatable. I cannot access the Datatype if i use
e.Item.DataItem.




shamila
 
S

shamila

Scott Roberts said:
shamila said:
we are trying to pass a value to format a value in the datacolumn. But
when we pass the datacolumn , it does not give me the value in the
column, instead gives me the column name.

the following code gives me an error invalid cast .

protected void ItemDataBoundEventHandler(object
sender,DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item ||
e.Item.ItemType==ListItemType.AlternatingItem)
{
for (int i = 2; i < oDT1.Columns.Count; i++)
{
DataColumn dc = oDT1.Columns;
string s = FormatDataColumn(ref dc);
e.Item.Cells[i-2].Text= s;

}
}
}


What is "oDT1"? You should probably be using e.Item.DataItem.

The type of e.Item.DataItem will vary depending on what is bound to the
DataGrid.


oDT1 is a a datatable. I cannot access the Datatype if i use
e.Item.DataItem.




shamila
 

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