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 = "";
                    }
                }

            }
        }
相關文章
相關標籤/搜索