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

Problem with SelectedItems

6 Answers 281 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rickard Berglund
Top achievements
Rank 1
Rickard Berglund asked on 18 Mar 2010, 02:04 PM
Hi!

I'm trying to get ID and Desciption values from the selected row in a grid when clicking on it, but i cant get it to work. The RadGrid_SelectedIndexChanged event is trigged but SelectedItems is always empty. Any suggestion whats wrong?

Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager ID="SpamControlRadScriptManager" runat="server"/>
    <div>
        <telerik:RadGrid runat="server" ID="RadGrid" OnSelectedIndexChanged="RadGrid_SelectedIndexChanged" AutoGenerateColumns="false" Width="300">
            <MasterTableView DataKeyNames="ID">
                <Columns>
                    <telerik:GridNumericColumn DataField="ID" UniqueName="IDGridNumericColumn" />
                    <telerik:GridBoundColumn DataField="Description" UniqueName="OverviewLanguagGridBoundColumn" />
                </Columns>
            </MasterTableView>
            <ClientSettings EnablePostBackOnRowClick="true">
                <Selecting AllowRowSelect="true" />
            </ClientSettings>
        </telerik:RadGrid>
    </div>
    </form>
</body>
</html>

Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Telerik.Web.UI;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable myDataTable = new DataTable();
        DataColumn myDataColumn;

        myDataColumn = new DataColumn();
        myDataColumn.DataType = Type.GetType("System.String");
        myDataColumn.ColumnName = "ID";
        myDataTable.Columns.Add(myDataColumn);

        myDataColumn = new DataColumn();
        myDataColumn.DataType = Type.GetType("System.String");
        myDataColumn.ColumnName = "Description";
        myDataTable.Columns.Add(myDataColumn);

        DataRow row;
        row = myDataTable.NewRow();
        row["ID"] = "1";
        row["Description"] = "Test1";
        myDataTable.Rows.Add(row);

        row = myDataTable.NewRow();
        row["ID"] = "2";
        row["Description"] = "Test2";
        myDataTable.Rows.Add(row);

        RadGrid.DataSource = myDataTable;
        RadGrid.DataBind();
    }

    protected void RadGrid_SelectedIndexChanged(object sender, EventArgs e)
    {
        // This is always 0
        int count = RadGrid.SelectedItems.Count;

        if (count > 0)
        {
            var dataItem = RadGrid.SelectedItems[0] as GridDataItem;
            if (dataItem != null)
            {
                var ID = dataItem["ID"].Text;
                var desc = dataItem["Description"].Text;
            }
        }
    }
}

Thanks!
Rickard

6 Answers, 1 is accepted

Sort by
0
Pavlina
Telerik team
answered on 18 Mar 2010, 04:03 PM
Hi Rickard,

Please, have in mind that RadGrid does not support mixing declarative grid columns with grid columns added dynamically at runtime. You should either create all the columns in the grid programmatically, or else define them all in the ASPX file.

Greetings,
Pavlina
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Rickard Berglund
Top achievements
Rank 1
answered on 18 Mar 2010, 04:53 PM
Hi!
Thanks for your answer.
But I just created a DataTable dynamically, not the grid columns.
I removed the column declarations in the aspx-file and changed AutoGenerateColumns="true" but still got the same problem.
Any other suggestions?

/Rickard


0
Pavlina
Telerik team
answered on 19 Mar 2010, 09:51 AM
Hello Rickard,

Attached to this message is a simple working application which handles the desired functionality. Check it out and let me know if it works for you.

Additionally, you could review this helps article:
Retrieving primary key field values for selected items

I hope this gets you started properly.

Sincerely yours,
Pavlina
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
daniel derakhshani
Top achievements
Rank 1
answered on 22 Jan 2011, 12:34 PM
Hello Rickard.

Your problem is that you'r binding grid in page_load, binding cause to clear all the pervious data, so your selected items will be clear.
you must add if(!IsPostBack) into your code,and change the page_load to page_Init.

Because putting databind (with !IsPostBack) in page_load do not save your data(viewstate),so when your page postbacked, then grid does not have any data. For saveing data in postback you must put your code into page_Init because Init is before the saving viewstate.

Best Regard.
Daniel Derakhshani
0
Jeff Scranton
Top achievements
Rank 1
answered on 31 Aug 2012, 08:14 PM
Daniel Derakshani, you are a genius! Thanks for sharing this with us. It was a true Homer Simpson "DOH!!" moment LOL.
0
Michael
Top achievements
Rank 1
answered on 07 Dec 2012, 10:34 PM
Bingo....thanks for the tip.
Tags
Grid
Asked by
Rickard Berglund
Top achievements
Rank 1
Answers by
Pavlina
Telerik team
Rickard Berglund
Top achievements
Rank 1
daniel derakhshani
Top achievements
Rank 1
Jeff Scranton
Top achievements
Rank 1
Michael
Top achievements
Rank 1
Share this question
or