方法一:
1,可以使用 DataGridView提供了一种列的类型,叫 DataGridViewButtonColumn,可以展示出一个按钮,虽然在UI界面中是一个button 的效果,但是并不是一个按钮,只是渲染出来的一个样式。所以并不能像button 一样去使用。
2,这里可以设置样式,看下效果把!
3,如果想要点击删除按钮可以触发事件的话,则需要设置 DataGridVIew 的事件 CellContentClick
private void uiDataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (uiDataGridView1.Columns[e.ColumnIndex].Name == "btnDel" && e.RowIndex >= 0)
{
MessageBox.Show("删除");
}
}
方法二
1,首先新增列类型 DataGridViewTextBoxColumn
2,添加代码
private void Form1_Load(object sender, EventArgs e)
{
int index = this.uiDataGridView1.Rows.Add();
this.uiDataGridView1.Rows[index].Cells[0].Value = "0";
this.uiDataGridView1.Rows[index].Cells[1].Value = "张之琅";
this.uiDataGridView1.Rows[index].Cells[2].Value = "男";
this.uiDataGridView1.Rows[index].Cells[3].Value = "18";
this.uiDataGridView1.Rows[index].Cells[4].Value = "勤奋";
Button btn = new Button();
btn.Text = "删除";
btn.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(80)))), ((int)(((byte)(160)))), ((int)(((byte)(255)))));
btn.Font = new System.Drawing.Font("微软雅黑", 8F);
this.uiDataGridView1.Controls.Add(btn);
Rectangle rect = this.uiDataGridView1.GetCellDisplayRectangle(5, index, false);
btn.Location = new Point(rect.Left, rect.Top);
btn.Click += new EventHandler(btnDel_Click);
}
void btnDel_Click(object sender, EventArgs e)
{
if (uiDataGridView1.SelectedRows.Count < 1)
return;
uiDataGridView1.Rows.Remove(this.uiDataGridView1.SelectedRows[0]);
}
最后看下效果把!