gpt4 book ai didi

c# - 使用 Linq To Sql 从 DataGridView 获取选定行后面的实体

转载 作者:行者123 更新时间:2023-11-30 19:49:53 24 4
gpt4 key购买 nike

检索 DataGridView 选定行后面的 Linq 实体的优雅/正确方法是什么?我在加载事件的表单中像这样填充我的 DataGridView:

this.Database = new MyAppDataContext();
var userList = from c in this.Database.Users
orderby c.LastName
select c;
this.gridUserList.DataSource = userList;

然后,在我正在执行的表单的 DoubleClick 事件中:

        int userPK = Convert.ToInt32(this.gridUserList.CurrentRow.Cells["colUserPK"].Value);
var user = (from c in this.Database.Users
where c.UserPK == userPK select c).First() ;
//Do something with user object

似乎应该有一种更优雅的方式来获取被双击的用户行。

最佳答案

这是我找到的最佳选择:

var selectedUser = (User)this.gridUserList.CurrentRow.DataBoundItem;

如果您使用 BindingSource,另一个选项是:

var selectedUser = (User)this.userBindingSource.Current;

关于c# - 使用 Linq To Sql 从 DataGridView 获取选定行后面的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3199279/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com