ComboBox默认选项问题

shan7719515 2012-06-18 10:45:07
我form里有两个ComboBox
一个选择串口,一个选择波特率

我想在数据库只保存一条记录
每次我选择串口和波特率后就更新这条记录
然后两个ComboBox就默认显示数据里保存的那条

不知道如何实现
...全文
213 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
__天涯寻梦 2012-06-18
  • 打赏
  • 举报
回复
string[] sp = { "0", "1", "2", "3", "4" };// 比如 串口 可选列表
string[] bitraye = { "5", "6", "7", "8", "9" };// 比如 波特率 可选列表

this.comboBox1.DataSource = sp;
this.comboBox2.DataSource = bitraye;

this.comboBox1.SelectedItem = "1";// 数据库查询得出的 串口 值赋予 comboBox1
this.comboBox2.SelectedItem = "8";// 数据库查询得出的 波特率 值赋予 comboBox1

// 注册 SelectedIndexChanged 事件,两个 comboBox 的事件方法指向同一个
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
string sp = (string)this.comboBox1.SelectedItem;
string bitrate = (string)this.comboBox2.SelectedItem;
// 更新数据库
}
shan7719515 2012-06-18
  • 打赏
  • 举报
回复
木人指点一下哇
shan7719515 2012-06-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

就在SelectedIndexChanged事件中处理就行了,联动,如果想使用 key - value 可以自定义类,或者使用字典类型去实现
[/Quote]能给句代码吗
bdmh 2012-06-18
  • 打赏
  • 举报
回复
就在SelectedIndexChanged事件中处理就行了,联动,如果想使用 key - value 可以自定义类,或者使用字典类型去实现
shan7719515 2012-06-18
  • 打赏
  • 举报
回复
不知道能不能设置 key-value 对
Change_L 2012-06-18
  • 打赏
  • 举报
回复
通过下拉框改变事件进行更新数据库,加载时comBox1.datasource=数据库查选的数据;
comboBox1.SelectedText=数据库字段
shan7719515 2012-06-18
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

C# code
string[] sp = { "0", "1", "2", "3", "4" };// 比如 串口 可选列表
string[] bitraye = { "5", "6", "7", "8", "9" };// 比如 波特率 可选列表

this.comboBox1.DataSource = sp;
this.comboBox2.DataSource = bitraye;

t……
[/Quote]
谢谢,我试一下

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧