Visual Studio C#: DataGridView not showing integer

VAer-4038 776 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];  
Developer technologies | C#
Developer technologies | 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.
{count} votes

Answer accepted by question author
  1. Jack J Jun 25,316 Reputation points
    2021-11-30T05:33:58.613+00:00

    @VAer-4038 , 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 125.7K 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

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.