无法在datagridview中使用复选框列突出显示多行 [英] Not able to highlight multiple rows with checkbox column in a datagridview

查看:79
本文介绍了无法在datagridview中使用复选框列突出显示多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的datagridview中有一个未绑定的复选框列,从设计中添加。当我选中相应行的任何复选框时,我想选择并突出显示每一行。为此,我写了以下代码:



  private   void  dgvUserData_CellValueChanged( object  sender,DataGridViewCellEventArgs e)
{
RowCheckBoxClick();
}

private void RowCheckBoxClick()
{
int count = 0 ;
foreach (DataGridViewRow row in dgvUserData.Rows)
{
if (Convert.ToBoolean(row.Cells [ 0 ]。Value))
{
row.Selected = true ;
count = count + 1 ; d
}
else
{
row.Selected = false ;
count = count - 1 ;
}
}

// txtRecordSelected.Text = count。 ToString();
}





但它表现得完全出乎意料。在我注意到调试时,Convert.ToBoolean(row.Cells [0] .Value)值对于我检查的复选框变为null。我试图将RowCheckBoxClick()事件放在其他事件中,但它们都没有正常工作。

但令人惊讶的是,当我点击屏幕上的任何其他按钮时,数据网格视图显示正确的行突出显示或选中。

我错过了什么吗?任何建议都将不胜感激。

解决方案

尝试使用CellValueChanged和CurrentCellDirtyStateChanged事件



  private   void  dataGridView1_CellValueChanged( object  sender,DataGridViewCellEventArgs e)
{
if (e.RowIndex > = 0 && e.ColumnIndex == 0
{
MessageBox.Show(dataGridView1.Rows [e.RowIndex] .Cells [ 0 ]。Value.ToString());
}
}

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







Quote:

当提交用户指定的值时会发生DataGridView.CellValueChanged事件,这通常发生在焦点离开单元格时。

In但是,如果是复选框单元格,通常需要立即处理更改。要在单击单元格时提交更改,您必须处理DataGridView.CurrentCellDirtyStateChanged事件。在处理程序中,如果当前单元格是复选框单元格,则调用DataGridView.CommitEdit方法并传入Commit值。

当单元格值更改时,控件中的行不会自动排序。要在用户修改单元格时对控件进行排序,请在CellValueChanged事件处理程序中调用Sort方法。





http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged .aspx [ ^ ]


I have an unbound checkbox column in my datagridview, added from the design. I want to select and highlight each row when i check any checkbox of corresponding row. For this i have wrote the following code:

private void dgvUserData_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            RowCheckBoxClick();
        }

private void RowCheckBoxClick()
        {
            int count = 0;
            foreach (DataGridViewRow row in dgvUserData.Rows)
            {
                if (Convert.ToBoolean(row.Cells[0].Value))
                {
                    row.Selected = true;
                    count = count + 1;d
                }
                else
                {
                    row.Selected = false;
                    count = count - 1;
                }
            }
            
            //txtRecordSelected.Text = count.ToString();
        }



But it is behaving totally unexpected way. While debugging I noticed, the Convert.ToBoolean(row.Cells[0].Value) value is coming as null for checkboxes which i checked. I tried to place the RowCheckBoxClick() event in other events also, but none of them is working properly.
But surprisingly when I am clicking any other button on the screen, then the datagridview is showing with properly row highlighted or selected.
Am i missing anything? Any suggestion would be appreciated.

解决方案

try with both CellValueChanged and CurrentCellDirtyStateChanged events

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex >= 0 && e.ColumnIndex == 0)
    {
        MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
    }
}

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




Quote:

The DataGridView.CellValueChanged event occurs when the user-specified value is committed, which typically occurs when focus leaves the cell.
In the case of check box cells, however, you will typically want to handle the change immediately. To commit the change when the cell is clicked, you must handle the DataGridView.CurrentCellDirtyStateChanged event. In the handler, if the current cell is a check box cell, call the DataGridView.CommitEdit method and pass in the Commit value.
Rows in the control are not automatically sorted when a cell value is changed. To sort the control when the user modifies a cell, call the Sort method in a CellValueChanged event handler.



http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged.aspx[^]


这篇关于无法在datagridview中使用复选框列突出显示多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆