/// <summary>
/// 为dataGridView添加“启动”“停止”两个按钮
/// </summary>
private void AddBtndgv2()
{
DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
btn.Name = "BeginBtn";//添加按钮的名字
btn.HeaderText = "启动";//添加按钮列的列名称
btn.DefaultCellStyle.NullValue = "启动";//添加按钮显示的名字
dataGridView2.Columns.Add(btn);//在dataGridView2的最后一列添加按钮
DataGridViewButtonColumn endbtn = new DataGridViewButtonColumn();
endbtn.Name = "EndBtn";
endbtn.HeaderText = "停止";
endbtn.DefaultCellStyle.NullValue = "停止";
dataGridView2.Columns.Insert(11, endbtn);//在dataGridView2的指定列添加按钮
}
/// <summary>
/// 为datagridview中添加的按钮添加点击事件
/// 在dataGridView2的CellContentClick事件下添加
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//点击button按钮事件
if (dataGridView2.Columns[e.ColumnIndex].Name == "BeginBtn" && e.RowIndex >= 0)
{
//说明点击的列是DataGridViewButtonColumn列
DataGridViewColumn column = dataGridView2.Columns[e.ColumnIndex];
MessageBox.Show("点击了启动按钮!");
}
//点击button按钮事件
if (dataGridView2.Columns[e.ColumnIndex].Name == "EndBtn" && e.RowIndex >= 0)
{
//说明点击的列是DataGridViewButtonColumn列
DataGridViewColumn column = dataGridView2.Columns[e.ColumnIndex];
MessageBox.Show("点击了停止按钮!");
}
}