Codingitup
asked on
Add additional text to databound cell in gridview c# ASP.net
Hi All,
I've got a datagrid view that has databound cells that return a numeric value. I'm trying to find out how I append to the data retrieved. Ie, if the cell receives 95.6 I would like to append a '%' symbol at the end to produce '95.6%'. I don't want to do this in my sql code as I need to do some other code depending on the number received.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False "
DataSourceID="SqlDataSourc e1">
<AlternatingRowStyle BackColor="#FFCC00" />
<Columns>
<asp:BoundField DataField="OrderNo" HeaderText="OrderNo"
SortExpression="OrderNo" />
<asp:BoundField DataField="MailingName" HeaderText="MailingName"
SortExpression="MailingNam e" />
<asp:BoundField DataField="Sale Price" HeaderText="Sale Price"
SortExpression="Sale Price" ReadOnly="True" />
<asp:BoundField DataField="Cost" HeaderText="Cost" SortExpression="Cost"
ReadOnly="True" />
<asp:BoundField DataField="MarginPer" HeaderText="MarginPer" ReadOnly="True"
SortExpression="MarginPer" />
</Columns>
</asp:GridView>
Many Thanks
Lee
I've got a datagrid view that has databound cells that return a numeric value. I'm trying to find out how I append to the data retrieved. Ie, if the cell receives 95.6 I would like to append a '%' symbol at the end to produce '95.6%'. I don't want to do this in my sql code as I need to do some other code depending on the number received.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False
DataSourceID="SqlDataSourc
<AlternatingRowStyle BackColor="#FFCC00" />
<Columns>
<asp:BoundField DataField="OrderNo" HeaderText="OrderNo"
SortExpression="OrderNo" />
<asp:BoundField DataField="MailingName" HeaderText="MailingName"
SortExpression="MailingNam
<asp:BoundField DataField="Sale Price" HeaderText="Sale Price"
SortExpression="Sale Price" ReadOnly="True" />
<asp:BoundField DataField="Cost" HeaderText="Cost" SortExpression="Cost"
ReadOnly="True" />
<asp:BoundField DataField="MarginPer" HeaderText="MarginPer" ReadOnly="True"
SortExpression="MarginPer"
</Columns>
</asp:GridView>
Many Thanks
Lee
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I'm guessing it would be column 4 in your case.
e.Row.Cells.Item(4).Text += "%"
e.Row.Cells.Item(4).Text += "%"
Oh, missed the C# in the question title.
You can also set the DataFormatString to specify a format string for the value. See http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring.aspx for more information.
One would normally use the format string {0:P} but in your case the value is higher than 1 so you should use the custom format.
<asp:BoundField DataField="MarginPer" HeaderText="MarginPer" ReadOnly="True" DataFormatString="{0}%" SortExpression="MarginPer" />
One would normally use the format string {0:P} but in your case the value is higher than 1 so you should use the custom format.
Protected Sub GridView1_RowDataBound(ByV
If e.Row.RowType = DataControlRowType.DataRow
e.Row.Cells.Item(1).Text += "%"
End If
End Sub