views:

46

answers:

2

Hi there.

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

For Example, One Drop Down List contains Names and other contains Dates. Both the Drop Down Lists can post back. So if i select a name from 1st Drop Down list, the Grid View should show all the results according to that Name. Similarly if i select the Date from the other Drop Down List , the Grid View should show the results according to the dates. But i cant figure out as how to bind Grid View to respond to 2 Drop Down List.

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??

A: 

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

netmajor
I am using 2 different DataSources for my DDL and both the DDLs are also autopostback. Can you please explain your code a little.
raziiq
In my code i don't use DataSource creating in aspx class. In project I create Entity Data Model class. During creating this class You see a wizard which help You select Your DataSource. EDM class is a Object Model of the Data Source I use(i use SQL SERVER database, but You can use object, database etc as a source of data). Then You use a DataClass which inherit from DataContext. DataContext is Model of Your source data. Next I use Linq to SQL to query data i need. At the end i "reset" GridView source and set my query data as Grid source and bind it ;)
netmajor
A: 

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();
netmajor
hey ;) Is my answer help You?
netmajor