To format data in a gridview boundcolumn or Eval statement using the property of the boundcolumn called DataFormatString or include the format in the Eval statement like this: [I added an extra space between the brackets so this would format correctly]
< %# Server.HtmlEncode( Eval("InvoiceAmount", "{0:$#,#.#0}").ToString() ) % >"
I got this from a great article http://blog.stevex.net/string-formatting-in-csharp/
Using the decimal value 4219.60 as my number from the database for a product.
{0}
4219.60
{0:c}
$4,219.60
{0:c2}
$4,219.60
{0:c4}
$4,219.6000
{0:$#.#}
$4219.6
{0:$#,#.#}
$4,219.6
{0:#,#.##}
$4,219.6
{0:$#,#.#0}
$4,219.60
Wednesday, December 9, 2009
Tuesday, December 8, 2009
The Value of Datakeys in GridView RowDataBound
To find the value of your gridview attribute Datakeys use this C# code
protected void gvSummary_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataTable dt = new DataTable();
if (e.Row.RowType == DataControlRowType.DataRow)
{
//find the invoice number - which is the Datakeys
int inv = Convert.ToInt32(((GridView)sender).DataKeys[e.Row.RowIndex].Value);
//find the charges grid - populated based on the invoice number
GridView gvCh = (GridView)e.Row.FindControl("gvCharges");
dt = maData.TrapRetrievalInvoiceChargesGet(inv);
//populate a nested grid
gvCh.DataSource = dt;
gvCh.DataBind();
}
}
ASP Code
asp:GridView ID="gvSummary" runat="server" DataKeyNames="Invoice"
...
protected void gvSummary_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataTable dt = new DataTable();
if (e.Row.RowType == DataControlRowType.DataRow)
{
//find the invoice number - which is the Datakeys
int inv = Convert.ToInt32(((GridView)sender).DataKeys[e.Row.RowIndex].Value);
//find the charges grid - populated based on the invoice number
GridView gvCh = (GridView)e.Row.FindControl("gvCharges");
dt = maData.TrapRetrievalInvoiceChargesGet(inv);
//populate a nested grid
gvCh.DataSource = dt;
gvCh.DataBind();
}
}
ASP Code
asp:GridView ID="gvSummary" runat="server" DataKeyNames="Invoice"
...
Subscribe to:
Posts (Atom)