1.塞入CheckBox:
1.1 在Form_Load裡面:
int iColumnCount = dataGridView1.ColumnCount;
if (iColumnCount == 0)
{
DataGridViewCheckBoxColumn col = new DataGridViewCheckBoxColumn()
{
Name = "選擇",
FalseValue = 0,
TrueValue = 1,
Visible = true
};
dataGridView1.Columns.Add(col);
}
1.2 檢視有被勾選的項目:
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataGridViewCheckBoxCell chk = row.Cells[0] as DataGridViewCheckBoxCell;
if(Convert.ToBoolean(chk.Value))
{
string data= row.Cells[1].Value.ToString() ;//取出第1欄位資料
}
}
2.塞入Button:
2.1 在Form_Load裡面:
int iColumnCount = dataGridView1.ColumnCount;
if (iColumnCount == 0)
{
DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
dataGridView1.Columns.Add(btn);
btn.Name = "btn";
btn.HeaderText = "標題";
btn.Text = "執行";
btn.UseColumnTextForButtonValue = true;
}
2.2 執行被按下的Button:
在dataGridView1_CellClick裡面,
if (e.ColumnIndex == 0)
{
... do something...
.
}