【问题标题】:Gridview data not getting displayed based on combobox selected valueGridview 数据未根据组合框选择的值显示
【发布时间】:2014-03-12 10:19:01
【问题描述】:

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

【问题讨论】:

  • 请在问题中添加您的代码。
  • 在您的组合中使用 AutoPostBack = True。因此,当事件 index_change 发生时,从您的数据库中选择结果,或者您可以通过 ajax 来完成。但是必须提供您尝试过的内容。

标签: c# asp.net winforms datagridview combobox


【解决方案1】:

据我了解您的问题,您可能可以这样做:

  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

【讨论】:

  • 这是一个与 nand your answer for Windows DataGrid 相关的 ASP.NET WebForms 问题
【解决方案2】:

据我所知,您正在谈论 DAtaGridView 和 ComboBox,您必须使用 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);
          }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多