Use the "C" parameter in the ToString function, and make sure you set the globalaztion attributes.
string parseValueIntoCurrency(double number) {
// set currency format
string curCulture = Thread.CurrentThread.CurrentCulture.ToString();
System.Globalization.NumberFormatInfo currencyFormat = new
System.Globalization.CultureInfo(curCulture).NumberFormat;
currencyFormat.CurrencyNegativePattern = 1;
return number.ToString("c", currencyFormat);
}
If you want to use a different Culture (say you're in USA, and you want the Indian format) then just use the appropriate CultureInfo element rather than getting it out of the current thread.
EXTRA INFO DUE TO OP EDIT
All right, what you're wanting to do to get this into your grid, is to create a PROTECTED function which takes in the number to be converted, and returns the converted string (this is basically the code above.
Now, on the ASPX side, you need to use that function in your grid view.
So, instead of this:
<asp:TemplateField HeaderText="Total Price (in lacs)" >
<EditItemTemplate>
<asp:TextBox ID="txttotal_price" runat="server"
Text='<%# Eval("total_price") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbltotal_price" runat="server"
Text='<%# Eval("total_price") %>'> />
</ItemTemplate>
</asp:TemplateField>
you'll use this templatefield:
<asp:TemplateField HeaderText="Total Price (in lacs)" >
<EditItemTemplate>
<asp:TextBox ID="txttotal_price" runat="server"
Text='<%# Eval("total_price") %>' />
</EditItemTemplate>
<ItemTemplate>
<%# parseValueIntoCurrency(Eval("total_price")) %>'>
</ItemTemplate>
</asp:TemplateField>
Note, two things. The first is that I'm still passing the UNFORMATTED value into the EDIT TEMPLATE, and that I'm not instantiating an extra LABEL in the ITEM TEMPLATE.
The reason I'm not doing the extra label, is because we just don't need it in there. That's just a bit more processor/memory overhead that you just don't need to incur.
As for passing the unformatted value to the text field, that's because it'll ultimately be easier to validate without having to parse out the commas and other string elements.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…