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
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
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
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
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?