C# DataGridView 更改类型 重绘

DataGridView 更改类型

需要用到重绘

            DataGridViewTextBoxColumn aa01 = new DataGridViewTextBoxColumn();
            aa00.DataPropertyName = "题目"; //绑定数据源的名称
            aa00.HeaderText = "题目00000";  //显示的名称
            aa00.Name = "题目"; //列的名称
            dataGridView1.Columns.Insert(1, aa01);

//绑定重绘事件

 private void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            try
            {
                int index = dgv.Columns["状态"].Index;  //获取列的索引值
                if (e.ColumnIndex >= index) // ColumnIndex 正在格式化单元格的索引
                {
                    if (e.Value.GetType().Name == "Boolean")
                    {
                        if ((bool)e.Value)
                        {
                            e.Value = "✔";
                        }
                        else
                        {
                            e.Value = string.Empty;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MsgBox.Error(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;

//使用

dgv.CurrentRow  获取选中的行
dgv.Rows[index].Selected = true; 选中指定行 dgv.Rows[index].Cells["工号"].Selected = true; //选中指定行 public DataGridViewRow objs; 保存选中的行 objs.Cells["姓名"].Value.ToString() 获得该行指定列的数据

dgv.CurrentRow 获取选中的行

dgv.Rows[index].Selected = true; 选中指定行

dgv.Rows[index].Cells["工号"].Selected = true; //选中指定行

public DataGridViewRow objs; 保存选中的行

objs.Cells["姓名"].Value.ToString() 获得该行指定列的数据

            //dgv.CurrentCell = dgv.Rows[10].Cells[1];//选中指定行
            //dgv.FirstDisplayedScrollingRowIndex = 10;//设置纵向滚动第一条数据