GridView“GridView1”触发了未处理的事件排序

时间:2012-03-01 14:34:11

标签: c# data-binding gridview sqldatasource

我在c#中使用工具箱创建了一个gridview, 它能够显示&对我的sqldatasource中的项进行排序,但是当我更改sqldatasource时,可以在下面的代码中看到,它显示错误“GridView'GridView1'触发的事件排序未处理”

SqlDataSource searchResults = new SqlDataSource(WebConfigurationManager.ConnectionStrings["MyDbConn"].ToString(), "SELECT * FROM Books WHERE id=1");
GridView1.DataSourceID = null;
GridView1.DataSource = searchResults;
GridView1.DataBind();

以下是我的gridview&我的Default.aspx中的sqldataconnection代码(通过拖放工具箱创建)

        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AllowSorting="True" AutoGenerateColumns="False" 
        DataSourceID="SqlDataSource1" BackColor="White" BorderColor="#DEDFDE" 
        BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" 
        GridLines="Vertical" Width="748px">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" />
            <asp:BoundField DataField="BookName" HeaderText="BookName" 
                SortExpression="BookName" />
            <asp:BoundField DataField="Status" HeaderText="Status" 
                SortExpression="Status" />
            <asp:BoundField DataField="ReturnDate" HeaderText="ReturnDate" 
                SortExpression="ReturnDate" />
            <asp:CheckBoxField DataField="Reserve" HeaderText="Reserve" 
                SortExpression="Reserve" />
        </Columns>
        <FooterStyle BackColor="#CCCC99" />
        <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
        <RowStyle BackColor="#F7F7DE" />
        <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#FBFBF2" />
        <SortedAscendingHeaderStyle BackColor="#848384" />
        <SortedDescendingCellStyle BackColor="#EAEAD3" />
        <SortedDescendingHeaderStyle BackColor="#575357" />
    </asp:GridView>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:MyDbConn %>" 
        SelectCommand="SELECT * FROM [Books]"></asp:SqlDataSource>

1 个答案:

答案 0 :(得分:3)

<强>更新

您不必动态添加新数据源,因为您要更改的只是数据源的SelectCommand。只是做

SqlDataSource1.SelectCommand = "SELECT * FROM Books WHERE id=1";
gv.DataBind();

如果您想通过搜索字词搜索图书,可以执行类似

的操作
SqlDataSource1.SelectCommand = "SELECT * FROM Books WHERE id LIKE '%" + searchTxt.Text + "'%";
gv.DataBind();

据我所知,动态添加新数据源似乎会造成严重问题。


请尝试

SqlDataSource searchResults = new SqlDataSource(WebConfigurationManager.ConnectionStrings["MyDbConn"].ToString(), "SELECT * FROM Books WHERE id=1");
searchResults.ID = "searchResults"; //or something else
this.Controls.Add(searchResults);
GridView1.DataSourceID = searchResults.ID;
GridView1.DataBind();

或更容易

 SqlDataSource searchResults = new SqlDataSource(WebConfigurationManager.ConnectionStrings["MyDbConn"].ToString(), "SELECT * FROM Books WHERE id=1");
this.Controls.Add(searchResults);
GridView1.DataSource = searchResults;
GridView1.DataBind();