我有一个LinqDataSource,它将数据提供给ASP Web窗体中的GridView。但是,在我的GridView中,我没有显示我的SQL表的所有列(因此我的LINQ类)。现在我想从GridView.SelectedItem获取主键值(让我们称之为RecordID)。如何访问绑定到SelectedItem的LINQ类对象,以便可以访问该对象的属性?
答案 0 :(得分:0)
您可以指定gridview的datakeynames
属性,如下所示:
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
autogeneratecolumns="true"
emptydatatext="No data available."
autogenerateselectbutton="true"
datakeynames="CustomerID"
onselectedindexchanged="CustomersGridView_SelectedIndexChanged"
runat="server">
</asp:gridview>
然后在代码后面的代码中,可以按如下方式访问所选行的数据键:
void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e)
{
// Determine the index of the selected row.
int index = CustomersGridView.SelectedIndex;
// Display the primary key value of the selected row.
Message.Text = "The primary key value of the selected row is " +
CustomersGridView.DataKeys[index].Value.ToString() + ".";
}