views:

5517

answers:

6

In C#

I have a processing time number data column in the database which is in in this format "###" or "##" ( eg: "813" or "67")

When I bind it to the grid view I wanted to display it in this format "0.###" (eg: "0.813" or "0.067")

I tried using {0:0.000} and other formatings. But none seem to work. Can anyone tell me how to write the format string?

+1  A: 

You need to disable HTML encoding on that column for the format string to take effect.

Further Reading

Greg
+2  A: 

You should really multiply it by .001 before displaying it, then you can use the normal {0:0.000} format string.

If, for some reason, that's not possible - you can use a format string of {0:0\\.000} which uses the "." not as a decimal separator, but as a literal.

Mark Brackett
A: 

If for some reason you can't change the value before you bind it to the grid, you can handle the RowDataBound event and divide the number by 1000 before displaying it.

// Handle the grid's RowDataBound event
MyGridView.RowDataBound += new GridViewRowEventHandler(MyGridView_RowDataBound);

// Set the value to x / 1000 in the RowDataBound event
protected void MyGridView_RowDataBound( object sender, GridViewRowEventArgs e )
{
    if( e.Row.RowType != DataControlRowType.DataRow )
      return;

    // Cast e.Row.DataItem to whatever type you're binding to
    BindingObject bindingObject = (BindingObject)e.Row.DataItem;

    // Set the text of the correct column.  Replace 0 with the position of the correct column
    e.Row.Cells[0].Text = bindingObject.ProcessingTime / 1000M;
}
Greg
Should you not be dividing by 1000 ?
Kev
Yeah, I guess so. I guess I wasn't worried about that particular part.
Greg
A: 

So are you looking for something like this?

String.Format("{0:0.000}", test/1000);

avgbody
A: 

Do you mean?

GridView.DataSource = ....

BoundField total = new BoundField(); total.DataField = "Total"; total.HeaderText = "Total Amount"; total.DataFormatString = "{0:C}"; donations.Columns.Add(total);

......

A: 
Nice Posts...it really healped....but better to use it this way


    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (((DataRowView)(e.Row.DataItem))["Columnname"].ToString().Equals("Total", StringComparison.OrdinalIgnoreCase))
                {
                    e.Row.Font.Bold = true;
                    //-----or ant thing you want to do------
                }
            }
        }
        catch (Exception ex)
        {

        }
Vinod Kumar