Cannot find column after sort

A

Allan

Hi,
I am having trouble when I dynamically assign sqldatasources to a gridview.
If I do not sort the gridviews, everything runs well even when I change the
gridview's sqldatasource by changing the dropdownbox. However, after I sort
the gridview by clicking the B column generated by SDS1 then change to SDS2,
error message will be "Cannot find column B". Does any one have ideas about
this?

Here is the simplified code:

ASPX

1 <%@ Page Language="C#"
2 AutoEventWireup="true"
3 CodeFile="WiredTest.aspx.cs"
4 Inherits="WiredTest" %>
5
6 <html xmlns="http://www.w3.org/1999/xhtml">
7 <head>
8 <title>Untitled Page</title>
9 </head>
10 <body>
11 <form id="form1" runat="server">
12 <div>
13
14 Which SDS:
15 <asp:DropDownList ID="ddl_display" runat="server">
16
17 <asp:ListItem Value="SDS1">SDS1</asp:ListItem>
18 <asp:ListItem Value="SDS2">SDS2</asp:ListItem>
19
20 </asp:DropDownList>
21
22 <asp:LinkButton ID="btn_query" runat="server"
23 onclick="btn_query_Click">Query</asp:LinkButton>
24
25
26 <br />
27
28 <asp:GridView ID="gv_CountyAll" runat="server"
29 AllowPaging="True" AllowSorting="True" />
30
31
32 <asp:SqlDataSource ID="SDS1" runat="server"
33 ConnectionString="<%$ ConnectionStrings:CP %>"
34 SelectCommand="
35 select 123 as A, 345 as B
36 union
37 select 123 , 456
38 union
39 select 234 , 567
40 " />
41 <asp:SqlDataSource ID="SDS2" runat="server"
42 ConnectionString="<%$ ConnectionStrings:CP %>"
43 SelectCommand="
44 select 123 as A, 456 as C
45 union
46 select 234 , 567
47 union
48 select 789 , 234 " />
49 </div>
50 </form>
51 </body>
52 </html>


Code Behind:1 public partial class WiredTest : System.Web.UI.Page
2 {
3 protected void Page_Load(object sender, EventArgs e)
4 {
5
6 }
7
8 protected void btn_query_Click(object sender, EventArgs e)
9 {
10 gv_CountyAll.DataSourceID = ddl_display.SelectedValue;
11 }
12
13 }
Thanks,Allan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top