当前位置:首页 > Windows程序 > 正文

DataGridView数据绑定后单元格格式化

2021-03-28 Windows程序

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);     } }


DataGridView数据绑定后单元格格式化

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/68772.html