带按钮控件的DataGridView - 删除行

nam*_*msu 5 .net c# datagridview bindinglist winforms

我想在每行的末尾有一个删除按钮,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<Product> lProd = new BindingList<Product>();
    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;
    }
    .....
}
Run Code Online (Sandbox Code Playgroud)

Rez*_*aei 14

DataGridView在行上显示按钮,您应该DataGridViewButtonColumn在网格的列中添加一个按钮.以下是使用按钮列时应该了解的一些常见任务:

  • 将按钮列添加到DataGridView
  • 在按钮上显示图像
  • 设置按钮文本
  • 处理按钮事件

将按钮列添加到DataGridView

要在网格的每一行上显示一个按钮,可以通过DataGridViewButtonColumn编程方式或使用设计器向网格的列添加:

var deleteButton=new DataGridViewButtonColumn();
deleteButton.Name="dataGridViewDeleteButton";
deleteButton.HeaderText="Delete";
deleteButton.Text="Delete";
deleteButton.UseColumnTextForButtonValue=true;
this.dataGridView1.Columns.Add(deleteButton);
Run Code Online (Sandbox Code Playgroud)

在按钮上显示图像

如果您更喜欢在按钮上绘制图像,则应在资源中拥有图像,然后处理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;
    }
}
Run Code Online (Sandbox Code Playgroud)

设置按钮文本

您可以使用以下任一选项:

您可以设置您的Text属性DataGridViewButtonColumn并将其设置UseColumnTextForButtonValuetrue,这样文本将显示在该列的每个单元格上.

deleteButton.Text="Delete";
deleteButton.UseColumnTextForButtonValue=true;
Run Code Online (Sandbox Code Playgroud)

你也可以使用Valuecell的属性:

this.dataGridView1.Rows[1].Cells[0].Value = "Some Text";
Run Code Online (Sandbox Code Playgroud)

另外,作为另一种选择,您可以处理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";
    }
}
Run Code Online (Sandbox Code Playgroud)

处理按钮事件

要点击按钮,您可以处理CellClick或触发网CellContentClick格.两个事件都通过单击和Space按键触发.

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

注意

  • 正如Ivan在评论中所提到的,当你使用时,你BindingList不需要将网格的数据源设置为null,并且每次更改都需要返回绑定列表.它BindingList本身反映了你的变化DataGridView.