1. 程式人生 > >C# DataGridView手動新增資料設定CheckBox預設選中

C# DataGridView手動新增資料設定CheckBox預設選中

private void TestForm_Load(object sender, EventArgs e)
{                      
    this.dataGridView1.Columns.Insert(0, new DataGridViewCheckBoxColumn() { HeaderText = "選擇", Name = "select" });
    this.dataGridView1.Columns.Insert(1, new DataGridViewTextBoxColumn() { HeaderText = "資料", Name = "Data"});
    this.dataGridView1.Columns.Insert(2, new DataGridViewButtonColumn() { HeaderText = "+", Text = "btn", Name = "btn"});
}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if(this.dataGridView1.Columns[e.ColumnIndex].Name == "btn")
    {
        DataGridViewRow row = new DataGridViewRow()
        {
            Cells = { new DataGridViewCheckBoxCell(){ Value = true } , new DataGridViewTextBoxCell(){ Value = "test"} , new DataGridViewButtonCell(){ Value = "+"}},
        };
        this.dataGridView1.Rows.Add(row);
    }
}