Visual Studio C#: DataGridView not showing integer

VAer 756 Reputation points

Server: MS SQL. There is a field Value, which is supposed to be integer.

Attached screenshot: why it does not show integer?



AdapterTD = new OdbcDataAdapter("SELECT * from TableName" ORDER BY CAST(Case_number AS Int)", GlobalVariables.ConnectionString);  
                dSetTD = new DataSet();  
                dgvTD.DataSource = dSetTD.Tables[0];  
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,985 questions
{count} votes

Accepted answer
  1. Jack J Jun 24,271 Reputation points Microsoft Vendor

    @VAer , you could try the CellFormatting event to show integer in your datagridview.

     public Form1()  
                dataGridView1.CellFormatting += DataGridView1_CellFormatting;  
            private void DataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)  
                if (e.ColumnIndex == 3 && e.RowIndex != this.dataGridView1.NewRowIndex)  
                    double d = double.Parse(e.Value.ToString());  
                    e.Value = d.ToString("N0");  

    Result in database and in datagirdview:


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Viorel 110.1K Reputation points

    Maybe an SqlDataAdapter will work differently.

    If the columns are not autogenerated, check the definition of this column in Form Designer. You can also change the DefaultCellStyle, setting Format to "N0".

    0 comments No comments