2014-03-12 54 views
0

我正在开发一个餐厅应用程序,其中将放置新订单。 Itemtype将在组合框中。基于组合框值的选择,结果应显示在DataGridView中。例如,如果我在组合框中选择“Biryani”项目,则应在DataGridView中显示所有Biryani类型项目。基于组合框选定值未显示Gridview数据

+1

请添加你的代码有问题。 – Adil

+1

使用AutoPostBack = True在你的组合中。所以当事件index_change发生时,从数据库中选择结果,或者你可以通过ajax来完成。但是强制提供你已经尝试过的东西。 –

回答

0

正如我可以读,你在谈论DAtaGridView和组合框,你必须使用Windows窗体。所以你可以做的是调用ComboBox的SelectedIndexChanged事件,然后你可以绑定DataGridView。例如

private void ComboBox1_SelectedIndexChanged(object sender, System.EventArgs e) 
{ 
     ComboBox combo = sender as ComboBox; 
      if(combo.SelectedIndex >=0) 
      { 
       int itemId=Convert.ToInt32(combo.SelectedValue); 
       datagridview1.DataSource = somFunction(itemId); 
      } 
} 
0

据我理解你的问题,你也许可以做到这一点:

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Windows.Forms; 

    namespace WindowsFormsApplication1 { 
    public partial class Form1 : Form { 

     public class Selection { 
      public enum eType { None, Indian, Chinese, Italian, British }; 
      public eType Type { get; private set; } 
      public string Name { get; private set; } 

      public Selection(eType xType, string xName) { 
       Type = xType; 
       Name = xName; 
      } // 

     } // class 

     private List<Selection> _AllMeals = new List<Selection>(); 
     public Form1() { 
      InitializeComponent(); 
      comboBox1.DataSource = Enum.GetValues(typeof(Selection.eType)).Cast<Selection.eType>(); 
      comboBox1.SelectedItem = Selection.eType.None; 

      Selection s1 = new Selection(Selection.eType.Chinese, "tasty Wan Tan soup"); 
      Selection s2 = new Selection(Selection.eType.Chinese, "yummy Spring Rolls"); 
      Selection s3 = new Selection(Selection.eType.Indian, "extreme spicy"); 
      Selection s4 = new Selection(Selection.eType.Indian, "deadly spicy"); 
      Selection s5 = new Selection(Selection.eType.Italian, "great Tortellini"); 
      Selection s6 = new Selection(Selection.eType.Italian, "large Pizza"); 
      Selection s7 = new Selection(Selection.eType.British, "fatty Fish and Chips"); 

      _AllMeals.AddRange(new Selection[] { s1, s2, s3, s4, s5, s6, s7 }); 
      dataGridView1.DataSource = _AllMeals; 
     } // 

     private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { 
      object o = comboBox1.SelectedItem; 
      Selection.eType lFilter = (Selection.eType)o; 

      var lOptions = (from x in _AllMeals 
          where x.Type == lFilter 
          select x).ToArray(); 

      dataGridView1.AutoGenerateColumns = true; 
      dataGridView1.DataSource = lOptions; 
      dataGridView1.Invalidate(); 
     } // 

    } // class 
    } // namespace 

访问我的博客www.ohta.de

+0

这是一个ASP.NET WebForms相关问题,您的答案适用于Windows DataGrid –

相关问题