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

Iterate specific SelectedItems collection

6 Answers 164 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jugoslav
Top achievements
Rank 1
Jugoslav asked on 23 Mar 2014, 11:32 AM
How do i iterate only the "Master" or lnly the Detail Table SelectedItems when a itemCommand event is fired?

foreach (GridDataItem item in RadGrid1.SelectedItems){// do something}


This iterates all selected items which is not what i want ... btw i was not even able to iterate e.Item.OwnerTableView.SelectedItems as it's not supported. 

Thank you

6 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 24 Mar 2014, 04:52 AM
Hi Jugoslav,

If you are setting the EnablePostBackOnRowClick="true" you can access the selected rows in the server side. Please try the following code snippet to get the detail table rows.

C#:
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
 if (e.CommandName == "RowClick" && e.Item.OwnerTableView.Name == "DetailTableName")
 {          
  GridDataItem selectItem = (GridDataItem)RadGrid1.SelectedItems[0];//accessing selected row
  string value = selectItem.GetDataKeyValue("ID").ToString();//accessing datakeyvalue
 }
}

Thanks,
Princy
0
Jugoslav
Top achievements
Rank 1
answered on 24 Mar 2014, 11:00 PM
Hmm it does not work because when i call command from the Master and i leave a detail table row selected it considers both masters and details table selected rows. Any ideas how to make a clear distinction between the selected items in the master and those in the detail table(s). Thank you
0
Jugoslav
Top achievements
Rank 1
answered on 25 Mar 2014, 09:15 AM
switch (e.Item.OwnerTableView.Name)
            {
                case "Master": // Approve all pending stuff
                    if (e.CommandName == "ApproveAllPending")
                    {
                        foreach (GridDataItem item in RadGrid1.SelectedItems)
                        {
                            int StuffID = (int)item.GetDataKeyValue("ID");                           
                            DataAccess.ApproveAllPendingStuff(StuffID);
                            GridTableView nestedView = ((GridDataItem)item).ChildItem.NestedTableViews[0];
                            nestedView.Rebind();
                             
                        }
                    }
                    break;

However when an item is selected in one of the detail tables it throws an exception of type "Object not set to reference ..." 

meaning it considers the child's selected items though it shouldn't :(

Any ideas?

P.S. i was thinking to check the StuffID against null but anyway i just want to be sure that when i say master that only rows being selected in the master will be checked. 

Thank you so much
0
Princy
Top achievements
Rank 2
answered on 26 Mar 2014, 05:47 AM
Hi Jugoslav,

Please try the following code snippet to get the selected rows.

C#:
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
   if (e.CommandName == "ApproveAllPending")
    {
        foreach (GridDataItem item in RadGrid1.SelectedItems)
        {
            if (item.OwnerTableView.Name == "Master")
            {
              // Parent item
              // Access current row column              
              string PID = item["ParentID"].Text;              
            }
            else if (item.OwnerTableView.Name == "Child")
            {
              // Child item
              // Access current row column
              string CID = item["ChildID"].Text;
              // Access Current row's parent
              string PID = item.OwnerTableView.ParentItem["ParentID"].Text;
            }
        }
    }
}

Thanks,
Princy
0
Jugoslav
Top achievements
Rank 1
answered on 28 Mar 2014, 10:00 AM
Thanks for replying Princy. Its much appreciated but this seems not what i need actually.
I just need it check once and if row is selected in the master i would rather IGNORE the child selection(s). 

How do i do that? Thank you again
0
Eyup
Telerik team
answered on 02 Apr 2014, 08:44 AM
Hello Jugoslav,

You can exit the traversing when you come across a master selection:
foreach (GridDataItem item in RadGrid1.SelectedItems)
        {
            if (item.OwnerTableView.Name == "Master")
            {
              // execute custom logic
              break;

Hope this helps. In case you have different requirements or further instructions please elaborate on your specific scenario and send us sample screenshots or video demonstrating the described behavior.

Regards,
Eyup
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Jugoslav
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Jugoslav
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or