C# 查询数据库某个表绑定到下拉框时如何默认初始值

稻庄 2013-11-12 09:18:30
如题,下面是案例:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace F6
{
public partial class UpdateUsers : Form
{
private System.Data.DataTable DT = new System.Data.DataTable();
private SqlDataAdapter SDA = new SqlDataAdapter();
private SqlConnection conn = new SqlConnection();


public UpdateUsers()
{
InitializeComponent();
}

public DataTable selectDutys()
{
DT.Clear();
conn = new SqlConnection(SQLForF6.conStr);
conn.Open();
SqlCommand scSelectDutys = new SqlCommand("select dutyid,dutyname from dutys order by dutyid asc", conn);
SDA = new SqlDataAdapter(scSelectDutys);
SDA.Fill(DT);
conn.Close();
scSelectDutys.Dispose();
return DT;
}

private void UpdateUsers_Load(object sender, EventArgs e)
{

//在窗体load事件为下拉框绑定数据
selectDutyCombox.DataSource = null;
selectDutyCombox.DataSource = selectDutys();
selectDutyCombox.DisplayMember = "dutyname";
selectDutyCombox.ValueMember = "dutyid";
}
}
}


通过上面的代码,在启动这个窗口时,能把从数据库查询出来的数据绑定在下拉框,可每次默认的初始值都是

select dutyid,dutyname from dutys order by dutyid asc

中查询出来的第一行的值。
我能否同时实现下面两个功能:
1/将查询出来的数据绑定在下拉框,
2/可以从查询出来的数据中任选一条数据作为窗体启动时下拉框的初始值
...全文
340 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Claire0118 2015-06-25
  • 打赏
  • 举报
回复
this.combobox.Text="给定值";
稻庄 2013-11-12
  • 打赏
  • 举报
回复
稻庄 2013-11-12
  • 打赏
  • 举报
回复
引用 3 楼 bdmh 的回复:
通过SelectedIndex设置,可以通过comboBox1.Items.IndexOf得到索引
版主能详细说明一下麽?
bdmh 2013-11-12
  • 打赏
  • 举报
回复
通过SelectedIndex设置,可以通过comboBox1.Items.IndexOf得到索引
稻庄 2013-11-12
  • 打赏
  • 举报
回复
不能沉下去了啊,新手啊,想了好久没想出来怎么处理,求救
稻庄 2013-11-12
  • 打赏
  • 举报
回复
不知上面的问题描述大家是否可以看清楚

110,538

社区成员

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

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

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