简体   繁体   English

DataGridView C# windows 应用程序(如果鼠标单击列标题,第一行不选择哪个是复选框)

[英]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.我开发了 ac# windows 应用程序,其中 DataGridView 第一列有复选框。 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:为了选择所有行级别的复选框,我有一个 dataGridView1_ColumnHeaderMouseClick 事件,代码是:

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注意:chek是初始化阶段声明的变量

Set your Selection mode property of data grid view to ColumnHeaderSelect将数据网格视图的选择模式属性设置为ColumnHeaderSelect

在此处输入图像描述

and make sure all your 'Text' columns have SortMode set to NotSortable并确保所有“文本”列的SortMode设置为NotSortable

在此处输入图像描述

UPDATE 2更新 2

In which case, Undo everything I ever told before and do that like this在这种情况下,撤消我之前告诉过的所有内容并像这样

Before you are assigning any DataTable to dataGridView1.在将任何 DataTable 分配给 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更新 3

Add an event handler for your dataGridView1's ColumnHeaderMouseClick Event like below为您的 dataGridView1 的ColumnHeaderMouseClick事件添加一个事件处理程序,如下所示

在此处输入图像描述

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.这是 Winforms 中一个非常奇怪的错误。 The problem more generally applies not to the first row, but to the first selected cell in any row of DataGridViewCheckBoxCell(s).该问题更普遍地适用于第一行,但适用于 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.您可以通过单击复选框来选择 CheckBox 单元格,或者选择复选框外的单元格,行为是相同的。 If you select 3 check boxes in the middle of your grid, the first of those three will freeze and not update properly.如果您选中网格中间的 3 个复选框,则这三个复选框中的第一个将冻结并且无法正确更新。 If you try to clear the selection in code, with a dataGridView1.ClearSelection() method call, it still does not work.如果您尝试通过调用 dataGridView1.ClearSelection() 方法清除代码中的选择,它仍然不起作用。

The correct answer is to call datagridview1.RefreshEdit() right after you change the checkbox data.正确答案是在更改复选框数据后立即调用 datagridview1.RefreshEdit()。 You can't just call it after all changes are made.您不能在完成所有更改后才调用它。 It must be made for each change in the CheckBox value.每次更改 CheckBox 值时都必须执行此操作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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