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);
}
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)
{
if (e.ColumnIndex == 0 && e.RowIndex != this.dgvAnalisis.NewRowIndex)
{
double d = double.Parse(e.Value.ToString());
e.Value = d.ToString("0.00##");
}
}