I am trying to fetch the primary key field values for grid items using the example on this page: http://www.telerik.com/help/aspnet/grid/grdretrieveprimarykeyfieldvaluesforitems.html Here is the aspx and code-behind:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="test_Default" %>
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>
<%@ Register assembly="DevExpress.Data.v8.2.Linq, Version=8.2.4.0, Culture=neutral, PublicKeyToken=9b171c9fd64da1d1" namespace="DevExpress.Data.Linq" tagprefix="dxdtlnq" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Vendor</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="AWDataContext" TableName="vVendors">
</asp:LinqDataSource>
<telerik:RadGrid ID="RadGrid1" runat="server"
DataSourceID="LinqDataSource1" GridLines="None"
onitemcommand="RadGrid1_ItemCommand">
<MasterTableView datasourceid="LinqDataSource1" autogeneratecolumns="False">
<Columns>
<telerik:GridButtonColumn CommandName="Select" UniqueName="Select" Text="Select" DataTextField="VendorId">
</telerik:GridButtonColumn>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class test_Default : System.Web.UI.Page
{
protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
if (e.CommandName == "Select")
{
//the next line throws the error
Response.Write("Primary key for the clicked item from ItemCommand: " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["VendorID"] + "<br>");
}
}
}
<telerik:GridBoundColumn DataField="VendorID" DataType="System.Int32"
HeaderText="VendorID" SortExpression="VendorID" UniqueName="VendorID">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Name" HeaderText="Name"
SortExpression="Name" UniqueName="Name">
</telerik:GridBoundColumn>
</Columns>
<RowIndicatorColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
<ExpandCollapseColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
</MasterTableView>
<ClientSettings>
<Selecting AllowRowSelect="True" />
</ClientSettings>
<FilterMenu EnableTheming="True">
<CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation>
</FilterMenu>
</telerik:RadGrid>
</div>
<telerik:RadScriptManager ID="RadScriptManager1" Runat="server">
</telerik:RadScriptManager>
</form>
</body>
</html>
Each time I click on Select I get the following error after the post back:
Why does this happens? Also what is the recommended way to collect the values of the selected row columns from a RadGrid? Should I just use a GrtidHyperLinkColumn bound to the VendorId?
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="test_Default" %>
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>
<%@ Register assembly="DevExpress.Data.v8.2.Linq, Version=8.2.4.0, Culture=neutral, PublicKeyToken=9b171c9fd64da1d1" namespace="DevExpress.Data.Linq" tagprefix="dxdtlnq" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Vendor</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="AWDataContext" TableName="vVendors">
</asp:LinqDataSource>
<telerik:RadGrid ID="RadGrid1" runat="server"
DataSourceID="LinqDataSource1" GridLines="None"
onitemcommand="RadGrid1_ItemCommand">
<MasterTableView datasourceid="LinqDataSource1" autogeneratecolumns="False">
<Columns>
<telerik:GridButtonColumn CommandName="Select" UniqueName="Select" Text="Select" DataTextField="VendorId">
</telerik:GridButtonColumn>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class test_Default : System.Web.UI.Page
{
protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
if (e.CommandName == "Select")
{
//the next line throws the error
Response.Write("Primary key for the clicked item from ItemCommand: " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["VendorID"] + "<br>");
}
}
}
<telerik:GridBoundColumn DataField="VendorID" DataType="System.Int32"
HeaderText="VendorID" SortExpression="VendorID" UniqueName="VendorID">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Name" HeaderText="Name"
SortExpression="Name" UniqueName="Name">
</telerik:GridBoundColumn>
</Columns>
<RowIndicatorColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
<ExpandCollapseColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
</MasterTableView>
<ClientSettings>
<Selecting AllowRowSelect="True" />
</ClientSettings>
<FilterMenu EnableTheming="True">
<CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation>
</FilterMenu>
</telerik:RadGrid>
</div>
<telerik:RadScriptManager ID="RadScriptManager1" Runat="server">
</telerik:RadScriptManager>
</form>
</body>
</html>
Each time I click on Select I get the following error after the post back: