Iterate specific SelectedItems collection

7 posts, 0 answers
  1. Jugoslav
    Jugoslav avatar
    64 posts
    Member since:
    Mar 2011

    Posted 23 Mar 2014 Link to this post

    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
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 23 Mar 2014 in reply to Jugoslav Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Jugoslav
    Jugoslav avatar
    64 posts
    Member since:
    Mar 2011

    Posted 24 Mar 2014 in reply to Princy Link to this post

    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
  5. Jugoslav
    Jugoslav avatar
    64 posts
    Member since:
    Mar 2011

    Posted 25 Mar 2014 in reply to Jugoslav Link to this post

    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
  6. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 26 Mar 2014 in reply to Jugoslav Link to this post

    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
  7. Jugoslav
    Jugoslav avatar
    64 posts
    Member since:
    Mar 2011

    Posted 28 Mar 2014 in reply to Princy Link to this post

    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
  8. Eyup
    Admin
    Eyup avatar
    3005 posts

    Posted 02 Apr 2014 Link to this post

    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.

     
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017