lundi 17 juillet 2017

DataGridView C# windows application(First row not selecting which is checkbox if mouse click on column Header)

Can please any one help me on this. I have developed a c# windows application which has DataGridView first column has checkboxes. if I click on first column header it selects all the row level check boxes except the first row. For selecting all row level check boxes I have an event of dataGridView1_ColumnHeaderMouseClick and the code is:

private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            foreach (DataGridViewColumn column in dataGridView1.Columns)
            {
                column.SortMode = DataGridViewColumnSortMode.NotSortable;
            }
            if (e.ColumnIndex == 0)
            {
                if (chek == 0)
                {
                    try
                    {
                        for (int i = 0; i < dataGridView1.RowCount; i++)
                        {
                            string paymentValue = dataGridView1.Rows[i].Cells[18].Value.ToString();
                            string incmngp = dataGridView1.Rows[i].Cells[20].Value.ToString();
                            if (paymentValue == "N" && incmngp =="")
                            {
                                dataGridView1.Rows[i].Cells[0].Value = 1;
                                chek = 1;
                            }
                        }
                        if (chek == 1)
                        {
                            btn_update.Text = "Update";
                        }
                    }
                    catch (Exception ) {  }
                }
                else if(chek==1)
                {
                    try
                    {
                        for (int i = 0; i < dataGridView1.RowCount; i++)
                        {
                            dataGridView1.Rows[i].Cells[0].Value = 0;
                            chek = 0;
                        }
                        if (chek == 0)
                        {
                            btn_update.Text = "OK";
                        }
                    }
                    catch (Exception) { }
                }
            }

Note: chek is the variable declared on initialize stage




Aucun commentaire:

Enregistrer un commentaire