DataGridView数据绑定后,经常需要对数据进行某种格式转换,比如说 1表示男性,2表示女性。
这时,需要用到CellFormatting事件
下面是微软官方的示例
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{ // If the column is the Artist column, check the
// value.
if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Artist")
{ if (e.Value != null)
{ // Check for the string "pink" in the cell.
string stringValue = (string)e.Value;
stringValue = stringValue.ToLower(); if ((stringValue.IndexOf("pink") > -1))
{
e.CellStyle.BackColor = Color.Pink;
}
}
} else if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Release Date")
{
ShortFormDateFormat(e);
}
}