2

I would like to know how to convert values into two decimals when I load them into DataGridView and TextBoxes from the sqlserver two tables.This is my code.I tried txtgrandTotal.Text = Total.ToString("#,0.00") code,when I insert data into database

 public void loadDataToUpdate()
    {

        DynamicConnection con = new DynamicConnection();

        try
        {
            con.sqlquery("SELECT p.PO_No,p.Supplier_ID,p.Date,p.RequiredDate,p.GrandTotal,b.BookName,c.ISBN_No,c.OrderQuantity,c.UnitPrice,c.Total FROM  TBL_PO_Cart AS c INNER JOIN TBL_Book AS b ON c.ISBN_No = b.ISBN_No INNER JOIN TBL_PO AS p ON c.PO_No=p.PO_No where p.PO_No='" + cmbPO.Text + "'");
            con.mysqlconnection();
            con.datatable();
            SqlDataAdapter da = new SqlDataAdapter(con.cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                txtPONo.Text = ds.Tables[0].Rows[i][0].ToString();
                cmbsupID.Text = ds.Tables[0].Rows[i][1].ToString();
                date1.Text = ds.Tables[0].Rows[i][2].ToString();
                requireddate.Text = ds.Tables[0].Rows[i][3].ToString();
                txtgrandTotal.Text = (ds.Tables[0].Rows[i][4].ToString());
            }
            con.dataread();
            this.dataGridView1.Rows.Clear();
            while (con.datareader.Read())
            {
                DataGridViewRow row = (DataGridViewRow)dataGridView1.Rows[0].Clone();
                row.Cells[0].Value = con.datareader["BookName"].ToString();
                row.Cells[1].Value = con.datareader["ISBN_No"].ToString();
                row.Cells[2].Value = con.datareader["OrderQuantity"].ToString();
                row.Cells[3].Value = con.datareader["UnitPrice"].ToString();
                row.Cells[4].Value = con.datareader["Total"].ToString();
                dataGridView1.Rows.Add(row);
            }
        }

So I need to display txtgrandTotal textbox, datagridview column3 and column4 with 2 decimal places.Please help me

Kith
  • 117
  • 3
  • 17
  • `I tried txtgrandTotal.Text = Total.ToString("#,0.00") code` what is the issue with this? – Chetan Jun 02 '18 at 06:27
  • Possible duplicate of [How do I round a decimal value to 2 decimal places (for output on a page)](https://stackoverflow.com/questions/164926/how-do-i-round-a-decimal-value-to-2-decimal-places-for-output-on-a-page) – Chetan Jun 02 '18 at 06:32
  • I don't know how to use this for in here for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { txtPONo.Text = ds.Tables[0].Rows[i][0].ToString(); cmbsupID.Text = ds.Tables[0].Rows[i][1].ToString(); date1.Text = ds.Tables[0].Rows[i][2].ToString(); requireddate.Text = ds.Tables[0].Rows[i][3].ToString(); txtgrandTotal.Text = (ds.Tables[0].Rows[i][4].ToString()); } – Kith Jun 02 '18 at 06:33
  • Tried `con.datareader["Total"].ToString("#,0.00");`? – Chetan Jun 02 '18 at 06:35
  • yes I tried but an error occurred in to string word.It said "No overload method 'ToString' takes 1 arguments – Kith Jun 02 '18 at 06:42
  • Is it web application or windows application? For WindowForm datagridview you can set the format for the column using approach described at https://stackoverflow.com/questions/18299040/number-format-for-datagridview-in-c-sharp – Chetan Jun 02 '18 at 07:10
  • Yes.Thank you.I used " row.Cells[4].Value = string.Format("{0:0.00}", (decimal)con.datareader["Total"]);" for convert it.Now it is work – Kith Jun 02 '18 at 09:16

0 Answers0