Link to home
Start Free TrialLog in
Avatar of Hojoformo
Hojoformo

asked on

Dynamic DataGrid - DataFormatString?

How can I define a dataformat string for a datagrid created dynamically?   Some of the columns of my datagrid need to be formated.  For static datagrids I was using a command like "DataFormatString="{0:c})" but I do not know how to apply this for a dynamic datagrid.


e.Item.Cells[1].Wdith = Unit.Pixel(200);
e.Item.Cells[1].Style  =  "{0:c})"    ???????  how do I format numeric and decimal fields???  
 
Avatar of mzalewski
mzalewski

DataFormatString is a BoundColumn property.

Try using:

DataGrid parentDataGrid = ((DataGrid)sender);
((BoundColumn)parentDataGrid.Columns[1]).DataFormatString = "{0:c})";

I havent tested this but it should work;

Avatar of Hojoformo

ASKER

Thank you for your suggestion but I am having a little problem getting it to work.  Can you help?

dgrdProducts.ItemDataBound += new DataGridItemEventHandler(DGrid_ItemDataBound);           
dgrdProducts.DataBind();
DataGrid parentDataGrid = ((DataGrid)sender);  ???? Error - Specified cast is not valid????
((BoundColumn)parentDataGrid.Columns[1]).DataFormatString = "{0:c})";

Using the code you suggested I get an error "Specified Cast is Not Valid" .  Any suggestions?  


ASKER CERTIFIED SOLUTION
Avatar of mzalewski
mzalewski

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial