Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7573241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:07:57+00:00 2026-05-30T16:07:57+00:00

I have created a gridview using toolbox in c#, it is able to show

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:07:58+00:00Added an answer on May 30, 2026 at 4:07 pm

    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In gridview control, am using RowEditingevent. I have created a command field to handle
I have created a gridview with a column of checkboxes. I want the user
I have a gridview to which I have created an Insert Template in the
I have a gridview and in that gridview i created a list of imagebuttons
I have a GridView to which I bind a dataTable that I manually created.
I have created a custom dialog for Visual Studio Setup Project using the steps
I have created a C# class file by using a XSD-file as an input.
I have two tables I am using to fill a gridview. The tables have
I have created a class doing some jobs like GridView inherit from System.Web.UI.WebControls.WebControl. public
I'm using Silverlight 4 on IE 8. I have created a new Silverlight web

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.