开发者

Making GridView Respond to 2 Drop Down Lists

开发者 https://www.devze.com 2022-12-22 18:51 出处:网络
I have got 2 DropDownLists on my Form and 1 GridView. I want the GridView to display the data according to the selection from the DropDownLists.

I have got 2 DropDownLists on my Form and 1 GridView. I want the GridView to display the data according to the selection from the DropDownLists.

For Example, One DropDownList contains Names and another contains Dates. Both the DropDownLists 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 f开发者_运维问答igure 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??


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 ;)


For Your problem You should create dwo EDM class for each data source. And simple in DDL select event Your choose of DataContext depends from user choose in DDL.

Example :

protected void DDL_SelectedItem(object sender, EventArgs e)
{
    TypeOfQueryData query = null;//you must know what type is data You query
    if(this.dropListGrupy.SelectedValue==someItemSelect)
    {
    DataClasses1DataContext db = new DataClasses1DataContext();
     //query to get data from source
    query= from p in db.Dzieckos
                where p.Grupy.Numer == Convert.ToInt32(this.dropListGrupy.SelectedValue)
                orderby p.Nazwisko
                select new { p.Imie, p.Nazwisko };
    }
    if(this.dropListGrupy.SelectedValue==otherItemSelect)
    {
     DataClasses2DataContext db = new DataClasses2DataContext();
     query= from p in db.Dzieckos
                where p.Grupy.Numer == Convert.ToInt32(this.dropListGrupy.SelectedValue)
                orderby p.Nazwisko
                select new { p.Imie, p.Nazwisko };
    }
    this.GridGrupy.DataSource = null;
    this.GridGrupy.DataSource = DzieciGrupa;
 //   this.GridView1.DataSourceID = String.Empty;//maybe You need do that
    this.GridGrupy.DataBind();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号