c# - 从 GridView C# 中的 DataRow 获取原始对象

标签 c# asp.net linq gridview

我尝试显示一个对象集合 ( IEnumerable )(通过 Linq to Sql 生成)。因此我绑定(bind)了 Gridview s DataSource属性添加到我的 Linq to SQL 方法的生成输出中。在SelectedIndexChanged GridView 事件我尝试转换选定的行 DataItem回到我原来的对象,但最终得到一个 null取而代之的值(value)。

这是我的代码:

protected void Page_Load(object sender, EventArgs e)
{
    RlDataContext dc = new RlDataContext();
    this.dgvReports.DataSource = dc.GetReports(1);
    this.dgvReports.DataBind();
}

protected void dgvReports_SelectedIndexChanged(object sender, EventArgs e)
{
    if (this.dgvReports.SelectedIndex >= 0)
    {
        Report rpt = (Report)this.dgvReports.SelectedRow.DataItem;
    }
}

GetReports 的返回类型是ISingleResult<Report>

最佳答案

在数据 GridView 和列表之间使用绑定(bind)源。做出选择后,datagridview 使用绑定(bind)源的 Current 属性从列表中获取正确的项目。

关于c# - 从 GridView C# 中的 DataRow 获取原始对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22300963/

相关文章:

c# - 如何将 viewbag 显示为 html?

c# - 部分 View 和 html 表单 Asp Mvc

ASP.net 缓存访问导致 foreach 循环中的 Collection Modified 异常

c# - 调用覆盖方法时如何强制执行方法调用(在基类中)?

c# - .Net 远程处理 : Indicate which local interface to use to connect to one server

asp.net - 是否可以在 ASP.NET webforms 中使用 get 而不是 post?

c# - 在数据库中查找随机内容 - Linq

c# - 映射多维数组

c# - 如何获取表单的截图

c# - elasticsearch.net 和 Nest (C#) 中的日期格式问题