Visual Studio C#: DataGridView not showing integer

VAer 756 Reputation points
2021-11-28T01:47:06.173+00:00

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

Attached screenshot: why it does not show integer?

Thanks.

153111-non-integer.jpg

AdapterTD = new OdbcDataAdapter("SELECT * from TableName" ORDER BY CAST(Case_number AS Int)", GlobalVariables.ConnectionString);  
  
                dSetTD = new DataSet();  
                AdapterTD.Fill(dSetTD);  
  
                dgvTD.DataSource = dSetTD.Tables[0];  
C#
C#
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.
10,301 questions
{count} votes

Accepted answer
  1. Jack J Jun 24,296 Reputation points Microsoft Vendor
    2021-11-30T05:33:58.613+00:00

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

     public Form1()  
            {  
                InitializeComponent();  
                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:

    153602-image.png


    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 112.5K Reputation points
    2021-11-28T09:26:49.583+00:00

    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