带按钮控件的DataGridView – 删除行
我想在DataGridView
的每一行的末尾有一个删除按钮,然后单击我要从绑定列表中删除所需的行,该列是我的网格的数据源。
但我似乎无法做到这一点我在产品类中创建了一个按钮对象,并使用唯一的id实例化它以从列表中删除该对象。 但按钮没有显示在行中。
表单中有TextBox,用户可以输入文本,当他们按下Add按钮时,产品的新对象将使用提供的字段进行实例化,然后将其添加到BindingList
。
最后,此列表绑定到DataGridView
并在网格中显示详细信息。 (我已经完成了这一部分)。
最后,通过单击“保存”按钮,列表将保存在数据库中。
public class Product{ public string Brand { get; set; } public int ProductPrice { get; set; } public int Quantity { get; set; } public product(string brand,int productPrice, int quantity){ this.Brand = brand; this.ProductPrice = productPrice; this.Quantity = quantity; } } public partial class MainForm: Form{ ..... BindingList lProd = new BindingList(); private void btnAddProduct_Click(object sender, EventArgs e){ string Brand = txtProBrand.Text; int Price = Convert.ToInt32(txtPrice.Text); int Quantity = Convert.ToInt32(txtQuantity.Text); Product pro = new Product(Brand, Price, Quantity); lProd.Add(pro); dataGridView1.DataSource = null; dataGridView1.DataSource = lProd; } ..... }
要在DataGridView
行上显示按钮,您应该将DataGridViewButtonColumn
添加到DataGridViewButtonColumn
的列中。 以下是使用按钮列时应该了解的一些常见任务:
- 将Button列添加到DataGridView
- 在按钮上显示图像
- 设置按钮文本
- 处理按钮事件
将Button列添加到DataGridView
要在网格的每一行上显示一个按钮,可以通过编程方式或使用设计器将DataGridViewButtonColumn
添加到DataGridViewButtonColumn
的列中:
var deleteButton=new DataGridViewButtonColumn(); deleteButton.Name="dataGridViewDeleteButton"; deleteButton.HeaderText="Delete"; deleteButton.Text="Delete"; deleteButton.UseColumnTextForButtonValue=true; this.dataGridView1.Columns.Add(deleteButton);
在按钮上显示图像
如果您更喜欢在按钮上绘制图像,则应在资源中拥有图像,然后处理网格的CellPainting
事件:
void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (e.RowIndex == dataGridView1.NewRowIndex || e.RowIndex < 0) return; if (e.ColumnIndex == dataGridView1.Columns["dataGridViewDeleteButton"].Index) { var image = Properties.Resources.DeleteImage; //An image e.Paint(e.CellBounds, DataGridViewPaintParts.All); var x = e.CellBounds.Left + (e.CellBounds.Width - image.Width) / 2; var y = e.CellBounds.Top + (e.CellBounds.Height - image.Height) / 2; e.Graphics.DrawImage(image, new Point(x, y)); e.Handled = true; } }
设置按钮文本
您可以使用以下任一选项:
您可以设置DataGridViewButtonColumn
Text
属性,并将其UseColumnTextForButtonValue
设置为true
,这样文本将显示在该列的每个单元格上。
deleteButton.Text="Delete"; deleteButton.UseColumnTextForButtonValue=true;
您还可以使用单元格的Value
属性:
this.dataGridView1.Rows[1].Cells[0].Value = "Some Text";
另外,作为另一种选择,您可以处理网格的CellFormatting
事件。 当您想要为按钮设置不同的文本时,这种方式可能很有用。
void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { //If this is header row or new row, do nothing if (e.RowIndex < 0 || e.RowIndex == this.dataGridView1.NewRowIndex) return; //If formatting your desired column, set the value if (e.ColumnIndex=this.dataGridView1.Columns["dataGridViewDeleteButton"].Index) { e.Value = "Delete"; } }
处理按钮事件
要点击按钮,您可以处理CellClick
或CellContentClick
事件。 这两个事件都是通过单击和按空格键来触发的。
void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e) { //if click is on new row or header row if( e.RowIndex == dataGridView1.NewRowIndex || e.RowIndex < 0) return; //Check if click is on specific column if( e.ColumnIndex == dataGridView1.Columns["dataGridViewDeleteButton"].Index) { //Put some logic here, for example to remove row from your binding list. yourBindingList.RemoveAt(e.RowIndex); } }
注意
- 正如Ivan在评论中所提到的,当您使用
BindingList
时,您不需要将网格的数据源设置为null并且每次更改都返回到绑定列表。BindingList
本身反映了对DataGridView
更改。