如何在其他下拉列表中获取dropdownlist选择项目各自的项目 [英] how to get dropdownlist selected item respective items in other dropdownlist

查看:56
本文介绍了如何在其他下拉列表中获取dropdownlist选择项目各自的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个下拉列表。我正在从数据库中检索两个名为(Group& SubGroup)的列,并填写数据集。



假设:

组:

A

B

C



子组:

属于A组

1

2

属于B组

3

4



属于C组

5

$





我将名为Group的数据集列复制到了Dropdownlist1。



I've two dropdownlists. I'm retrieving two columns named(Group & SubGroup) from database and filling in to Dataset.

Assume:
Group:
A
B
C

SubGroup:
Belongs to A group
1
2
Belongs to B group
3
4

Belongs to C group
5
6


I copied Dataset Column named "Group" into the Dropdownlist1.

//Get Group Data
          var Y = (from X in dsOptionDetails.Tables[0].AsEnumerable()
                   select X["Group"]).Distinct().ToList();

          foreach (var item in Y)
          {
              ddlModulegroup.Items.Add(item.ToString());
          }







现在我想编写Linq查询以获取Dropdownlist1项目各自的子项目到DropDownlist2。我是linq的新手,我可以知道怎么做?








Now i want to write the Linq query to get the the Dropdownlist1 Selected item respective Child items into DropDownlist2. I'm new to linq, May i knw how?


//Get Subgroup Data
var Z = (from X in ds.Tables[0].AsEnumerable()
                 where X["Group"]= ddlModulegroup.SelectedItem.ToString()
                 select X["SubGroup"]).ToList();

        foreach (var item in ddlModulegroup.SelectedItem.ToString())
        {

            ddlModuleSubgroup.Items.Add(item.ToString());
        }

推荐答案

如果我理解正确的话......



试试这个:

If i understand you correctly...

Try this:
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Group", typeof(string)));
dt.Columns.Add(new DataColumn("SubGroup", typeof(int)));

dt.Rows.Add(new object[]{"A", 1});
dt.Rows.Add(new object[]{"A", 2});
dt.Rows.Add(new object[]{"B", 3});
dt.Rows.Add(new object[]{"B", 4});
dt.Rows.Add(new object[]{"C", 5});
dt.Rows.Add(new object[]{"C", 6});

string dropdownselecteditem = "A"; //get value from DropDown

var result = dt.AsEnumerable()
    .Where(x=>x.Field<string>("Group")==dropdownselecteditem);

foreach(var record in result)
{
    Console.WriteLine("{0}", record.Field<int>("SubGroup"));
}





结果:



Result:

1
2


这篇关于如何在其他下拉列表中获取dropdownlist选择项目各自的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆