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

set datasource of details view in code behind

3 Answers 328 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kashif Imran
Top achievements
Rank 1
Kashif Imran asked on 13 May 2009, 01:07 PM

I need to show list of questions in grid and their answers in details view when a person expnads question item. Below is how I set datasource of radGrid. How can i set the datasource of nested details grid now?



protected
void ShowQuestions()

 

{

 

try

 

 

 

 

{

 

    this.radGridQuestions.DataSource = this.questions;

 

}

 

 

catch

 

 

 

 

{

}

}

 

<

 

tk:RadGrid ID="radGridQuestions" runat="server" AutoGenerateColumns="False" ShowHeader="false" GridLines="None">

 

 

<MasterTableView DataKeyNames="Id">

 

 

<Columns>

 

 

<tk:GridTemplateColumn>

 

 

<ItemTemplate>

 

 

<table>

 

 

<tr>

 

 

<td style="width: 30px; vertical-align: top;">

 

 

<img src="<%# PostStatusImage(Container.DataItem)%>" alt="PostStatus" />

 

 

</td>

 

 

<td style="vertical-align: top;">

 

 

<asp:LinkButton ID="lbViewQuestion" runat="server" Text='<%# Eval("Title") %>' CommandArgument='<%# Eval("Id") %>'

 

 

CommandName="ViewQuestion"></asp:LinkButton>&nbsp; Posted On:&nbsp;<%# Eval("PostedDate") %>

 

 

</td>

 

 

</tr>

 

 

</table>

 

 

</ItemTemplate>

 

 

</tk:GridTemplateColumn>

 

 

</Columns>

 

 

<DetailTables>

 

 

<tk:GridTableView runat="server" DataKeyNames="Id">

 

 

<ParentTableRelation>

 

 

<tk:GridRelationFields DetailKeyField="ParentThreadId" MasterKeyField="Id" />

 

 

</ParentTableRelation>

 

 

<DetailTables>

 

 

</DetailTables>

 

 

</tk:GridTableView>

 

 

</DetailTables>

 

 

</MasterTableView>

 

 

</tk:RadGrid>

 

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 14 May 2009, 07:34 AM
Hello Kashif,

You can try out the following code to bind your detail table on expanding a row in the MasterTableView:
c#:
protected void radGridQuestions_PreRender(object sender, EventArgs e) 
    { 
       foreach (GridDataItem item in radGridQuestions.MasterTableView.Items) 
        { 
            if (item.Expanded) 
            { 
                GridTableView nestedView = (GridTableView)item.ChildItem.NestedTableViews[0]; 
                nestedView.DataSource = " ";//set the datasource here 
                 
            } 
        } 
   }       

Thanks
Princy.
0
Kashif Imran
Top achievements
Rank 1
answered on 14 May 2009, 02:49 PM

 

 

 

Thanks that worked, but I had to make one small change. If I dont do "nestedView.DataBind();" it always binds parent list to each nested view. But after adding nestedView.DataBind(); it works.

Why do I have to call DataBind where?

if
(item.Expanded)

 

{

 

long pid = Convert.ToInt64(item.GetDataKeyValue("Id"));

 

 

List<ChildThread> list = this.GetChildren(pid);

 

 

GridTableView nestedView = (GridTableView)item.ChildItem.NestedTableViews[0];

 

nestedView.DataSource = list;

//CHANGED I HAD TO MAKE
nestedView.DataBind();

}

 

 

 

 

0
Sebastian
Telerik team
answered on 18 May 2009, 12:11 PM

Hello Kashif,

The more appropriate place to bind nested table to data is the DetailTableDataBind event handler of the grid as illustrated on this online demo:

http://demos.telerik.com/aspnet-ajax/grid/examples/programming/detailtabledatabind/defaultcs.aspx

Thus when you have HierarchyLoadMode = ServerOnDemand (default load mode for hierarchy) only the detailed tables for the expanded parent items will be bound to data.

Alternatively, you can use NestedView template and show/hide the panel with the nested view content dynamically as shown on this online demo:

http://demos.telerik.com/aspnet-ajax/grid/examples/hierarchy/nestedviewtemplate/defaultcs.aspx

Kind regards,

Sebastian
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.
Tags
Grid
Asked by
Kashif Imran
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Kashif Imran
Top achievements
Rank 1
Sebastian
Telerik team
Share this question
or