我尝试显示一个对象集合 ( 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/