Sai Yang

Sai Yang

  • NA
  • 136
  • 27.4k

How to display only 2 digits after decima?

Nov 11 2018 12:46 AM
Hello freinds.
 
I browse a score file excel to C# datagridview. The CGPA column is decimal column in excel file and it is formatted as Number and 2 digits after decimal, but when I browse it to Datagridview that column contains with more than 2 digits after decimal.
 
My Datagridview has not added any column, Means it will display the header columns with the select command.
 
this is my code in Browse Button:
 
           
            private void btnbrowsefile_Click(object sender, EventArgs e)
              {
                  OpenFileDialog openfill = new OpenFileDialog();
                   openfill.Filter = "Excel Files | *.xlsx;*.xls;*.xlsm";
                    if (openfill.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                       {
                               this.txtchoose.Text = openfill.FileName;
                        }
                   OpenFileDialog openfile = new OpenFileDialog();
                      string stringconn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + txtchoose.Text + "; Extended Properties=\"Excel 12.0;HDR=Yes;\";";
                    OleDbConnection con = new OleDbConnection(stringconn);
                     if (openfill.FileName == "")
                      {
                             MessageBox.Show("??????????????? ....", "????????????????......", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                             return;
                          }
                         else
                           {
                           con.Open();
                              cmbSheet.DataSource = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                          cmbSheet.DisplayMember = "TABLE_NAME";
                             cmbSheet.ValueMember = "TABLE_NAME";
                             }
                              }
 
 
 
The Sheet Members are displayed to the ComboBox named @cmbSheet. 
After the Sheet selectcomitted, code as below:
 
 
                                    string stringconn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + txtchoose.Text + "; Extended Properties=\"Excel 12.0;HDR=Yes;\";";
                                     OleDbConnection con = new OleDbConnection(stringconn);
                                       OleDbDataAdapter sda = new OleDbDataAdapter("Select * from[" + cmbSheet.Text + "] ", con);
                                    DataTable dt = new DataTable();
                                    sda.Fill(dt);
                                       dgvscore.DataSource = dt;
 
the Excel file has 2 Sheet Named(@January,@March) Score in 2 months, Each month has the same columns and it has 10 columns.
 
     first Col is StudentID, Nexts are the Subjects Name and last two columns are Total Score and CGPA columns. The CGPA is formatted as 2 digits after decimal in Excel file but when I browse to Datagridview is more than 2 digits after decimal.
 
 
Any body can tell me how to solve this isue?
 
thank you. 
 
 

Answers (1)