raziiq raziiq - 14 days ago 6
ASP.NET (C#) Question

Making GridView Respond to 2 Drop Down Lists

I have got 2

DropDownList
s on my
Form
and 1
GridView
. I want the
GridView
to display the data according to the selection from the
DropDownList
s.

For Example, One
DropDownList
contains Names and another contains Dates. Both the
DropDownList
s can post back. So if I select a name from 1st
DropDownList
, the
GridView
should show all the results according to that Name. Similarly if i select the
Date
from the other
DropDownList
, the
GridView
should show the results according to the dates. But i cant figure out as how to bind
GridView
to respond to 2
DropDownList
.

BTW i am binding both the Drop Down Lists and the Grid View to the DataSource Objects, which is getting data from the database.

Any Suggestions??

Answer

It's better and cleaner if You use two DataSource with selecting data from db, and binding each of them to the DropDownList. I tried in my ASP.NET app do what You want but unfortunatelly I have erorrs :/

My only sollution is to don't use DataSouce in aspx file, but in DropDownList SelectedItem event use DataContext and it's possible that then you could bind both to the same DataView like below. I am not sure, but maybe You must use null in DataSource to reset GridView before use new data source:

protected void btPokaz_Click(object sender, EventArgs e)
{
    DataClassesDataContext db = new DataClassesDataContext();
    var DzieciGrupa = from p in db.Dzieckos
                where p.Grupy.Numer == Convert.ToInt32(this.dropListGrupy.SelectedValue)
                orderby p.Nazwisko
                select new { p.Imie, p.Nazwisko };
    if (DzieciGrupa.Count() == 0) this.Label1.Text = "W " + this.dropListGrupy.SelectedValue.ToString() + " grupie nie ma dzieci zapisanych!";
    this.GridGrupy.DataSource = null;
    this.GridGrupy.DataSource = DzieciGrupa;
    // this.GridView1.DataSourceID = String.Empty;
    this.GridGrupy.DataBind();

Try it and tell say that works ;)

Comments