The GridView 'GridView1' fired event Sorting which wasn't handled
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();
Related videos on Youtube
HOY
Enterpreneur, Opportunist. Computer Engineer, Software Developer
Updated on June 04, 2022Comments
-
HOY almost 2 years
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>
-
HOY about 12 years2 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
-
AGuyCalledGerald about 12 yearsCan you insert in the second code, after this.Controls.Add(searchResults); : GridView1.DataSourceID = String.Empty;
-
HOY about 12 yearsNow error message is same with the title. "The GridView 'GridView1' fired event Sorting which wasn't handled."
-
AGuyCalledGerald about 12 yearsstrange. Can you perhaps provide your gridview markup and all the code where you set datasource/datasourceID?
-
HOY about 12 yearsI have added the the source codes, but as I said I added the gridview by drag & drop
-
AGuyCalledGerald about 12 yearsThere is some problem I cannot figure out at the moment. You should be fine if you put the other datasource in the markup (like SqlDataSource1). Is this possible for you or do you have to load it in code behind?
-
HOY about 12 yearsLet me tell you what I am tring to do, I have a search textbox, search button and gridview, when I press search button, it creates an sql query using text in searchbox then sqldatasource, and display the new data in gridview, that is why I was tring to change datasource (which I create using the new sql query), do you think that is the correct approach ?
-
AGuyCalledGerald about 12 yearsLook at my updated answer. Sorry for the messy answer, I did´nt test it properly :(