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



        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;

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..

Edited by Dani: Formatting fixed

5 Years
Discussion Span
Last Post by Mitja Bonca
This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.