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

Is this even possible?

7 Answers 65 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jason Thacker
Top achievements
Rank 1
Jason Thacker asked on 19 Aug 2010, 07:33 PM
I have a basic RadGrid (no heirarchy etc.) on a page, with sort, filter and paging enabled and it is associated with a sqldatasource (SELECT * FROM table1), autogeneratecolumns is set to true.

When I select a value from a dropdownlist I programatically change the datasource select string to another value (SELECT * FROM table2) and then databind the grid to change the columns etc.

The table appears to update successfully, showing the columns from the new table. But when I click on a header to sort a column, I get an error saying "Column not found" for that column name.

a) Does anybody know what is causing this?

b) Is there any way to perform the equivalent of a "Refresh schema" on the RadGrid so that it "resets" itself internally to match the new table?

7 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 20 Aug 2010, 01:21 PM
Hello,


You can perform same functionality by attaching the OnNeedDataSource event for grid and setting the grid DataSource in the event. Create the query based on the DropdownList selection and set the datasource. no need to add extra code for sorting, paging and advanced features.

CS:
protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
    // Make the query based DropDownList selection, get the DataSet
    // (source as RadGrid).DataSource = dataset; // set the datasource
}


Thanks,
Princy.
0
Jason Thacker
Top achievements
Rank 1
answered on 20 Aug 2010, 01:35 PM
Thanks for the help. I tried that but I am still getting the "column not found" error.
0
Cori
Top achievements
Rank 2
answered on 20 Aug 2010, 02:32 PM
Hello Jason,

When you change the query in your drop-down are you calling the RadGrid's Rebind  or DataBind method? I would suggest calling the Rebind method.

I hope that helps.
0
Jason Thacker
Top achievements
Rank 1
answered on 20 Aug 2010, 03:12 PM
I have used both, but I am currently using Rebind()
0
Jason Thacker
Top achievements
Rank 1
answered on 20 Aug 2010, 03:15 PM
I have included the relevant source below:

ASPX
<table align="left">
    <tr>
        <td>
            Database Table
        </td>
        <td>
               
        </td>
        <td>
            <asp:DropDownList ID="TableDropDownList" runat="server" AutoPostBack="True" DataSourceID="TableSqlDataSource"
                DataTextField="name" DataValueField="name" OnSelectedIndexChanged="TableDropDownList_SelectedIndexChanged"
                Width="200px">
            </asp:DropDownList>
        </td>
        <td>
               
        </td>
        <td>
            <asp:Button ID="ExportExcelButton" runat="server" Text="Export Excel" OnClick="ExportExcelButton_Click" />
               
            <asp:Button ID="AdminHomeButton" runat="server" Text="Admin Home" 
                onclick="AdminHomeButton_Click" />
        </td>
    </tr>
</table>
 <br />
 <br />
 <br />
<telerik:RadGrid ID="TableRadGrid" runat="server" AllowPaging="True" AllowSorting="True"
    DataSourceID="RadGridSqlDataSource" GridLines="None" Skin="Sunset" OnNeedDataSource="TableRadGrid_NeedDataSource"
    AllowFilteringByColumn="True">
    <MasterTableView >
    </MasterTableView>
    <ExportSettings ExportOnlyData="true">
        <Excel Format="ExcelML" />
    </ExportSettings>
    <PagerStyle Mode="NextPrevNumericAndAdvanced" />
</telerik:RadGrid>
<asp:SqlDataSource ID="RadGridSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:EHS2ConnectionString %>"
    SelectCommand="SELECT * FROM [BCI]"></asp:SqlDataSource>
<asp:SqlDataSource ID="TableSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:EHS2ConnectionString %>"
    SelectCommand="SELECT [name] FROM [EHS2].[sys].[all_objects] WHERE (type_Desc = 'USER_TABLE') ORDER BY name">
</asp:SqlDataSource>

C#
protected void TableDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
    RadGridSqlDataSource.SelectCommand = "SELECT * FROM " + TableDropDownList.SelectedValue;
    TableRadGrid.MasterTableView.SortExpressions.Clear();
    TableRadGrid.MasterTableView.FilterExpression = String.Empty;
    TableRadGrid.Rebind();
}
protected void ExportExcelButton_Click(object sender, EventArgs e)
{
    RadGridSqlDataSource.SelectCommand = "SELECT * FROM " + TableDropDownList.SelectedValue;
    TableRadGrid.DataBind();
    TableRadGrid.ExportSettings.IgnorePaging = true;
    TableRadGrid.ExportSettings.OpenInNewWindow = true;
    TableRadGrid.MasterTableView.ExportToExcel();
}
protected void AdminHomeButton_Click(object sender, EventArgs e)
{
    Response.Redirect("admin.aspx");
}
protected void TableRadGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e) 
{
    RadGridSqlDataSource.SelectCommand = "SELECT * FROM " + TableDropDownList.SelectedValue;
    TableRadGrid.DataSource = RadGridSqlDataSource;
}

Hope that this helps somebody help me ! :)

Thanks again!
0
Casey
Top achievements
Rank 1
answered on 20 Aug 2010, 06:24 PM
Hi Jason,


Can you please try modifying your code for the TableDropDownList_SelectedIndexChanged event to the below? I think you need to set the TableRadGrid's datasource to an empty string before you call the TableRadGrid.Rebind(), otherwise the NeedDataSource event will not be triggered.

Hope this helps!
Casey

protected void TableDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
    RadGridSqlDataSource.SelectCommand = "SELECT * FROM " + TableDropDownList.SelectedValue;
    TableRadGrid.MasterTableView.SortExpressions.Clear();
    TableRadGrid.MasterTableView.FilterExpression = String.Empty;
    TableRadGrid.DataSourceID = "";
    TableRadGrid.Rebind();
}
0
Jason Thacker
Top achievements
Rank 1
answered on 23 Aug 2010, 12:07 PM
That worked! Thanks a lot guys!
Tags
Grid
Asked by
Jason Thacker
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Jason Thacker
Top achievements
Rank 1
Cori
Top achievements
Rank 2
Casey
Top achievements
Rank 1
Share this question
or