Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I need help to update my grid-view after selecting a filter from a drop-down list and clicking the search button.

I have a drop-down list which contains distinct person_types and when I select a person_type from the list I want to click search and for it to automatically update the grid-view.

This is the code for my gridview:

ASP.NET
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                                BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" 
                                CellPadding="4" DataSourceID="SqlDataSource2" 
                                Width="2092px" AllowSorting="True" 
                                onselectedindexchanged="GridView1_SelectedIndexChanged">
                                <Columns>
                                    <asp:BoundField DataField="Employee_ID" HeaderText="EmployeeID" 
                                        SortExpression="Employee_ID" />
                                    <asp:BoundField DataField="Student_ID" HeaderText="StudentID" 
                                        SortExpression="Student_ID" />
                                    <asp:BoundField DataField="Title" HeaderText="Title" 
                                        SortExpression="Title" />
                                    <asp:BoundField DataField="Legal_FName" HeaderText="First Name" 
                                        SortExpression="Legal_FName" />
                                    <asp:BoundField DataField="Preffered_FName" HeaderText="Preffered Name" 
                                        SortExpression="Preffered_FName" />
                                    <asp:BoundField DataField="LName" HeaderText="Last Name" 
                                        SortExpression="LName" />
                                    <asp:BoundField DataField="Person_Type" HeaderText="Type of person" 
                                        SortExpression="Person_Type" />
                                    <asp:BoundField DataField="Phone_Number" HeaderText="Phone_Number" 
                                        SortExpression="Phone_Number" />
                                    <asp:BoundField DataField="Mobile_Number" HeaderText="Mobile_Number" 
                                        SortExpression="Mobile_Number" />
                                    <asp:BoundField DataField="Street" HeaderText="Street" 
                                        SortExpression="Street" />
                                    <asp:BoundField DataField="Suburb" HeaderText="Suburb" 
                                        SortExpression="Suburb" />
                                    <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
                                </Columns>
                                <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
                                <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
                                <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
                                <RowStyle BackColor="White" ForeColor="#330099" />
                                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
                                <SortedAscendingCellStyle BackColor="#FEFCEB" />
                                <SortedAscendingHeaderStyle BackColor="#AF0101" />
                                <SortedDescendingCellStyle BackColor="#F6F0C0" />
                                <SortedDescendingHeaderStyle BackColor="#7E0000" />
 </asp:GridView>


This is the code for my dropdown list:

ASP.NET
<asp:DropDownList ID="DropDownList1" runat="server" 
                        DataSourceID="SqlDataSource1" DataTextField="Person_Type" 
                        DataValueField="Person_Type">
                    </asp:DropDownList>
                    <asp:Button ID="Button1" runat="server" Text="Search" onclick="Button1_Click" />
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:Award_Management2ConnectionString %>" 
                        SelectCommand="SELECT Distinct [Person_Type] FROM [People]">
                    </asp:SqlDataSource>


Currently the code for my search button is:
aspx:
ASP.NET
<asp:Button ID="Button1" runat="server" Text="Search" onclick="Button1_Click" />


aspx.cs:
C#
protected void Button1_Click(object sender, EventArgs e)
 {
     GridView1.DataBind();
 }


Can someone please help me get the search button to update the grid-view please ?
Posted
Comments
idenizeni 15-Aug-13 12:46pm    
Where is the code where you populate the sqlDatasource2 object?
venkateshCST 16-Aug-13 1:21am    
Hey Aamir,Do you want data to displayed on grid based on item selected in drop down?
Aamir Mitha 18-Aug-13 6:30am    
yup that's what I am trying to do

Just use a SelectParameter in your sqlDatasource1's SelectCommand so it references the selected value of your dropdown list, like so...
ASP.NET
<asp:sqldatasource id="sqlDataSource1" runat="server">
 ConnectionString="<%$ ConnectionStrings:Award_Management2ConnectionString %>"  SelectCommand="Select * From sometable Where SomeID=@SelectedId">
    <SelectParameters>
        <asp:controlparameter controlid="DropDownList1" defaultvalue="0" name="SelectedId" propertyname="SelectedValue" />
    </SelectParameters>
 </asp:sqldatasource>
 
Share this answer
 
v3
Since GridView1 uses SqlDataSource2, what you want to do is update and rebind SqlDataSource2 everytime you search. My code isn't exact as I didn't test it, so make sure you go through it yourself.

In your Search button click event, update the datasource, and then rebind.
C#
protected void Button1_Click(object sender, EventArgs e)
{
   //Change your SelectCommand on SqlDataSource2 (make sure the query is right)
   SqlDataSource2.SelectCommand = "SELECT * FROM People WHERE Person_Type=" + DropDownList1.SelectedItem.Text;
   //Execute select statement
   SqlDataSource2.Select(DataSourceSelectArguments.Empty);

   //Refresh DataSource
   GridView1.DataSource = SqlDataSource2; //You may have to convert data here
   GridView1.DataBind();
}
 
Share this answer
 
v2
Comments
idenizeni 15-Aug-13 12:26pm    
Won't this cause two post-backs to the server?
Silvabolt 15-Aug-13 12:35pm    
You're right, updated the solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900