Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a question and I'll be grateful if you answer me .
I have written a C# program and I've exported excel files to my form's DataGridView in my win app but I have a problem , I don't know how I can show my numbers with thousand separator in DGV while DefaultCellStyle doesn't work for solving this problem .
What should I do?

thanks in advance
Posted
Comments
Appokea 19-Feb-16 4:43am    
Maria in case you're interested there is a direct way how you can export an excel file into a DataGridView with C#. Note that this approach keeps all the formatting from your excel file.

1 solution

Add a handler for the CellFormatting event of your grid, thus:
C#
myGridView.CellFormatting += new System.Windows.Forms.DataGridViewCellFormattingEventHandler(this.myGridView_CellFormatting);


Then add a handler to get the cell's text and adjust it to your own style; something like:
C#
private void myGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    string strData;
    if (e.Value != null)
    {
        strData = (string)e.Value;
        if (myGridView.Columns[e.ColumnIndex].Name == // "< your column name here >")
        {
            // Adjust the text of the cell to the correct format and paste it back
            e.Value = // < adjust the text as required here >
            e.FormattingApplied = true;
        }
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900