Hi all, I want to create code to delete row in datagridview based on checkBox selection. I've managed added the checkBox Button using code below:

private void FormTampil_Load(object sender, EventArgs e)
        {
            DataGridViewCheckBoxColumn checkboxColumn = new DataGridViewCheckBoxColumn();
            checkboxColumn.Width = 30;
            checkboxColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dataGridView1.Columns.Insert(0, checkboxColumn);

            // add checkbox header
            Rectangle rect = dataGridView1.GetCellDisplayRectangle(0, -1, true);
            // set checkbox header to center of header cell. +1 pixel to position correctly.
            rect.X = rect.Location.X + (rect.Width / 4);

            CheckBox checkboxHeader = new CheckBox();
            checkboxHeader.Name = "checkboxHeader";
            checkboxHeader.Size = new Size(18, 18);
            checkboxHeader.Location = rect.Location;
            checkboxHeader.CheckedChanged += new EventHandler(checkboxHeader_CheckedChanged);

            dataGridView1.Controls.Add(checkboxHeader);

        }

        private void checkboxHeader_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                dataGridView1[0, i].Value = ((CheckBox)dataGridView1.Controls.Find("checkboxHeader", true)[0]).Checked;
            }
            dataGridView1.EndEdit();
        }

But I'm still confuse how to declare the code in "Delete" button, so if I select one or all the rows, the data selected will be deleted from datagridview also from database. I want to delete the data based no_simpan as primary key.

private void btnDelete_Click(object sender, EventArgs e)
{
        //what code should I create here?
}

Pls someone help me...
Thanks in advance..

Hi, have you managed to salve the problem yet?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.