C# Gridview数据未根据组合框的选定值显示

C# Gridview数据未根据组合框的选定值显示,c#,asp.net,winforms,datagridview,combobox,C#,Asp.net,Winforms,Datagridview,Combobox,我正在开发一个餐厅应用程序,其中将放置新订单。Itemtype将位于组合框中。根据combobox值的选择,结果应显示在DataGridView中。例如,如果我在combobox中选择“Biryani”项,则所有Biryani类型的项都应显示在DataGridView中。因为我知道您正在谈论DataGridView和combobox,所以您必须使用Windows窗体。因此,您可以调用ComboBox的SelectedIndexChanged事件,然后绑定DataGridView。e、 g 据我所

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

因为我知道您正在谈论DataGridView和combobox,所以您必须使用Windows窗体。因此,您可以调用ComboBox的SelectedIndexChanged事件,然后绑定DataGridView。e、 g


据我所知,你可以这样做:

  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
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Windows.Forms;
命名空间Windows窗体应用程序1{
公共部分类Form1:Form{
公开选课{
公共枚举词组{None,Indian,Chinese,意大利语,British};
公共eType类型{get;private set;}
公共字符串名称{get;private set;}
公共选择(eType xType,string xName){
Type=xType;
Name=xName;
} //
}//类
私人列表_allfeeds=新列表();
公共表格1(){
初始化组件();
comboBox1.DataSource=Enum.GetValues(typeof(Selection.eType)).Cast();
comboBox1.SelectedItem=Selection.eType.None;
选择s1=新选择(Selection.eType.Chinese,“美味万谭汤”);
选择s2=新选择(Selection.eType.Chinese,“美味春卷”);
选择s3=新选择(Selection.eType.Indian,“极端辣”);
选择s4=新选择(Selection.eType.Indian,“致命辣”);
选择s5=新选择(Selection.eType.意大利语,“great Tortellini”);
选择s6=新选择(Selection.eType.意大利语,“大比萨饼”);
选择s7=新选择(Selection.eType.British,“脂肪鱼和薯条”);
_AllFounds.AddRange(新选择[]{s1、s2、s3、s4、s5、s6、s7});
dataGridView1.DataSource=\u所有膳食;
} //
私有无效组合框1\u SelectedIndexChanged(对象发送方,事件参数e){
对象o=组合框1.SelectedItem;
Selection.eType lFilter=(Selection.eType)o;
变量变量=(从x开始)
其中x.Type==lFilter
选择x).ToArray();
dataGridView1.AutoGenerateColumns=true;
dataGridView1.DataSource=lOptions;
dataGridView1.Invalidate();
} //
}//类
}//名称空间

访问我的博客www.ohta.de

请添加问题中的代码。在组合框中使用AutoPostBack=True。因此,当事件索引发生变化时,可以从数据库中选择结果,也可以通过ajax进行选择。但必须提供您尝试过的内容。这是一个与ASP.NET WebForms相关的问题,以及您对Windows DataGrid的答案
  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