Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have problem in my datagridview's column/cell format, i have cell ["Close"] that contain float data, then i will minus data in cell["Close"] and put the result in cell ["Up"], but my result not in decimal, i have try every way, like: "N2", DefaultStyleFormat, CellFormatting, there's no one work for me.

What I have tried:

Let me show my code:


<pre lang="c#"><pre>private void FrmAnalisis_Load(object sender, EventArgs e)
        {
            conn = new SqlConnection("Server=localhost;Data Source=localhost; Database=dbSaham;Integrated Security=SSPI");
            conn.Open();
        }

        private void btnMulaiAnalisis_Click(object sender, EventArgs e)
        {
            string dt1 = dtpMulai.Value.Date.ToString();
            string dt2 = dtpAkhir.Value.Date.ToString();
            SqlCommand cmd = new SqlCommand("SELECT * From UNICJKtutup Where Date BETWEEN @date AND @date2", conn);
            cmd.Parameters.AddWithValue("@date", dtpMulai.Value.Date);
            cmd.Parameters.AddWithValue("@date2", dtpAkhir.Value.Date);
            if (dtpMulai.Value.Date != dtpAkhir.Value.Date)
            {
                ds = new DataSet();
                da = new SqlDataAdapter(cmd);
                da.Fill(ds, "UNICJKtutup");
                dgvAnalisis.ReadOnly = true;
                dgvAnalisis.AllowUserToAddRows = false;
                dgvAnalisis.AllowUserToDeleteRows = false;
                dgvAnalisis.MultiSelect = true;
                dgvAnalisis.DataSource = ds.Tables["UNICJKtutup"];
                this.dgvAnalisis.CellFormatting += new DataGridViewCellFormattingEventHandler(dgvAnalisis_CellFormatting);//this i MARK the event
            }
            else
                MessageBox.Show("Tanggal Unvalid\nHarap pilih tanggal yang berbeda", "Informasi");

            if (dgvAnalisis.DataSource == null) return;
            var rowLen = dgvAnalisis.Rows.Count;

            foreach (DataGridViewRow dr in dgvAnalisis.Rows)
            {
                if (dr.Index + 1 == rowLen) break;
                var nextRow = dgvAnalisis.Rows[dr.Index + 1];
                string col1 = dr.Cells["Close"].Value.ToString();
                string col2 = nextRow.Cells["Close"].Value.ToString();
                float val1 = 0, val2 = 0;
                if (float.TryParse(col1, out val1) && float.TryParse(col2, out val2))
                {
                    if (val1 < val2)
                    {
                        nextRow.Cells["Up"].Value = val2 - val1;
                        nextRow.Cells["Down"].Value = 0;
                    }
                    else if (val1 > val2)
                    {
                        nextRow.Cells["Down"].Value = val1 - val2;
                        nextRow.Cells["Up"].Value = 0;
                    }
                    else
                    {
                        nextRow.Cells["Up"].Value = 0;
                        nextRow.Cells["Down"].Value = 0;
                    }
                }
            }
        }

        private void dgvAnalisis_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) //this too the event that didn't work
        {
            if (e.ColumnIndex == 0 && e.RowIndex != this.dgvAnalisis.NewRowIndex)
            {
                double d = double.Parse(e.Value.ToString());
                e.Value = d.ToString("0.00##");
            }
        }
Posted
Updated 27-Feb-17 12:54pm

1 solution

Google is your friend: format datagridview column - Google[^].

2 second search (see above) turned up this gem as the fist result: How to: Format Data in the Windows Forms DataGridView Control[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900