Format of DataGridView value
I have a DataGridView which is filling from Table in SQL Server Database. One of it's columns is price
.
Type of column in DGV is automatically sets as Decimal.
In some cases I need to write in cells of this column text like "none", instead of price.
How can I do it?
DGV.Item("price", 0).Value = "none"
doesn't work, because of decimal type 开发者_如何转开发of a cell.
instead of all this,Use datacolumn expression property.
If you just want to change how the data is displayed (and that's what it sounds like you want), hook the CellFormatting event. Here's the general idea (code untested):
private void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (some_condition(e.ColumnIndex, e.RowIndex)) {
e.Value = "none";
e.FormattingApplied = true;
}
}
Look at implementing IFormatProvider & ICustomFormatter...
Then in DGV you can assign it to the column format via ColumnFormatProvider.
精彩评论