How to use the DataFormatString to format DataGrid data dynamically

Platform: ASP.NET| Category: DataGrid

In the ItemDataBound Event of DataGrid write following code.

VB.NET


Dim ds As DataSet = CType(DataGrid1.DataSource, DataSet)
Dim dv As DataView = ds.Tables(0).DefaultView
Dim dcCol As DataColumnCollection = dv.Table.Columns
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
   e.Item.Cells(dcCol.IndexOf(dcCol('UnitPrice'))).Text = DataBinder.Eval(e.Item.DataItem, 'UnitPrice', '{0:c}')
End If

C#


DataSet ds = (DataSet)DataGrid1.DataSource ;
DataView dv = ds.Tables[0].DefaultView ;
DataColumnCollection dcCol  = dv.Table.Columns ; 
if ((e.Item.ItemType == ListItemType.Item )||( e.Item.ItemType == ListItemType.AlternatingItem ))
{
	e.Item.Cells[dcCol.IndexOf (dcCol ['UnitPrice'])].Text = DataBinder.Eval(e.Item.DataItem, 'UnitPrice', '{0:c}');
}

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.