2010-03-11 75 views
1

我在我的Form和1 GridView上获得了2 DropDownList s。我希望GridView根据DropDownList的选择显示数据。使GridView响应2下拉列表

例如,一个DropDownList包含名称,另一个包含日期。 DropDownList可以回发。所以如果我从第一个DropDownList中选择一个名字,GridView应该根据这个名字显示所有的结果。同样,如果我从其他DropDownList中选择DateGridView应根据日期显示结果。但我不知道如何绑定GridView来响应2 DropDownList

顺便说一句,我绑定下拉列表和网格视图到数据源对象,它是从数据库中获取数据。

任何建议??

回答

0

如果您使用两个DataSource从db中选择数据,并将它们绑定到DropDownList,它会更好,更干净。我想在我的ASP.NET应用程序做你想要的,但可惜的是我有erorrs:/

我唯一sollution是不aspx文件使用DataSouce,但在DropDownListSelectedItem事件中使用DataContext,它是可能的,那么你可以像下面那样绑定到相同的DataView。我不知道,但也许您必须使用空在DataSource之前使用新的数据源来重置GridView

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

尝试,并告诉说作品;)

+0

我使用我的DDL 2个不同的数据源和两个的DDL还自动回。你能否请你解释一下你的代码。 – raziiq 2010-03-13 04:18:37

+0

在我的代码中,我不使用在aspx类中创建数据源。在项目中,我创建了实体数据模型类。在创建此类时,您会看到一个向导,它可以帮助您选择您的数据源。 EDM类是我使用的数据源的对象模型(我使用SQL SERVER数据库,但可以使用对象,数据库等作为数据源)。然后你使用从DataContext继承的DataClass。 DataContext是您的源数据的模型。接下来我使用Linq to SQL来查询我需要的数据。最后,我“重置”GridView源代码,并将我的查询数据设置为Grid源并将其绑定;) – netmajor 2010-03-13 15:14:15

0

您的问题,您应该创建DWO EDM每个数据源的类。在DDL选择事件和简单你选择的DataContext取决于用户选择在DDL。

例子:

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

嘿;)我的回答对您有帮助吗? – netmajor 2010-03-17 09:57:14