我们怎样才能在使用C#选择combobx1时获得combobox2中的值(我有XML文件) [英] How can we get values in combobox2 on selecting combobx1 using C# ( I have XML file)

查看:72
本文介绍了我们怎样才能在使用C#选择combobx1时获得combobox2中的值(我有XML文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从XML文件中获取了进入combobox1的值,如果我选择某个医院,我需要在组合框中获取该医院的用户。



我是什么尝试过:



 void MainFormLoad(object sender,EventArgs e)
{
var response = new WebClient()。DownloadString(http://192.168.0.74/hospitals.php?);;

string myXmlfile = @C:\Users\anusha \Documents\SharpDevelop Projects \ Hospital Data \ dataata;
DataSet ds = new DataSet();
using(System.IO.FileStream fsReadXml = new System.IO.FileStream(myXmlfile,System.IO.FileMode.Open))
{
ds.ReadXml(fsReadXml);
}
cmbHospitals.DataSource = ds.Tables [0];
cmbHospitals.DisplayMember =hname;
}
void CmbHospitalsSelectedIndexChanged(object sender,EventArgs e)
{
cmbhusers.Items.Clear();
if(cmbHospitals.SelectedItem.ToString()==0)
{
cmbhusers.Items.Add(Created_by);
}
其他
{
cmbhusers.Items.Add(file);
}

解决方案

您必须使用以下方式过滤数据表:

1. DataTable.Select方法(字符串)(System.Data) [ ^ ]

2. Linq to DataSet [ ^ ]或 Linq To Xml [ ^ ]



 DataTable dt =(DataTable)cmbHospitals.DataSource; 
DataTable dst = dt.Select( string .Format( hname ='{0}',cmbHospitals.SelectedItem.ToString()))。CopyToDataTable();
cmbhusers.DataSource = dst;
cmbhusers.DisplayMember = user;


I got values into combobox1 from XML file ,if I select certain hospital i need to get users of that hospital in combo box.

What I have tried:

void MainFormLoad(object sender, EventArgs e)
		{		
			var response= new WebClient().DownloadString("http://192.168.0.74/hospitals.php?");;
			
			string myXmlfile=@"C:\Users\anusha\Documents\SharpDevelop Projects\Hospital Data\data.xml";
            DataSet ds = new DataSet();
			using (System.IO.FileStream fsReadXml = new System.IO.FileStream(myXmlfile, System.IO.FileMode.Open))
		    {
			   ds.ReadXml(fsReadXml);
		     }
			cmbHospitals.DataSource = ds.Tables[0];
			cmbHospitals.DisplayMember = "hname";
		}
		void CmbHospitalsSelectedIndexChanged(object sender, EventArgs e)
		{  
			cmbhusers.Items.Clear();
			if(cmbHospitals.SelectedItem.ToString()=="0")
			{
				cmbhusers.Items.Add("Created_by");
			}
			else
			{
				cmbhusers.Items.Add("file");
			}

解决方案

You have to filter your datatable via using:
1. DataTable.Select Method (String) (System.Data)[^]
2. Linq to DataSet[^] or Linq To Xml[^]

DataTable dt = (DataTable)cmbHospitals.DataSource;
DataTable dst = dt.Select(string.Format("hname='{0}'", cmbHospitals.SelectedItem.ToString())).CopyToDataTable();
cmbhusers.DataSource = dst;
cmbhusers.DisplayMember = "user"; 


这篇关于我们怎样才能在使用C#选择combobx1时获得combobox2中的值(我有XML文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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