Home » Asp.netRSS

Filtering Gridview Rows from DropDownList Selection

I'm trying to filter data based on the selected value from a drop down list and the according values of a local variable within a gridview_rowdatabound event. For example, if "In Arrears" is selected, the gridview should return all rows where the account balance is less than -100. So far, when I select a value from the dropdown list, it has no effect on the gridview. Here's the code I have:

If (DropDownList1.SelectedIndex = 0) Then

e.Row.Visible = True 

ElseIf ((DropDownList1.SelectedIndex = 1) And (AccountBalance <= -100)) Then

e.Row.Visible = False


ElseIf ((DropDownList1.SelectedIndex = 2) And (AccountBalance > 100) And (AccountBalance < -100)) Then

e.Row.Visible = False



6 Answers Found


Answer 1


you should create first an event  DropDownList1_SelectedIndexChanged and in this code you bind the data to your gridview and put your code and tests

and for sure the dropdown list must have autopost back= true <asp:DropDownList ID="DropDownList1" runat="server"  AutoPostBack="true" >

 Protected Sub UserList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles UserList.SelectedIndexChanged
    End Sub


Answer 2

within the selectedindexchanged event it doesnt recognize e.Row, only within the rowdatabound event. So when I try to use the e.row.visible function I get an error message. What can I do about this?


Answer 3

You can use this:

protected void Dropdownlist1_SelectIndexChanged(……)


    using (SqlDataAdapter adapter = new SqlDataAdapter())


            DataTable dt = new DataTable();

            if(Dropdownlist1.SelectedValue == "xxx")


             adapter.SelectCommand = new SqlCommand("your own sql select statement……", new SqlConnection("xxx"));








            GridView.DataSource = dt;




Answer 4

This wont work because the criterion that the filter is based upon is not a databound value. It is a calculated local variable within the rowdatabound event. Is there a way around using an sql statement here?


Answer 5

If you want to Calculate things, maybe do this in the SQl is the best:

select a,b,(a+b) as xxx from ……

Thus you can use Sql to filter with...


<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter