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

Edit Grid2 within FromTemplate of Grid1

4 Answers 66 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Stephan
Top achievements
Rank 1
Stephan asked on 26 Jan 2009, 09:14 PM
I have a Radgrid1 control that contains another Radgrid2 within the formtemplate and now I'm having trouble accessing Radgrid2.
I can populate it and show data, but I don't know how to "edit" or "delete" rows in Radgrid2. Can anyone provide some inside on how to user a Grid within a formtemplate of another Grid?

4 Answers, 1 is accepted

Sort by
0
Baatezu
Top achievements
Rank 2
answered on 26 Jan 2009, 11:26 PM
I'm working on a very similar situation - I found this post which solved my problem.

The main part to I took out of that post to solve the problem for me was 
protected void RadGrid2_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)   
    {   
        RadGrid grid = (RadGrid)source;   
        GridDataItem item = (GridDataItem)grid.NamingContainer;   
        string strtxt = item.GetDataKeyValue("ContID").ToString();  
        //your code 

Make sure to set the DataKeyValue in RadGrid1 (the container). In this case it's ContID.
The thread has more details and a fuller code examples.
0
Stephan
Top achievements
Rank 1
answered on 27 Jan 2009, 01:45 PM
That's good to know, but that's not quite it. I'm able to populate Grid2, which is in the form template of Grid1.
I am not using DataSourceID, but rather DataSource, storing the Gridsources as session variables. To populate Grid2 I get the PK from the select row in Grid1 by doing this and it works:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)

{

 

    if ((e.Item is GridEditableItem) && (e.Item.IsInEditMode))

    {
        //...
        this.SessionGrid1Source[e.Item.ItemIndex].PK_value
        //...
    }
}

Now I want to be able to edit Grid2, but when I click the "Edit" link, Grid2 disappears after the postback.
I tried getting Grid2 this way, but it doesn't work, which is probably because it's a Grid within Grid1:
protected void RadGrid2_ItemCommand(object source, GridCommandEventArgs e)    
{  
    if (e.CommandName == RadGrid.EditCommandName)  
    {  
        GridEditableItem item = (GridEditableItem)e.Item;  
 
        RadGrid Grid2 = (RadGrid)item.FindControl("RadGrid2");  
        //Grid2 is always NULL  
    }  
I belive that if I could get a hold of Grid2 in the code above, I could re-assign the Datasource, which gets lost at the postback, and do an inline edit.

Any thoughts?
0
Accepted
Baatezu
Top achievements
Rank 2
answered on 27 Jan 2009, 04:14 PM
Are you using RadGrid2_NeedDataSource to populate the grid?

I added an ItemCommand event to my RadGrid2 and it triggered and my data stayed, but each time it posted back, it looks like it called the NeedDataSource function to keep it populated.

I'm not sure of your exact situation, but if possible, I'd use NeedDataSource to populate the RadGrid2. I always find that works best, and I'd assume you could still use the same method of using sessions in that function... But not 100% on that.

As for the issue with getting radgrid2 in ItemCommand - the object source parameter is the radgrid, so just cast it to a grid
RadGrid grid2 = (RadGrid)source;
and you should have it.
0
Stephan
Top achievements
Rank 1
answered on 27 Jan 2009, 04:30 PM
That makes perfect sense. I didn't know that this works 
"RadGrid grid2 = (RadGrid)source;"

Thanks a lot.
Tags
Grid
Asked by
Stephan
Top achievements
Rank 1
Answers by
Baatezu
Top achievements
Rank 2
Stephan
Top achievements
Rank 1
Share this question
or