Data format in a DataGridView in a Windows application

I cannot show the currency format in a DataGridView. Can you see this code.

private void dataGridView1_DataBindingComplete(object sender,
                                   DataGridViewBindingCompleteEventArgs e)
{
    objPreview.dataGridView1.Columns["Debit"].DefaultCellStyle.Format = "c";
    objPreview.dataGridView1.Columns["Credit"].DefaultCellStyle.Format = "c";
}
+5
source share
6 answers

if it's a windows form, than writing this code before you bind the data to the grid ... something lower in the form of consturctor ...

public Form1()
{
   this.dataGridView1.Columns["UnitPrice"].DefaultCellStyle.Format = "c";
}

if its ASP.Net try something like DataFormatString="{0:c}"

<asp:BoundField HeaderText="Price/Unit" 
                DataField="UnitPrice" SortExpression="UnitPrice" 
                DataFormatString="{0:c}">
                    <ItemStyle HorizontalAlign="Right"></ItemStyle>
+4
source

Try to add this.

objPreview.dataGridView1.Columns["Debit"].ValueType = Type.GetType("System.Decimal")
objPreview.dataGridView1.Columns["Credit"].ValueType = Type.GetType("System.Decimal")
+4
source

( - ), DataBindingComplete . , , , .

() , , , ,

http://msdn.microsoft.com/en-us/library/k4sab6f9

, ,

+1
source

You can also use this for Windows Forms: (or its equivalent for WPF or ASP, ...)

yourColumn.DefaultCellStyle.Format = "#,#";

// And add below if you want
yourColumn.DefaultCellStyle.NullValue= "0";
0
source

If you have already bound your DataSource to a DataGridView, you can set your cell style by setting each cell format, for example:

foreach (DataGridViewRow row in dataGridView1.Rows)
{
  row.Cells["Debit"].Style.Format = "c";
  row.Cells["Credit"].Style.Format = "c";  
}

Make sure your value is numeric.

0
source

Try adding the @Pranay Rana code suggested in the CellFormatting Event in the DataGridView:

private void NameOfYourGridGoesHere_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    this.NameOfYourGridGoesHere.Columns["UnitPrice"].DefaultCellStyle.Format = "c";
}
0
source

All Articles