This is a migrated thread and some comments may be shown as answers.

Getting column values of selected columns

2 Answers 101 Views
This is a migrated thread and some comments may be shown as answers.
Top achievements
Rank 1
Matt asked on 20 Oct 2008, 05:33 PM
I am trying to fetch the primary key field values for grid items using the example on this page:  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=, Culture=neutral, PublicKeyToken=9b171c9fd64da1d1" namespace="DevExpress.Data.Linq" tagprefix="dxdtlnq" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:LinqDataSource ID="LinqDataSource1" runat="server"
            ContextTypeName="AWDataContext" TableName="vVendors">
        <telerik:RadGrid ID="RadGrid1" runat="server"
            DataSourceID="LinqDataSource1" GridLines="None"
<MasterTableView datasourceid="LinqDataSource1" autogeneratecolumns="False">
    <telerik:GridButtonColumn CommandName="Select" UniqueName="Select" Text="Select" DataTextField="VendorId">

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 DataField="Name" HeaderText="Name"
        SortExpression="Name" UniqueName="Name">

<HeaderStyle Width="20px"></HeaderStyle>

<HeaderStyle Width="20px"></HeaderStyle>

                <Selecting AllowRowSelect="True" />

<FilterMenu EnableTheming="True">
<CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation>
    <telerik:RadScriptManager ID="RadScriptManager1" Runat="server">

Each time I click on Select I get the following error after the post back:

"Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index" on the line noted above. 

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?

2 Answers, 1 is accepted

Sort by
Top achievements
Rank 1
answered on 20 Oct 2008, 06:40 PM
You missed to declare DataKeyNames!

Top achievements
Rank 1
answered on 20 Oct 2008, 08:13 PM
That was it, thank you very much.  I updated the MasterTableView in the ASPX as shown below.
<MasterTableView datasourceid="LinqDataSource1" autogeneratecolumns="False" datakeynames="VendorID" >
Asked by
Top achievements
Rank 1
Answers by
Top achievements
Rank 1
Top achievements
Rank 1
Share this question