In this example, we will learn How To Delete the rows in the datagridview using checkbox items that are in datagridview rows in C# Windows Form Application.
Final Preview
Form Design:
How to Remove DataGridView Checked Rows Cell Using C# ?
Source Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace delete_selected_rows { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.Text = "csharp-console-examples.com"; this.BackColor = Color.IndianRed; DataGridViewTextBoxColumn dgvId = new DataGridViewTextBoxColumn(); dgvId.HeaderText = "Id"; DataGridViewTextBoxColumn dgvFn = new DataGridViewTextBoxColumn(); dgvFn.HeaderText = "First Name"; DataGridViewTextBoxColumn dgvLn = new DataGridViewTextBoxColumn(); dgvLn.HeaderText = "Last Name"; DataGridViewCheckBoxColumn dgvCheckBox = new DataGridViewCheckBoxColumn(); dgvCheckBox.HeaderText = "Select"; dataGridView1.Columns.Add(dgvId); dataGridView1.Columns.Add(dgvFn); dataGridView1.Columns.Add(dgvLn); dataGridView1.Columns.Add(dgvCheckBox); dataGridView1.Rows.Add("1", "First Name 1", "Last Name 1", false); dataGridView1.Rows.Add("2", "First Name 2", "Last Name 2", false); dataGridView1.Rows.Add("3", "First Name 3", "Last Name 3", false); dataGridView1.Rows.Add("4", "First Name 4", "Last Name 4", false); dataGridView1.Rows.Add("5", "First Name 5", "Last Name 5", false); dataGridView1.Rows.Add("6", "First Name 6", "Last Name 6", false); dataGridView1.Rows.Add("7", "First Name 7", "Last Name 7", false); dataGridView1.Rows.Add("8", "First Name 8", "Last Name 8", false); dataGridView1.Rows.Add("9", "First Name 9", "Last Name 9", false); dataGridView1.Rows.Add("10", "First Name 10", "Last Name 10", false); dataGridView1.Rows.Add("11", "First Name 11", "Last Name 11", false); dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; dataGridView1.AllowUserToAddRows = false; } private void button1_Click(object sender, EventArgs e) { for(int i=dataGridView1.RowCount-1;i>=0;i--) { bool chk = (bool)dataGridView1.Rows[i].Cells[3].Value; if(chk==true) { DataGridViewRow drow = dataGridView1.Rows[i]; dataGridView1.Rows.Remove(drow); } } } } } |