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

Getting the DataSource Index of a row (or: DataSetIndex and Sorting)

2 Answers 462 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jon
Top achievements
Rank 1
Jon asked on 23 Feb 2009, 09:07 PM
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:
        <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 { getset; } 
        public int Number { getset; } 
    } 
 
    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();             
    } 
 

2 Answers, 1 is accepted

Sort by
0
Jon
Top achievements
Rank 1
answered on 24 Feb 2009, 04:02 PM
Hate to bump this, but can anyone provide any insight into this?  As a last resort I could randomly generate an ID on all my objects and use that as a DataKey, but I'd rather not have to rely on that for various reasons.

Thanks.
0
Nikolay Rusev
Telerik team
answered on 27 Feb 2009, 07:52 AM
Hello Jon,

I am afraid that DataSetIndex property of GridDataItem will not give you the index which you need.
I will suggest you find the item from grid data source by DataKeyName of selected GridDataItem.

<telerik:RadGrid runat="server" ID="rgListTest"     
        onitemcommand="rgListTest_ItemCommand"     
        onneeddatasource="rgListTest_NeedDataSource">    
        <MasterTableView AutoGenerateColumns="false" AllowSorting='true' DataKeyNames="Number">    
            <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>   

protected void rgListTest_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)  
    {  
        if (e.CommandName == "Click")  
        {  
            int number = (int)e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Number"];  
            MyDataObject myItem = GetListData().Where(o => o.Number == number).FirstOrDefault();  
            lblResult.Text = String.Format("Clicked on LIST row {0} (ds-index:{1}): {2} {3}",  
            e.Item.ItemIndex,  
            e.Item.DataSetIndex,  
            myItem.Name,  
            myItem.Number  
            );  
        }  
    } 


Sincerely yours,
Nikolay
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Grid
Asked by
Jon
Top achievements
Rank 1
Answers by
Jon
Top achievements
Rank 1
Nikolay Rusev
Telerik team
Share this question
or