2010-03-11 44 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