DataGridView with control button - Delete row

I want the "Delete" button at the end of each row DataGridViewand clicking on it, I want to remove the desired row from the binding list, which is the data source of my grid.

But I can’t do it. I created a button object in the product class and created it with a unique identifier to remove this object from the list. but the button does not appear on the line.

Screenshot

The form has text fields, and users can enter text, and when they click the Add button, a new product object is created with the fields provided, and then it is added to BindingList.

Finally, this list is tied to DataGridView, and the details are displayed in the grid. (I did this part).

and finally, by clicking the save button, the list is saved in the database.

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;
    }
    .....
}
+5
1

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);

, , 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;
    }
}

:

Text DataGridViewButtonColumn, 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 . 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);
    }
}

  • , BindingList, grid . BindingList DataGridView.
+13

Source: https://habr.com/ru/post/1614606/


All Articles