如何制作,在combobox2中显示属于combobox1的内容 [英] how make, that in combobox2 show what belong to combobox1

查看:81
本文介绍了如何制作,在combobox2中显示属于combobox1的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有两个数据库表:

hello, i have two database tables:

Resursai

id

resursas

 

Kop_failai

id

resursas

失败

Nauji


 button3.Enabled = false;
      string serv = "Data Source=HPDL580;Initial Catalog=metaDB;Integrated Security=True";
      SqlConnection connection = new SqlConnection(serv);
      connection.Open();
      SqlDataAdapter resursai = new SqlDataAdapter("SELECT * FROM Resursai ", connection);
      DataTable resTable = new DataTable();
      resursai.Fill(resTable);
      for (int i = 0; i < resTable.Rows.Count; i++)
      {
         comboBox1.Items.Add(resTable.Rows[i]["Resursas"]);
      }

推荐答案

将两个表放入数据集中.在表之间创建一个关系.将父表(resTable)设置为两个组合框的数据源,并使用该关系获取combobox2的值.

Put your two tables in a dataset. Create a relation between the tables. Set the parent table (resTable) as the datasource of both comboboxes and use the relation to get the value for combobox2.

string serv = "Data Source=HPDL580;Initial Catalog=metaDB;Integrated Security=True";
SqlConnection connection = new SqlConnection(serv);
connection.Open();
SqlDataAdapter resursai = new SqlDataAdapter("SELECT * FROM Resursai ", connection);
DataTable resTable = new DataTable();
resursai.Fill(resTable);

SqlDataAdapter failai = new SqlDataAdapter("SELECT * FROM Kop_failai WHERE (Nauji IS NULL) ", connection);
DataTable failaiTable = new DataTable();
failai.Fill(failaiTable);


//Add the tables to a dataset
DataSet ds = new DataSet();
ds.Tables.Add(resTable);
ds.Tables.Add(failaiTable);
//Create a relation
ds.Relations.Add("relation", resTable.Columns["ID"], failaiTable.Columns["Resursas"]);
comboBox1.DataSource = resTable;
comboBox1.DisplayMember = "Resursas";
comboBox2.DataSource = resTable;
//Use the relation
comboBox2.DisplayMember = "relation.Failas";


这篇关于如何制作,在combobox2中显示属于combobox1的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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