.net 网格绑定到已排序的DataView时,如何将DataGridView的选定行设置为新添加的行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/191339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:18:24  来源:igfitidea点击:

How to set selected row of DataGridView to newly-added row when the grid is bound to sorted DataView?

.netwinformsdata-bindingdatagridviewdataview

提问by Phillip Wells

I have a DataGridViewbound to a DataView. The grid can be sorted by the user on any column.

我有DataGridView一个DataView. 用户可以在任何列上对网格进行排序。

I add a row to the grid by calling NewRow on the DataView's underlying DataTable, then adding it to the DataTable's Rows collection. How can I select the newly-added row in the grid?

我通过在DataView的基础上调用 NewRow 向网格添加一行DataTable,然后将其添加到DataTable的 Rows 集合。如何在网格中选择新添加的行?

I tried doing it by creating a BindingManagerBaseobject bound to the BindingContextof the DataView, then setting BindingManagerBase.Position = BindingManagerBase.Count. This works if the grid is not sorted, since the new row gets added to the bottom of the grid. However, if the sort order is such that the row is not added to the bottom, this does not work.

我试图通过创建一个这样做BindingManagerBase势必对象BindingContextDataView,然后设置BindingManagerBase.Position = BindingManagerBase.Count。如果网格未排序,这会起作用,因为新行被添加到网格的底部。但是,如果排序顺序是该行未添加到底部,则这不起作用。

How can I reliably set the selected row of the grid to the new row?

如何可靠地将网格的选定行设置为新行?

回答by stefano

As soon as you update the bound DataTable, a "RowsAdded" event is fired by the DataGridView control, with the DataGridViewRowsAddedEventArgs.RowIndex property containing the index of the added row.

一旦更新绑定的 DataTable,DataGridView 控件就会触发“RowsAdded”事件,DataGridViewRowsAddedEventArgs.RowIndex 属性包含添加行的索引。

//local member
private int addedRowIndex;

private void AddMyRow()
{
    //add the DataRow           
    MyDataSet.MyDataTable.Rows.Add(...);

    //RowsAdded event is fired here....

    //select the row
    MyDataGrid.Rows[addedRowIndex].Selected = true;
}

private void MyDataGrid_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
    addedRowIndex = e.RowIndex;
}

Not the most elegant solution, perhaps, but it works for me

也许不是最优雅的解决方案,但它对我有用

回答by Ruben Trancoso

Dont know id its the best solution but for instance looks better than iterate.

不知道它是最好的解决方案,但例如看起来比迭代更好。

            DataRowView drv = (DataRowView)source.AddNew();
            grupoTableAdapter.Update(drv.Row);
            grupoBindingSource.Position = grupoBindingSource.Find("ID", drv.Row.ItemArray[0]);

回答by Brendan Kendrick

Assuming you have some sort of unique identifier in your data source you could iterate over your collection of rows and compare, as such:

假设您的数据源中有某种唯一标识符,您可以遍历行集合并进行比较,如下所示:

Dim myRecentItemID As Integer = 3

For Each row As GridViewRow In gvIndividuals.Rows
    Dim drv As DataRowView = DirectCast(row.DataItem, DataRowView)
    If CInt(drv("ItemID")) = myRecentItemID Then
        gvIndividuals.EditIndex = row.RowIndex
    End If
Next

Hope this helps!

希望这可以帮助!