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

Getting column values of selected columns

2 Answers 75 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Matt
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:  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:

"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
0
Accepted
Vladimir
Top achievements
Rank 1
answered on 20 Oct 2008, 06:40 PM
You missed to declare DataKeyNames!

Vlad
0
Matt
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" >
Tags
Grid
Asked by
Matt
Top achievements
Rank 1
Answers by
Vladimir
Top achievements
Rank 1
Matt
Top achievements
Rank 1
Share this question
or