简体   繁体   中英

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 ac# 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

Set your Selection mode property of data grid view to ColumnHeaderSelect

在此处输入图像描述

and make sure all your 'Text' columns have SortMode set to NotSortable

在此处输入图像描述

UPDATE 2

In which case, Undo everything I ever told before and do that like this

Before you are assigning any DataTable to dataGridView1.

da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
dataGridView1.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
     foreach(DataGridViewColumn dc in dataGridView1.Columns)
                {
                    dc.SortMode = DataGridViewColumnSortMode.NotSortable;
                }
 dataGridView1.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;

UPDATE 3

Add an event handler for your dataGridView1's ColumnHeaderMouseClick Event like below

在此处输入图像描述

Add the below code (Generic code if you want to use the same functionality for any column of check boxes)

 private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            //Enter your own column index here
            if(e.ColumnIndex == 0)
            foreach(DataGridViewRow row in dataGridView1.Rows)
            foreach (DataGridViewCell cell in row.Cells)
            {
                    //Check if the cell type is of a CheckBoxCell
                if (cell.GetType() == typeof(DataGridViewCheckBoxCell))
                {
                    DataGridViewCheckBoxCell c = (DataGridViewCheckBoxCell)cell;
                    c.TrueValue = "T";
                    c.FalseValue = "F";
                    if (c.Value == c.FalseValue|| c.Value == null )
                    c.Value = c.TrueValue;
                    else
                        c.Value = c.FalseValue;
                }
            }
            dataGridView1.RefreshEdit();
        }

This is a very bizarre bug in Winforms. The problem more generally applies not to the first row, but to the first selected cell in any row of DataGridViewCheckBoxCell(s). You can select the CheckBox cell by clicking on the check box, or select the cell outside the check box, the behavior is the same. If you select 3 check boxes in the middle of your grid, the first of those three will freeze and not update properly. If you try to clear the selection in code, with a dataGridView1.ClearSelection() method call, it still does not work.

The correct answer is to call datagridview1.RefreshEdit() right after you change the checkbox data. You can't just call it after all changes are made. It must be made for each change in the CheckBox value.

foreach (DataGridViewRow row in Results.Rows)
    {
       var ck =  (DataGridViewCheckBoxCell) row.Cells["check"];
       ck.Value = ck.TrueValue;
       Results.RefreshEdit();
     }
    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM