Loop Thru Grid Rows and extract form values

9 posts, 0 answers
  1. Troy
    Troy avatar
    4 posts
    Member since:
    Apr 2009

    Posted 16 Jun 2009 Link to this post


    I have a form with 3 text boxes.  Under this is a RadGrid that is dynamically created, each row represents an entity(ie. User) and each column represents a Permission (Create, Edit, Read, Delete).  Each cell is populated with a user control that tracks the permission setting for each item in the matrix (User:Create =  value1, User:Edit = value2, User:Read = value3, User:Delete = value4)

    The entire grid forms part of the edit form, so every row of the grid is in "edit" mode all the time, but I am not using the EditMode of RadGrid.  Rows are not edited/saved individually.  The user may load the matrix, change values in row 1, column 2, row 3 column 4, etc.  and then click the save button.

    How can I on postback loop through rows and columns and extract the new values for the matrix?


                foreach (GridDataItem gridRow in this.uxRadGridEntityPermissions.MasterTableView.Items)
                {
    TableCell cell = gridRow["permissionColumnName1"]; // returns the cell
    TableCell cell = gridRow["permissionColumnName2"]; // returns the cell
    TableCell cell = gridRow["permissionColumnName3"]; // throws exception - index out of range
    TableCell cell = gridRow["permissionColumnName4"]; // throws exception - index out of range

    TableCell cell = gridRow["permissionColumnName1"]; // returns the cell




    TableCell cell = gridRow["permissionColumnName1"]; // returns the cell
    }

    The above code works in the Init event (all cells can be retrieved) but in the button Click event, only the first 2 cells can be retrieved?
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 16 Jun 2009 Link to this post

    Hi Troy,

    Try the following code snippet in order to loop through the rows and extract the values from grid.

    CS:
     
    protected void Button2_Click(object sender, EventArgs e) 
        foreach (GridDataItem item in RadGrid1.EditItems) 
        { 
            GridEditFormItem editItem = (GridEditFormItem)item.EditFormItem; 
            if (editItem.IsInEditMode) 
            { 
                String Permission1 =  (editItem["PermissionColumnName1"].Controls[0] as TextBox).Text; 
                String Permission2 = (editItem["PermissionColumnName2"].Controls[0] as TextBox).Text; 
            . . . 
            } 
        } 

    Shinu
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Troy
    Troy avatar
    4 posts
    Member since:
    Apr 2009

    Posted 17 Jun 2009 Link to this post

    Thanks.  I tried your code snippet however, the EditItems collection contains 0 elements, so the foreach loop is never entered.

    This led me to think that I needed to put the entire grid into Edit mode, so I tried this (which really seems like a hack -- why not a single property available for this?):

            protected void uxRadGridEntityPermissions_PreRender(object sender, EventArgs e) 
            { 
                foreach (GridDataItem item in uxRadGridEntityPermissions.Items) 
                { 
                    item.Edit = true
                } 
                uxRadGridEntityPermissions.Rebind(); 
            } 

    This caused my ITemplate image button control to disappear because I was adding it to the ItemTemplate property of a GridTemplateColumn.

    I changed this to use the EditItemTemplate property instead, but this caused my ITemplate (an ITemplate class wraps the creation of a user control) to fail because it didn't implement the IBindableTemplate interface.  I changed my template to implement IBindableTemplate and the page renders.

    However, only the 2nd row (out of 2) is rendered in edit mode.  The first row still seems to be rendered in normal mode.  Also, now if I do a postback, all of the columns are duplicated, even though I am doing this in the Init() before creating the grid on each request.

                    this.uxRadGridEntityPermissions.Columns.Clear(); 
     

    And still, after all this, and despite one row looking like it is in EditMode, the EditItems collection in my button click handler still contains 0 elements.  That is probably because the grid is created in Init() but doesn't enter EditMode until PreRender.

    Perhaps I should just use an asp:Repeater instead, since my grid is only displaying an edit form and will never toggle between EditMode and normal mode.



  5. Troy
    Troy avatar
    4 posts
    Member since:
    Apr 2009

    Posted 18 Jun 2009 Link to this post

    bump?  anyone have any ideas?
  6. Troy
    Troy avatar
    4 posts
    Member since:
    Apr 2009

    Posted 22 Jun 2009 Link to this post

    bump.
  7. Yavor
    Admin
    Yavor avatar
    11 posts

    Posted 23 Jun 2009 Link to this post

    Hello Troy,

    The approach mentioned by Shinu should work in your case. However, based on the information which you supplied in the latest post, I suspect there may be a problem with the way the grid control is created. If the issue persists, you can open a formal support ticket, and send us the problematic code, for additional review and testing.

    Best wishes,
    Yavor
    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.
  8. -DJ-
    -DJ- avatar
    263 posts
    Member since:
    Oct 2004

    Posted 24 Jun 2009 Link to this post

    Hi guys,

    Are you sure it should work Yavor? As Troy explained the grid is never in editmode.

    I am dealing with the exact same situation.
    I'm displaying controls in normal mode of the grid, then on a button click I want to loop through each row and extract the values.

    I'm migrating to RadGrid from a normal Gridview where I had this working perfectly, but for some reason it's proving tricky to set it up in a RadGrid.

    Something as simple as this:
            Dim playerCell As TableCell = dataItem("Player"
            chbPlayer = playerCell.FindControl("chbPlayer"
            blnChecked = chbPlayer.Checked 

    isn't working. Always returns false even when the checkbox is checked.

    The easiest solution would be to stay with the gridview, but I must keep the same look & feel as in other parts of the application.

    So the main question is, shouldn't we be able to loop through the gridrows and extract values from controls by a buttonclick?
    I should state that in my case the grid isn't dynamically created and nothing is ajaxified.

    Regards,
    -DJ-
  9. Yavor
    Admin
    Yavor avatar
    11 posts

    Posted 25 Jun 2009 Link to this post

    Hi -DJ-,

    If the control is not in edit mode, the approach will be different. You can iterate through all the items in the control (RadGrid1.MasterTableView.Items), and then through the respective cells, to get all the required data.
    Let me know how this goes.

    Regards,
    Yavor
    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.
  10. Chris @ Intrinsic
    Chris @ Intrinsic avatar
    98 posts
    Member since:
    Oct 2010

    Posted 16 Feb 2011 Link to this post

    Thanks.  But, what exactly is the code to do this?  There are no examples again.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017