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
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