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

Changing DatasourceID at runtime

5 Answers 309 Views
Grid
This is a migrated thread and some comments may be shown as answers.
James
Top achievements
Rank 1
James asked on 22 Jan 2009, 12:14 PM
Hi,

I'm having a problem at the moment dynamically changing the datasource ID of a grid. I have 2 SQLDataSource controls on the web page. I want to change the grid to point to a each one dependant on certain conditions.

I've tried setting the "DatasourceID" and doing "Databind" in the form init/load events, but the grid always looks like the data from the original dataource.

Has anyone any suggestions?

thx in advance

Bobby

5 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 22 Jan 2009, 01:56 PM
Hi Bobby,

I tried changing the DataSourceID for the RadGrid in the code behind and it is working well. I tried this in the SelectIndexChanged event of a DropDownList. Here is the code I tried.


ASPX:
 <asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
             
             <asp:ListItem Text="SqlDataSource1" ></asp:ListItem> 
               <asp:ListItem Text="SqlDataSource2" ></asp:ListItem> 
            </asp:DropDownList> 

CS:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
        DropDownList ddl=(DropDownList)sender; 
        RadGrid2.DataSourceID = ddl.SelectedItem.Text; 
        RadGrid2.Rebind(); 
    } 

If this does not help consider sending your  aspx and code behind.

 Shinu
0
James
Top achievements
Rank 1
answered on 22 Jan 2009, 02:51 PM

Thanks for the reply Shinu. I've just created a sample app and have the same problem. The postback is done, but the columns and data in the grid don't change. I have got the AutoGenerateColumns set to true

Code is below:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <telerik:RadScriptManager ID="RadScriptManager1" Runat="server">
        </telerik:RadScriptManager>
        <telerik:RadGrid ID="RadGrid2" runat="server" DataSourceID="SqlDataSource2"
            GridLines="None">
<HeaderContextMenu>
<CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation>
</HeaderContextMenu>

<MasterTableView AutoGenerateColumns="False" DataSourceID="SqlDataSource2"
                DataKeyNames="BearID">
<RowIndicatorColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>

<ExpandCollapseColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
    <Columns>
        <telerik:GridBoundColumn DataField="Name" HeaderText="Name"
            SortExpression="Name" UniqueName="Name">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="BearID" DataType="System.Int32"
            HeaderText="BearID" SortExpression="BearID"
            UniqueName="BearID" ReadOnly="True">
        </telerik:GridBoundColumn>
    </Columns>
</MasterTableView>

<FilterMenu>
<CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation>
</FilterMenu>
        </telerik:RadGrid>
         <asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
            
             <asp:ListItem Text="SqlDataSource1" ></asp:ListItem>
               <asp:ListItem Text="SqlDataSource2" ></asp:ListItem>
            </asp:DropDownList>

        <asp:SqlDataSource ID="SqlDataSource2" runat="server"
            ConnectionString="<%$ ConnectionStrings:TitanicConnectionString %>"
            SelectCommand="SELECT [Name], [BearID] FROM [BEAR]">
        </asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:TitanicConnectionString %>"
            SelectCommand="SELECT [BearID], [Name], [Description], [DatePurchased], [Picture], [Checked] FROM [BEAR]">
        </asp:SqlDataSource>
        <br />
   
    </div>
    </form>
</body>
</html>

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
        Dim ddl As DropDownList = DirectCast(sender, DropDownList)
        RadGrid2.DataSourceID = ddl.SelectedItem.Text
        RadGrid2.Rebind()
    End Sub
End Class


0
James
Top achievements
Rank 1
answered on 22 Jan 2009, 03:39 PM
Additional Note: It seems to change the data if the colums list is the same (i.e. just a different where clause), but doesn't seem to change if the data is just a different list of columns
0
Shinu
Top achievements
Rank 2
answered on 23 Jan 2009, 06:25 AM
Hi Bobby,

I tried this on my end and it is working fine. Can you try setting the DataSourceID of the Grid to String.Empty in the SelectedIndexChanged event prior to changing.

CS:
 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
        DropDownList ddl=(DropDownList)sender; 
        RadGrid2.DataSourceID = string.Empty; 
        RadGrid2.DataSourceID = ddl.SelectedItem.Text; 
        RadGrid2.Rebind(); 
    } 


Thanks
Shinu
0
James
Top achievements
Rank 1
answered on 23 Jan 2009, 08:25 AM
'fraid I get the  same results.

Are you using different columns in your data sources?

e.g, if in datasource1, I use BearID and Name, and in Datasource 2, I use BearID and Description.

At design time, the grid shows BearID and Name columns, it's correct at runtime, but if I change the dropdown to the other datasource, the 2 columns are still there. The BearID column shows data as expected, the Name column is blank, but the description column doesn't appear.

Is there some other property I need to change, or method to call to refresh the grid against the different schema once bound?
Tags
Grid
Asked by
James
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
James
Top achievements
Rank 1
Share this question
or