Hi,
I'm binding some RadGrids to custom data objects; however I'm having trouble picking the correct item back out of the collection on any sort of event in the grid. It seems that, when sorting a grid, the DataSetIndex returned by the Item in the event doesn't return the correct value (and instead returns the index of the row itself that was clicked). Unfortunately, since these data objects won't be written to the database until much later in the application, so I don't have any IDs to use as a DataKey and have to rely on the index of the object in the collection instead.
Looking at the documentation for DataSetIndex, it looks like the property is only designed for working with DataSets/DataTables. However, I ran into the same issues when using those instead of collections.
Sample code demonstrating this is below (expected functionality is that, when clicking on one of the "Click" buttons, the label at the top will show the Name and Number of the row clicked on. This works fine in its initial configuration, but will return the incorrect results once you sort the grid).
So am I using the DataSetIndex for its intended use or does it actually represent something else? Is there a simple way to get the index an item in the original collection during any RadGrid event?
Thanks in advance.
.aspx:
.aspx.cs:
I'm binding some RadGrids to custom data objects; however I'm having trouble picking the correct item back out of the collection on any sort of event in the grid. It seems that, when sorting a grid, the DataSetIndex returned by the Item in the event doesn't return the correct value (and instead returns the index of the row itself that was clicked). Unfortunately, since these data objects won't be written to the database until much later in the application, so I don't have any IDs to use as a DataKey and have to rely on the index of the object in the collection instead.
Looking at the documentation for DataSetIndex, it looks like the property is only designed for working with DataSets/DataTables. However, I ran into the same issues when using those instead of collections.
Sample code demonstrating this is below (expected functionality is that, when clicking on one of the "Click" buttons, the label at the top will show the Name and Number of the row clicked on. This works fine in its initial configuration, but will return the incorrect results once you sort the grid).
So am I using the DataSetIndex for its intended use or does it actually represent something else? Is there a simple way to get the index an item in the original collection during any RadGrid event?
Thanks in advance.
.aspx:
<telerik:RadScriptManager runat="server" ID="ScriptManager"></telerik:RadScriptManager> |
<asp:Label runat="server" ID="lblResult" Text="Click a button:"></asp:Label> |
<br /> |
<Telerik:RadGrid runat="server" ID="rgListTest" |
onitemcommand="rgListTest_ItemCommand" |
onneeddatasource="rgListTest_NeedDataSource"> |
<MasterTableView AutoGenerateColumns="false" AllowSorting='true'> |
<Columns> |
<telerik:GridBoundColumn HeaderText="Name" DataField="Name" SortExpression="Name"></telerik:GridBoundColumn> |
<telerik:GridBoundColumn HeaderText="Number" DataField="Number" SortExpression="Number"></telerik:GridBoundColumn> |
<telerik:GridButtonColumn CommandName="Click" Text="Click"></telerik:GridButtonColumn> |
</Columns> |
</MasterTableView> |
</Telerik:RadGrid> |
<Telerik:RadGrid runat="server" ID="rgDataSetTest" |
onitemcommand="rgDataSetTest_ItemCommand" |
onneeddatasource="rgDataSetTest_NeedDataSource"> |
<MasterTableView AutoGenerateColumns="false" AllowSorting='true'> |
<Columns> |
<telerik:GridBoundColumn HeaderText="Name" DataField="Name" SortExpression="Name"></telerik:GridBoundColumn> |
<telerik:GridBoundColumn HeaderText="Number" DataField="Number" SortExpression="Number"></telerik:GridBoundColumn> |
<telerik:GridButtonColumn CommandName="Click" Text="Click"></telerik:GridButtonColumn> |
</Columns> |
</MasterTableView> |
</Telerik:RadGrid> |
.aspx.cs:
using System; |
using System.Data; |
using System.Collections.Generic; |
using System.Linq; |
using System.Web; |
using System.Web.UI; |
using System.Web.UI.WebControls; |
public partial class _Default : System.Web.UI.Page |
{ |
protected class MyDataObject |
{ |
public string Name { get; set; } |
public int Number { get; set; } |
} |
protected void Page_Load(object sender, EventArgs e) |
{ |
} |
private List<MyDataObject> GetListData() |
{ |
return new List<MyDataObject>() |
{ |
new MyDataObject() { Name = "Bob", Number = 3 }, |
new MyDataObject() { Name = "Jim", Number = 2 }, |
new MyDataObject() { Name = "Steve", Number = 1 }, |
new MyDataObject() { Name = "Dave", Number = 4 } |
}; |
} |
private DataTable GetDataSetData() |
{ |
DataTable table = new DataTable(); |
table.Columns.Add("Name", typeof(string)); |
table.Columns.Add("Number", typeof(int)); |
table.Rows.Add("Bob", 3); |
table.Rows.Add("Jim", 2); |
table.Rows.Add("Steve", 1); |
table.Rows.Add("Dave", 4); |
return table; |
} |
protected void rgListTest_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e) |
{ |
rgListTest.DataSource = GetListData(); |
} |
protected void rgListTest_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e) |
{ |
if (e.CommandName == "Click") |
{ |
lblResult.Text = String.Format("Clicked on LIST row {0} (ds-index:{1}): {2} {3}", |
e.Item.ItemIndex, |
e.Item.DataSetIndex, |
GetListData()[e.Item.DataSetIndex].Name, |
GetListData()[e.Item.DataSetIndex].Number |
); |
} |
} |
protected void rgDataSetTest_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e) |
{ |
if (e.CommandName == "Click") |
{ |
lblResult.Text = String.Format("Clicked on DATATABLE row {0} (ds-index:{1}): {2} {3}", |
e.Item.ItemIndex, |
e.Item.DataSetIndex, |
GetDataSetData().Rows[e.Item.DataSetIndex]["Name"], |
GetDataSetData().Rows[e.Item.DataSetIndex]["Number"] |
); |
} |
} |
protected void rgDataSetTest_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e) |
{ |
rgDataSetTest.DataSource = GetDataSetData(); |
} |
} |