Link to home
Start Free TrialLog in
Avatar of Codingitup
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="SqlDataSource1">
        <AlternatingRowStyle BackColor="#FFCC00" />
        <Columns>
            <asp:BoundField DataField="OrderNo" HeaderText="OrderNo"
                SortExpression="OrderNo" />
            <asp:BoundField DataField="MailingName" HeaderText="MailingName"
                SortExpression="MailingName" />
            <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
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

In RowDataBound you can do this if you know the column index:

Protected Sub GridView1_RowDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then            
            e.Row.Cells.Item(1).Text += "%"
        End If
    End Sub
ASKER CERTIFIED SOLUTION
Avatar of pvr
pvr
Flag of India image

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
I'm guessing it would be column 4 in your case.

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.

<asp:BoundField DataField="MarginPer" HeaderText="MarginPer" ReadOnly="True" DataFormatString="{0}%" SortExpression="MarginPer" />

Open in new window


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.