As mentioned above it seems to be impossible to change the visual appearance of boolean values in a data bound scenario.
Even DataGridViewCellStyle.FormatProvider does not work correctly with types like System.Int32, System.Int64, System.Decima, etc.
Therefore I found a workaround which works for me. Probably it is not the best solution but currently it fits my needs.
I handle the DataGridView.ColumnAdded event and replace DataGridViewCheckBoxColumn with DataGridViewTextBoxColumn. Afterwards I use CellFormating event (recommended by Microsoft, see links above) to format source data.
private DataGridViewTextBoxColumn textBoxColumn = null;
void _dataGrid_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
{
// Avoid recursion
if (e.Column == textBoxColumn) return;
DataGridView gridView = sender as DataGridView;
if (gridView == null) return;
if( e.Column is DataGridViewCheckBoxColumn)
{
textBoxColumn = new DataGridViewTextBoxColumn();
textBoxColumn.Name = e.Column.Name;
textBoxColumn.HeaderText = e.Column.HeaderText;
textBoxColumn.DataPropertyName = e.Column.DataPropertyName;
gridView.Columns.Insert(e.Column.Index, textBoxColumn);
gridView.Columns.Remove(e.Column);
}
}
void _dataGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridViewColumn col = _dataGrid.Columns[e.ColumnIndex];
try
{
if ( col.Name == "IsMale")
{
bool isMale = Convert.ToBoolean(e.Value);
e.Value = isMale ? "male" : "female";
}
}
catch (Exception ex)
{
e.Value = "Unknown";
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…