winform中dataGridView单元格根据值设置新值,完全解决绑定后数据类型转换的困难

winform中dataGridView单元格在数据绑定后,数据类型更改困难,只能迂回实现。有时候须要将数字变换为不一样的文字描述,就会出现int32到string类型转换的异常,借助CellFormatting事件就能够轻松解决了。javascript

 

在dataGridView添加CellFormatting事件,如:dataGridView1_CellFormattinghtml

 

参考代码:java

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 3)
            {
                e.FormattingApplied = true;

                if (dataGridView1.Rows[e.RowIndex] != null && !dataGridView1.Rows[e.RowIndex].IsNewRow)
                {
                    if (e.Value.ToString() == "0")
                    {
                        e.Value = "保存";
                    }
                    else if (e.Value.ToString() == "1")
                    {
                        e.Value = "提交";
                    }
                    else
                    {
                        e.Value = "";
                    }
                }

            }
        }
相关文章
相关标签/搜索