3

I have created a gridview using toolbox in c#, it is able to show & sort the items in my sqldatasource, but when I change the sqldatasource as it can be seen in the below code, it displays the error "The GridView 'GridView1' fired event Sorting which wasn't handled"

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

Below is my gridview & sqldataconnection codes in my Default.aspx (Created by drag & drop from toolbox)

        <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 Answer 1

3

UPDATE

You do not have to dynamically add a new Datasource since all you want to change is the SelectCommand of the Datasource. Just do

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

If you want to search books via a search term, you could do something like

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

Adding a new datasource dynamically seems to cause serious problems as far as I have experienced.


Please try

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();

or easier

 SqlDataSource searchResults = new SqlDataSource(WebConfigurationManager.ConnectionStrings["MyDbConn"].ToString(), "SELECT * FROM Books WHERE id=1");
this.Controls.Add(searchResults);
GridView1.DataSource = searchResults;
GridView1.DataBind();
8
  • 2 errors recieved (I copy pasted your codes), different for 2 parts you sent, For the first one -> The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control with ID 'searchResults' could not be found. For the second one ->Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition
    – HOY
    Mar 1, 2012 at 16:31
  • Can you insert in the second code, after this.Controls.Add(searchResults); : GridView1.DataSourceID = String.Empty; Mar 1, 2012 at 16:45
  • 1
    Now error message is same with the title. "The GridView 'GridView1' fired event Sorting which wasn't handled."
    – HOY
    Mar 1, 2012 at 16:51
  • strange. Can you perhaps provide your gridview markup and all the code where you set datasource/datasourceID? Mar 1, 2012 at 17:00
  • I have added the the source codes, but as I said I added the gridview by drag & drop
    – HOY
    Mar 1, 2012 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.