如何在c#.net中从组合框中选择项目时触发事件 [英] how to trigger a event when an item is selected from combobox in c#.net
问题描述
private void Form1_Load( object sender,EventArgs e)
{
string Sql_type = 从lk_tb_property_type中选择property_type_id,type_name;
OleDbCommand cmd_type = new OleDbCommand(Sql_type,con);
OleDbDataReader DR_two = cmd_type.ExecuteReader();
DataTable table_one = new DataTable();
table_one.Load(DR_two);
DataRow row = table_two.NewRow();
行[ type_name] = 选择Poperty Name;
行[ property_type_id] = 0 跨度>;
table_one.Rows.InsertAt(row, 0 );
comboBox1.DataSource = table_one;
comboBox1.DisplayMember = name;
comboBox1.ValueMember = id;
comboBox1.Text = 选择Poperty Name;
}
使用此代码我从database.now获取组合框的值。假设我的combobx有两个名为A和B的项目。我有一个更多的组合框...现在我想要的是当用户从组合框中选择项目A时,第二个组合框应该在用户选择项目B时显示与项目A相关的数据,那么应该显示与项目B相关的数据...所以如何实现这一点... ??
检查这个
c#combobox selectedindexchanged events [ ^ ]
你好,
我将只展示一个小例子这将显示使用comboboxselectedindexchanged事件,如上面的Karthik Sir所示:我将只展示一小段代码:
我有2个组合框:
combobox1和combobox2:
- 在combobox1中我填写了一个列表项目。
- 如果我从combobox1中选择一个项目,那么就像你想要的那样:
引用:当用户从组合框中选择项目A时,当用户选择项目B时,第二个组合框应显示与项目A相关的数据,然后应显示与项目B相关的数据
以下是代码:
FORM CODE:
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load( object sender,EventArgs e)
{
load_first_ComboBox();
}
public void load_first_ComboBox()
{
List< string> list = new 列表< string> { a, b, c}; /// //创建了一个列表,此列表将作为combobox1的源。在你的情况下,它将是数据库
comboBox1.DataSource = list; /// /将combobox1绑定到源。
}
private void comboBox1_SelectedIndexChanged(< span class =code-keyword> object sender,EventArgs e)
{
switch (comboBox1.SelectedItem.ToString( )) /// //使用开关来测试从第一个组合框中选择的内容
{
case a:
comboBox2.Text = 您选择了; /// /与a相关的数据
破跨度>;
case b :
comboBox2.Text = 您选择了b; /// /与b相关的数据
破跨度>;
case c :
comboBox2.Text = 您选择了c; /// /与c相关的数据
破跨度>;
}
}
}
- 我总是建议使用UI的人阅读有关背景工作者的文章。
- 如果处理增加,则UI变得无响应。要在背景工作者上克服这一点。
参考:
初学者的BackgroundWorker类示例 [ ^ ]
- 如果此代码有效,请告诉我,谢谢
非常感谢,
- Rahul
private void Form1_Load(object sender, EventArgs e)
{
string Sql_type = "select property_type_id, type_name from lk_tb_property_type";
OleDbCommand cmd_type = new OleDbCommand(Sql_type, con);
OleDbDataReader DR_two = cmd_type.ExecuteReader();
DataTable table_one = new DataTable();
table_one.Load(DR_two);
DataRow row = table_two.NewRow();
row["type_name"] = "Select Poperty Name";
row["property_type_id"] = 0;
table_one.Rows.InsertAt(row, 0);
comboBox1.DataSource = table_one;
comboBox1.DisplayMember = "name";
comboBox1.ValueMember = "id";
comboBox1.Text = "Select Poperty Name";
}
with this code i am fetching values for a combobox from database.now suppose my combobx is having 2 items named A and B..I have one more combobox...now what i want is that when user chooses item A from combobox the second combobox should display data related to item A when user chooses item B then data related to item B should be displayed...so how to achieve this...??
check this
c# combobox selectedindexchanged events[^]
Hello,
I will just demonstrate a small example which will show you the use of comboboxselectedindexchanged event as per illustrated by Karthik Sir above: i will just show a small piece of code:
I have 2 comboboxes:
combobox1 and combobox2:
- In combobox1 i have populated a list of items.
- If i select an item from combobox1, then as you wanted:
Quote:when user chooses item A from combobox the second combobox should display data related to item A when user chooses item B then data related to item B should be displayed
Here is the code :
FORM CODE:
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { load_first_ComboBox(); } public void load_first_ComboBox() { List<string> list = new List<string> { "a", "b", "c" }; /////created a list, this list will act as a source for the combobox1. In your case it will be the database comboBox1.DataSource = list; ////bind the combobox1 to the source. } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { switch (comboBox1.SelectedItem.ToString()) /////using switch to test as to what was selected from the first combobox { case "a": comboBox2.Text = "you selected a"; ////data related to "a" break; case "b": comboBox2.Text = "you selected b"; ////data related to "b" break; case "c": comboBox2.Text = "you selected c"; ////data related to "c" break; } } }
- I always suggest people who work with UI to read an article on backgroundworker.
- If the processing increases then UI becomes unresponsive.To overcome this read on backgroundworker.
Refer:
BackgroundWorker Class Sample for Beginners[^]
- Please let me know if this code works, thanks
Thanks a ton,
- Rahul
这篇关于如何在c#.net中从组合框中选择项目时触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!