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

Loop Thru Grid Rows and extract form values

8 Answers 863 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Troy
Top achievements
Rank 1
Troy asked on 16 Jun 2009, 09:33 PM

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?

8 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 17 Jun 2009, 04:43 AM
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
0
Troy
Top achievements
Rank 1
answered on 17 Jun 2009, 02:46 PM
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.



0
Troy
Top achievements
Rank 1
answered on 18 Jun 2009, 05:41 PM
bump?  anyone have any ideas?
0
Troy
Top achievements
Rank 1
answered on 22 Jun 2009, 01:12 PM
bump.
0
Yavor
Telerik team
answered on 23 Jun 2009, 06:17 AM
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.
0
-DJ-
Top achievements
Rank 1
answered on 24 Jun 2009, 05:15 PM
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-
0
Yavor
Telerik team
answered on 25 Jun 2009, 07:35 AM
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.
0
Chris @ Intrinsic
Top achievements
Rank 1
answered on 17 Feb 2011, 01:07 AM
Thanks.  But, what exactly is the code to do this?  There are no examples again.
Tags
Grid
Asked by
Troy
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Troy
Top achievements
Rank 1
Yavor
Telerik team
-DJ-
Top achievements
Rank 1
Chris @ Intrinsic
Top achievements
Rank 1
Share this question
or