Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I have added checkbox dynamically and I am enable to select full rows by checked the checkbox but, I couldn't deselect rows by unchecked the checkbox in DataGridView.

And, I have a button, where I want to show message only selected checkbox.

My Code for Dynamically adding Checkbox in DataGridView:
C#
DataGridViewCheckBoxColumn checkColumn = new DataGridViewCheckBoxColumn();
             
            this.dtgrid_event_list.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

            checkColumn.Name = "Check";
            checkColumn.HeaderText = "Check";
            checkColumn.Width = 50;
            checkColumn.FillWeight = 50;
            this.dtgrid_event_list.Columns.Add(checkColumn);
            this.dtgrid_event_list.AllowUserToAddRows = false;
            this.dtgrid_event_list.AllowUserToDeleteRows = false;
            
            this.dtgrid_event_list.DataSource = db.Adapter("select device, event_name, frame, frame_rate, event_duration from alb_events");


My Selection Row Code:
C#
private void dtgrid_event_list_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex != -1)
            {
                string point = "";
                Color oldColor = this.dtgrid_event_list.Rows[e.RowIndex].DefaultCellStyle.SelectionBackColor;
                this.dtgrid_event_list.Rows[e.RowIndex].DefaultCellStyle.SelectionBackColor = Color.FromArgb(oldColor.R < 235 ? oldColor.R + 20 : 0,
                                    oldColor.G, oldColor.B);
                foreach (DataGridViewRow drv in this.dtgrid_event_list.Rows)
                {
                    DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)drv.Cells[0];
                    

                    foreach (DataGridViewCell cell in drv.Cells)
                    {
                        if (drv.Index == e.RowIndex)
                        {
                            if (chk.Value == chk.TrueValue)
                            {
                                cell.Style.BackColor = Color.BlueViolet;
                               
                            }
                                
                            if (cell.ColumnIndex == 4)
                            {
                                if(cell.Value.ToString().Length > 2)
                                    point = cell.Value.ToString().Substring(2, 1).ToString();
                                
                            }
                            if (point == ".")
                                cell.Style.BackColor = Color.Red;

                           
                                

                        }

                    }
                }
            }
        }


Now, Please help me to deselect row by unchecked Checkbox and Show Only selected Rows by clicking in Button.

What I have tried:

private void btnAddEvent_Click(object sender, EventArgs e)
{
int count = 1;
foreach (DataGridViewRow drv in this.dtgrid_event_list.Rows)
{
DataGridViewCheckBoxCell chkchecking = new DataGridViewCheckBoxCell();
chkchecking= (DataGridViewCheckBoxCell)drv.Cells[0];
MessageBox.Show(chkchecking.Value.ToString());

if ((bool)chkchecking.Value == true)
{
MessageBox.Show(count.ToString());
}

count = count + 1;
}
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900