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

Is it possible to dynamically change the datasource of a grid

2 Answers 575 Views
Grid
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 11 May 2011, 08:31 PM
Hi everyone,

I want to know if it's possible to change the datasource of a grid. The reason is that I used a listbox and I want that, when someone click on a item in the listbox, the grid automatically show the data associated with the item. But each item contains differents data. Can someone helps me ?

Ps : If it can help you, I want to create something that similiar to a file explorer.

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 12 May 2011, 05:49 AM
Hello David,

You can set the RadGrid datasource dynamically. Here is the similar scenario that I tried.
Aspx:
<telerik:RadGrid ID="RadGrid1" AutoGenerateColumns="true" runat="server">
        </telerik:RadGrid>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString3 %>"
            SelectCommand="SELECT * FROM [Employees]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString3 %>"
            SelectCommand="SELECT * FROM [Test]"></asp:SqlDataSource>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"  onselectedindexchanged="DropDownList1_SelectedIndexChanged">
        <asp:ListItem Text="Datasource1" Value="1"></asp:ListItem>
        <asp:ListItem Text="Datasource2" Value="2"></asp:ListItem>
</asp:DropDownList>

C#:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
   {
       if (DropDownList1.SelectedValue == "1")
       {
           RadGrid1.DataSourceID = "SqlDataSource1";
           RadGrid1.Rebind();
           Response.Write("Hai");
       }
       else
       {
           RadGrid1.DataSourceID = "SqlDataSource2";
           RadGrid1.Rebind();
       }
   }

Thanks,
Shinu.
0
David
Top achievements
Rank 1
answered on 12 May 2011, 02:23 PM
Thank you,

It works very well. It just bother me, cause it exactly the same code that me, except that I didn't knew that I had to write radGrid1.Rebind(); 
Tags
Grid
Asked by
David
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
David
Top achievements
Rank 1
Share this question
or