views:

84

answers:

4

i have a gridview populated by the code below:

protected void CautaProiect_Click(object sender, EventArgs e)
        {
            wipDBTableAdapters.GetSummaryProiectTableAdapter proiecte = new wipDBTableAdapters.GetSummaryProiectTableAdapter();
            SummaryGrid.DataSource = proiecte.GetData(CodProiect.Text);
            SummaryGrid.DataBind();
        }

The gridview will be populated with some columns with values. The problem is that the values are formated like this 1234.5600 and i want them to be like 1,234.56

How ca i do this ?

+1  A: 

You can format your data in the OnRowDatabound event

sample:

    protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label l = (Label)e.Row.FindControl("lblValue");
            l.Text = String.Format("{0:C}", l.Text);
        }
    }
TheGeekYouNeed
what is this label ? can you please explain the code a little bit more?
Iulian
A: 

In your GridView columns, use the DataFormatString property and set it to the format you prefer. In your case, you'll want to use "N2".

A great cheatsheet of other formatting options can be found here.

Dillie-O
how do i call DataFormatString ? the field are not bounded
Iulian
You should be able to use the DataFormatString on your template column as well. If you're binding all your sources via code behind, then you'll want to look at Cen's solution. The format string link I provided still applies.
Dillie-O
A: 

you can use the following code to display in the gridview ItemTemplate

  <asp:Label ID="lblFinalPrice" runat="server" Text='<%#Convert.ToDouble(Eval("FinalPrice")).ToString("#.00")%>'></asp:Label>
ricky roy
A: 

I have finally managed to find an answer for this :

Here is how :

protected void SummaryGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
         if (e.Row.RowType == DataControlRowType.DataRow)  
         {  
             double value = Convert.ToDouble(e.Row.Cells[4].Text);  
             e.Row.Cells[4].Text = value.ToString("#,#.##");              
         }  
Iulian