在WinForm应用程序中排序DataGridView时选择的行 [英] Selected rows when sorting DataGridView in WinForm application

查看:254
本文介绍了在WinForm应用程序中排序DataGridView时选择的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个WinForm应用程序中,C#4.0,我有一个DataGridView绑定到一个SortableBindingList。因此,它可以通过点击标题列排序 - 所有罚款到目前为止; - )



问题是,所选行似乎被行记住了数。这是发生了什么:

  A *<  - 已选
B
C

现在排序降序,C在顶部并选择。我想要选择一个:

  C *<  - 已选
B
A< - 要有

与所选择的多行相似。是否有解决方法?

解决方案

您可以通过存储当前所选行的值来解决此问题行),然后排序然后重新选择行。



您需要使用CellMouseDown事件 - 有必要使用此事件,因为它是唯一触发的事件发生这种情况。另外,像ColumnHeaderMouseClick这样的事情都已经太晚了。



在CellMouseDown事件处理程序中,检查行索引是否为-1,以确保标题被选中。

  void dataGridView1_CellMouseDown(object sender,DataGridViewCellMouseEventArgs e)
{
if(e.RowIndex == -1)
{
selected = dataGridView1.SelectedRows [0] .Cells [0] .Value.ToString();
}
}

我有一个类级别的字段选择,用于存储所选列的唯一标识符。如果你没有一个唯一的ID,那么你可以添加一个列,以此隐藏它。



然后在 Sorted DataGridView的eventhandler可以使用网格绑定源的.Find()方法:

  void dataGridView1_Sorted (object sender,EventArgs e)
{
if(!string.IsNullOrEmpty(selected))
{
int itemFound = _bindingSource.Find(name,selected);
_bindingSource.Position = itemFound;
}
}

在调查这一点时,我发现以下 post 在MSDN论坛上答案使用DataBindingComplete事件 - 我不是100%为什么他们发现这是必要的,因为我的方法已经用于我所有的测试,但你可能会找到一个有用的参考。


In a WinForm application, C# 4.0, I have a DataGridView bound to a SortableBindingList. Hence it can be sorted by clicking on the header column - all fine so far ;-)

The problem is, that selected rows seem to be "remembered" by the row number. Here is what happens:

A*  <- "Selected"
B
C

Now sorting descending, C on top and selected. I'd like to have still A selected:

C*  <- "Selected"
B
A   <- "Want have"

Same happens similar with multiple rows being selected. Is there a workaround for this?

解决方案

You can work around this behaviour by storing away the value of the currently selected row (or rows) before sorting and then reselecting the row afterwards.

You need to use the CellMouseDown event - it is necessary to use this event since it is the only one which fires before the sort happens. Alternative events like ColumnHeaderMouseClick are all too late.

In the CellMouseDown eventhandler check that the row index is -1 to ensure that the headers were selected.

void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
    if (e.RowIndex == -1)
    {
        selected = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
    }
}

I have a class level field selected that I use to store the unique identifier of the column that is selected. If you don't have a unique id then you could add in a column for this purpose and hide it.

Then in the Sorted eventhandler of the DataGridView you can use the .Find() method of the grid's binding source:

void dataGridView1_Sorted(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(selected))
    {
        int itemFound = _bindingSource.Find("name", selected);
        _bindingSource.Position = itemFound;
    }
}

While investigating this I found the following post on the MSDN forums where the answer uses the DataBindingComplete event - I'm not 100% why they found that necessary as my approach has worked for all my tests, but you might find it a helpful reference.

这篇关于在WinForm应用程序中排序DataGridView时选择的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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