hey guys
i have a DGV which has a check box column. what i want to do is that when user checks one of these check boxes the entire row be highlighted as if a normal cell was clicked, here is the tricky part, and i want when user clicks on another check box on another row the same thing as before happens but the first row wont go unselected (unhighlighted). and the opposite happens when user unckecks a check box

Here you go:

public Form1()
        {
            InitializeComponent();
            //adding some example columns:
            //1. column is checkBox, rest of 2 are some usual column:

            DataGridViewCheckBoxColumn checkColumn = CreateCheckBoxColumn();
            dataGridView1.Columns.Add(checkColumn);
            dataGridView1.Columns.Add("col2", "Column 2");
            dataGridView1.Columns.Add("col3", "Column 3");

            //adding some example rows:
            for (int i = 1; i <= 10; i++)
            {
                dataGridView1.Rows.Add(false, "item A" + i, "item B" + i);
            }

            //create an event:
            dataGridView1.CurrentCellDirtyStateChanged += new EventHandler(dataGridView1_CurrentCellDirtyStateChanged);
            dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
        }

        private DataGridViewCheckBoxColumn CreateCheckBoxColumn()
        {
            DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
            {
                column.Name = "checkColumn";
                column.HeaderText = "Select rows";
                column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
                column.ThreeState = false;
            }
            return column;
        }

        void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (dataGridView1.IsCurrentCellDirty)
                dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
        }

        private void dataGridView1_CellValueChanged(object obj, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == dataGridView1.Columns[0].Index) //compare to checkBox column index
            {
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    if (!row.IsNewRow)
                    {
                        DataGridViewCheckBoxCell checkBox = dataGridView1[0, row.Index] as DataGridViewCheckBoxCell;
                        if (checkBox != null)
                        {
                            if ((bool)checkBox.Value == true)
                                dataGridView1.Rows[row.Index].Selected = true;
                            else
                                dataGridView1.Rows[row.Index].Selected = false;
                        }
                    }
                }
            }
        }
commented: Nice +14

thanks for the code, i created a winapp and put your code in it and it works perfect, but when i put the code in my main app it gives me an error on line 53 ( the code above), the error is:
"NullReferenceException was unhandled
Object reference not set to an instance of an object."
i really dont know what possibly is the problem here, i only renamed the DGV name and i already had a check box column which i had made in designer so i used that column instead of the one you created in runtime.

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.