0

At the moment i am using this code below to filter my gridview according to what i have selected in the dropdownlist which is calling a SQL statement which populates it.

 protected void SetYear(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
        if (filter != null)
        {
            if (filter.ContainsKey("Year 1"))
            {
                foreach (ListItem li in ddl.Items)
                {
                    if (li.Text == filter["Year 1"].ToString().Substring(2, filter["Year 1"].ToString().Length - 3))
                    {
                        li.Selected = true;
                        return;
                    }
                }
            }
        }
    }
    protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
    {
        DropDownList dd = (DropDownList)sender;
        if (dd.SelectedItem.Text != "Please Select")
        {
            if (filter.ContainsKey("Year 1"))
            {
                filter["Year 1"] = "='" + dd.SelectedItem.Text + "'";
            }
            else
            {
                filter.Add("Year 1", "='" + dd.SelectedItem.Text + "'");
            }
        }
        else
        {
            filter.Remove("Year 1");
        }
        ApplyGridFilter();
        GridView1.PageIndex = 0;
    }

I was wondering if there is any other way i can do this??

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
BlahWoo
  • 345
  • 4
  • 9
  • 28

1 Answers1

0

The way which you are doing is fine. you can use update panel to avoid the full postback. others ways are there like,

Get the result in datatable at first time when page load, and sub sequent request filter the records in datatable itself.

dt.DefaultView.RowFilter = "Name LIKE 'XXX' "

Also if you using collections then use LINQ to filter the records. But all those methods will not give you the upto date records.

watraplion
  • 287
  • 4
  • 17