DataGrid Formatting

lothos12345

Junior Contributor
Joined
May 2, 2002
Messages
294
Location
Texas
I have a datagrid that displays the data I want it to. The problem is that I am trying to format the last column in the datagrid as currency and it is not allowing me to. The columns in the datagrid are generated automatically at runtime and the connection to the database was created in VB.NET code. There are 9 columns counting from 0, when I do a column count it returns the number of 0 which is not the case. Thus I am unable to format the last column in the datagrid as currency. Any help with this problem would be greatly appreiciated.
 
lothos12345 said:
I have a datagrid that displays the data I want it to. The problem is that I am trying to format the last column in the datagrid as currency and it is not allowing me to. The columns in the datagrid are generated automatically at runtime and the connection to the database was created in VB.NET code. There are 9 columns counting from 0, when I do a column count it returns the number of 0 which is not the case. Thus I am unable to format the last column in the datagrid as currency. Any help with this problem would be greatly appreiciated.
I think if you do the format in the datagrid's prerender event, you should be fine!
 
Set AutoGenerateColumns property to "False" and create your own BoundColumn subcontrol to lay out your grid. This will allow you to use the DataFormatString property. Below is an example for you to review.



<asp:DataGrid id="DataGrid1" BorderColor="gray" runat="server"
AutoGenerateColumns="false" CellPadding="2" Font-Name="Arial" Font-Size="10pt"
HeaderStyle-BackColor="lightgreen"
AllowPaging="True"
PageSize="4"
PagerStyle-Mode="NumericPages"
OnPageIndexChanged="DataGrid1_PageIndexChanged">
<Columns>
<asp:BoundColumn HeaderText="Customer#" DataField="CustNum"
ItemStyle-HorizontalAlign="Center"/>
<asp:BoundColumn HeaderText="Last Name" DataField="LastName"/>
<asp:BoundColumn HeaderText="Order#" DataField="OrdNum"
ItemStyle-HorizontalAlign="Center"/>
<asp:BoundColumn HeaderText="Order Date" DataField="OrderDate"
DataFormatString="{0:MM-dd-yy}" ItemStyle-HorizontalAlign="Center"/>
<asp:BoundColumn HeaderText="Item Description" DataField="Description"/>
<asp:BoundColumn HeaderText="Quantity" DataField="LIQuantity"
ItemStyle-HorizontalAlign="Center"/>
<asp:BoundColumn HeaderText="Unit Price" DataField="Price"
DataFormatString="{0:C}" ItemStyle-HorizontalAlign="Right"/>
<asp:BoundColumn HeaderText="Ext Price" DataField="ExtPrice"
DataFormatString="{0:C}" ItemStyle-HorizontalAlign="Right"/>
</Columns>
</asp:DataGrid>
 
Back
Top