How to format gridview columns?

2019-03-04 03:13发布

问题:

Below is my code:

var grid = new GridView();
grid.DataSource = myDataset;

grid.DataBind();

I want to format some of the columns of my grid to Currency with thousand separator and with decimal like '$9,999,999.99'.

I am exporting the grid to excel and I want to see the values in currency format.

I am creating gridview during runtime. I have no html code for my gridview.

I am just bounding dataset to gridview and exporting to excel. I won't see gridview in HTML format.

How to do that?

回答1:

Create a BoundField for the currency column with appropriate DataFormatString.

<asp:BoundField  DataField="currencyColumnName" DataFormatString="{0:c}" />


回答2:

Google brought me here, so I thought I'd share my answer. Here's how I got to format one cell to currency in asp.net.

With Me.ratesGridView
        .Rows(i).Cells(4).Text = FormatCurrency(.Rows(i).Cells(4).Text)
End With

Enjoy.