DataGridView 更改类型
需要用到重绘
DataGridViewTextBoxColumn aa01 = new DataGridViewTextBoxColumn(); aa00.DataPropertyName = "题目"; //绑定数据源的名称 aa00.HeaderText = "题目00000"; //显示的名称 aa00.Name = "题目"; //列的名称 dataGridView1.Columns.Insert(1, aa01);
//绑定重绘事件
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { try { int index = dataGridView1.Columns["题目"].Index; //获取列的索引值 if (e.ColumnIndex == index) // ColumnIndex 正在格式化单元格的索引 { //类型名称 DataGridViewCellFormattingEventArgs //全名System.Windows.Forms.DataGridViewCellFormattingEventArgs var t = e.GetType(); //e.DesiredType //只读 //e.Value //可读可写 e.Value = "✔"; } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
数据源
DataTable dt = new DataTable(); dt.Columns.Add("科目名称", typeof(string)); dt.Columns.Add("题目", typeof(bool)); DataRow dr = dt.NewRow(); dr["科目名称"] = "000"; dr["题目"] = true; dt.Rows.Add(dr); dataGridView1.DataSource = dt;