Hi everybody,
I'm working with Csharp .Net 2.0, I have a grid view that I need to customize format for each cell for many field depending on the currency:
code
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 2 & e.RowIndex<3)
{
e.CellStyle.FormatProvider = CultureInfo.CreateSpecificCulture(dataGridView1[1, e.RowIndex].Value.ToString());
e.CellStyle.Format = "C";
}
}
But modification is not being commited due to the error occuring when editing on all cells that have different culture than the default set in regional settings.
What to do???
Loading
Hemant KumarPosted Jan 16, 2012, 5:16 AM
If your column type is of string type which is by default, you shoud handle the cellFormating event to do a trick work like below:
private void DgvCellStyle_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("a");
dt.Rows.Add(155.6565);
dt.Rows.Add(2342.2);
this.dataGridView1.DataSource = dt;
this.dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
}
void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 0 && e.RowIndex != this.dataGridView1.NewRowIndex)
{
double d = double.Parse(e.Value.ToString());
e.Value = d.ToString("N2");
}
}