Currency format in DataGridView in windows application

后端 未结 6 2022
一个人的身影
一个人的身影 2021-01-13 06:55

i am unable to show currency format on DataGridView. Can you people look this code.

private void dataGridView1_DataBindingComplete(object sender,
                    


        
相关标签:
6条回答
  • 2021-01-13 07:22

    if it is windows form than write this code before you are binding data to the grid...something as below in the form 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>
    
    0 讨论(0)
  • 2021-01-13 07:22

    Also you can 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 讨论(0)
  • 2021-01-13 07:35

    Try adding this

    objPreview.dataGridView1.Columns["Debit"].ValueType = Type.GetType("System.Decimal")
    objPreview.dataGridView1.Columns["Credit"].ValueType = Type.GetType("System.Decimal")
    
    0 讨论(0)
  • 2021-01-13 07:35

    Can you check ( breakpoint or something ) that DataBindingComplete event is fired . So at least we know its not that

    ( edited ) So if its fired check this maybe it will help

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

    Maybe by creating a new style it will help

    0 讨论(0)
  • 2021-01-13 07:43

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

    private void NameOfYourGridGoesHere_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        this.NameOfYourGridGoesHere.Columns["UnitPrice"].DefaultCellStyle.Format = "c";
    }
    
    0 讨论(0)
  • 2021-01-13 07:44

    If you have already binded your DataSource to the DataGridView, you can set your cell style by setting each cell format like:

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

    Make sure your value is numerical.

    0 讨论(0)
提交回复
热议问题