使用C#windows应用程序从SQL查询进行组合框数据绑定 [英] combobox Data Binding From SQL Query using C# windows application
本文介绍了使用C#windows应用程序从SQL查询进行组合框数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在sql server查询中使用combobox进行数据绑定(
select id,name 来自 town 其中 countryid = combobox2.selectedvalue
)
和display member = name
和价值成员= id
并且请写一个代码视图
感谢我们的努力
最好的问候
解决方案
SqlConnection con = new SqlConnection(your_connection_string);
SqlCommand cmd = new SqlCommand();
SqlDataAdapter ada = new SqlDataAdapter();
DataTable dt = new DataTable();
/ *将此代码写入第一个组合框内点击事件* /
尝试{
cmd.Connection = con;
cmd.CommandText =选择id,来自城镇的名称countryid = + combobox1.selectedvalue;
ada.SelectCommand = cmd;
ada.Fill(dt);
if(dt.Rows.Count > 0)
{
combobox2.DataSource = dt;
combobox2.DisplayMember =name;
combobox2.ValueMember =id;
}
else
{
}
}
catch(Exception exp)
{
}
how can i make Data Binding in combobox from sql server query (
select id , name from town where countryid = combobox2.selectedvalue
)
and display member = name
and value member = id
and please write a view of code
thanks for our effort
best regards
解决方案
SqlConnection con=new SqlConnection("your_connection_string");
SqlCommand cmd=new SqlCommand();
SqlDataAdapter ada=new SqlDataAdapter();
DataTable dt=new DataTable();
/* Write this code inside the first combobox click event */
try{
cmd.Connection=con;
cmd.CommandText="select id , name from town where countryid ="+ combobox1.selectedvalue;
ada.SelectCommand=cmd;
ada.Fill(dt);
if(dt.Rows.Count>0)
{
combobox2.DataSource=dt;
combobox2.DisplayMember="name";
combobox2.ValueMember="id";
}
else
{
}
}
catch(Exception exp)
{
}
这篇关于使用C#windows应用程序从SQL查询进行组合框数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文