I have a problem with a readonly C# Winform DataGridView.
I have a DataSource
for a DataTable
and assign it to DataGridView1.DataSource
. I want to display cell text by cell value without changing the DataSource
.
Ex:
cell value=1 => cell display text="one",
cell value=2 => cell display text="two"
I want that, if I get:
DataGridView1.Rows[rowIndex].Cells[columnIndex].Value
Then it must be 1
(or 2
, or 3
) not "one" (or "two", or "three").
You can use CellFormatting event handler.
private void DataGridView1_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
if (dgv.Columns[e.ColumnIndex].Name == "TargetColumnName" &&
e.RowIndex >= 0 &&
dgv["TargetColumnName", e.RowIndex].Value is int)
{
switch ((int)dgv["TargetColumnName", e.RowIndex].Value)
{
case 1:
e.Value = "one";
e.FormattingApplied = true;
break;
case 2:
e.Value = "two";
e.FormattingApplied = true;
break;
}
}
}
my solutions is to put the value in DataGridViewCell.Tag property.
Like this :
DataGridView1.Rows[rowIndex].Cells[columnIndex].Tag = 1;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With