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

发布于 2024-07-07 19:27:08 字数 505 浏览 7 评论 0原文

我有一个绑定到 DataViewDataGridView。 用户可以在任何列上对网格进行排序。

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

我尝试通过创建绑定到 DataViewBindingContextBindingManagerBase 对象,然后设置 BindingManagerBase.Position = BindingManagerBase.Count。 如果网格未排序,则此方法有效,因为新行会添加到网格的底部。 但是,如果排序顺序使得该行不添加到底部,则此方法不起作用。

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

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

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?

I tried doing it by creating a BindingManagerBase object bound to the BindingContext of 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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

唯憾梦倾城 2024-07-14 19:27:08

一旦更新绑定的 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;
}

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

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.

//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

以可爱出名 2024-07-14 19:27:08

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

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

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]);
眼角的笑意。 2024-07-14 19:27:08

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

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

希望这有帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文