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

DataKeyValue incorrect in RadGrid_ItemCommand event

2 Answers 157 Views
This is a migrated thread and some comments may be shown as answers.
Top achievements
Rank 1
BtsiTech asked on 30 Oct 2012, 12:50 AM

I am trying to use the RadGrid control to implement a table
that has a field indicating if the record is a favourite,
based upon a database bit field. In the RadGrid the field is
an image button.
I am using the RadGrid_ItemCommand event
to toggle the status of the flag in the database for the record
that the user clicked on.
I use the item.GetDataKeyValue("<DataKeyField>") to get the IDs that are
used to identify the record to toggle.

The problem:
The value returned by item.GetDataKeyValue("cli_id") is not
always correct. It is sometimes a value corresponding to a
different row.
I also tried using the CommandArgument property, but that has the
same behavior.

How do I get the correct value for the row that was clicked on

by the user?

Here is the RadGrid_ItemCommand event:

protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
    Trace.Write("RadGrid1_ItemCommand: starting up...");
    if (e.CommandName == "ChangeStatus")
        GridDataItem item = (GridDataItem)e.Item;
        //Get the record IDs from the DataKeyNames
        System.Data.DataRowView oRow = (System.Data.DataRowView)item.DataItem;
        int iApp_id = Convert.ToInt32(item.GetDataKeyValue("app_id"));
        int iCli_id = Convert.ToInt32(item.GetDataKeyValue("cli_id"));
        bool bIsFav = Convert.ToBoolean(item.GetDataKeyValue("IsFav"));
        Trace.Write("RadGrid1_ItemCommand: iApp_id: " + iApp_id);
        Trace.Write("RadGrid1_ItemCommand: cli_id: " + iCli_id);
        Trace.Write("RadGrid1_ItemCommand: bIsFav: " + bIsFav);
        int itmp = Convert.ToInt32(e.CommandArgument);
        //Toggle the Favorite status for that combination
        SqlConnection oConn = new SqlConnection(SqlDataSource_GridViewSolutions.ConnectionString);
        SqlCommand oCmd = new SqlCommand("toggle_gru_IsFav_Status", oConn);
        oCmd.CommandType = System.Data.CommandType.StoredProcedure;
        //ToDo make User name dynamic: oCmd.Parameters.Add(this.Page.User.Identity.Name);
        oCmd.Parameters.Add(new SqlParameter("strLoginID", "the_user"));
        oCmd.Parameters.Add(new SqlParameter("app_id", iApp_id));
        oCmd.Parameters.Add(new SqlParameter("cli_id", iCli_id));
        catch (Exception ex1)
            Response.Write("Error updating favorite status: " + ex1.Message);

Here is the markup for the RadGrid control:

<div class="apps_by_clients">
    <asp:SqlDataSource ID="SqlDataSource_GridViewSolutions"
        ConnectionString="Data Source=BTSLSQLDEV01;Initial Catalog=GenericStorage;User ID=userid;pwd=xxxxxxxxx"
        SelectCommand="EXEC GetAppsWithFavStatus 'the_user'"
        runat="server" />
<telerik:RadGrid ID="RadGrid1" runat="server" 
    DataKeyNames="app_id, cli_id, IsFav"
        <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
        <RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column"></RowIndicatorColumn>
        <ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column"></ExpandCollapseColumn>
            <telerik:GridBoundColumn DataField="cli_name" 
                FilterControlAltText="Filter cli_name column" HeaderText="cli_name" 
                UniqueName="cli_name" SortExpression="cli_name">
            <telerik:GridBoundColumn DataField="app_name" 
                FilterControlAltText="Filter app_name column" HeaderText="app_name" 
                SortExpression="app_name" UniqueName="app_name">
            <telerik:GridBoundColumn DataField="app_id" DataType="System.Decimal" 
                FilterControlAltText="Filter app_id column" HeaderText="app_id" ReadOnly="True" 
                SortExpression="app_id" UniqueName="app_id">
            <telerik:GridTemplateColumn DataField="IsFav" 
                    <asp:ImageButton ID="ImageButton1" runat="server" 
                        CommandArgument='<%# Eval("cli_id") %>'
            <EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
        <PagerStyle PageButtonCount="5" />
    <PagerStyle PageButtonCount="5" />
    <FilterMenu EnableImageSprites="False"></FilterMenu>

We are using ASP.Net 4.0 on Windows server 2008
We are using 2012.2.607.40 of the Telerik Radcontrols


2 Answers, 1 is accepted

Sort by
Angel Petrov
Telerik team
answered on 01 Nov 2012, 03:21 PM
Hi Geoff,

Thank you for contacting us.

Based on you scenario I have created a project with a RadGrid and an image button and GetDataKeyValue behaves correctly as shown in this video. Could you please open a regular support ticket and send us a runnable project in which this problem replicates? Attached is the project which I have used in the video. Please review it and tell me if I am missing something.

Looking forward to your reply.

Kind regards,
Angel Petrov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Top achievements
Rank 1
answered on 02 Nov 2012, 09:24 PM

Hi Angel,

Thanks for the response.

I tried building a test version of the page in a separate

web site. I do not see the problem then. The key difference

seems to be that I am using the RadGrid in a User Control on

a sitefinity page. I have placed a support ticket for that, so

I will close this post and follow up with the Support ticket.

Thanks for the help.



Asked by
Top achievements
Rank 1
Answers by
Angel Petrov
Telerik team
Top achievements
Rank 1
Share this question