如何把一个表的一个字段,放在一个comboBox1控件里显示,代码越详细越好! 就是想把comboBox1控件显示一个表的一个字段列! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //定义DataTable表 public DataTable DTTableName = new DataTable ("表名"); //获取表记录 private void GetList() { this.DTTableName = .....;//获取方法不写了,应该会吧。 } //绑定列表 private void BindItemDeail() { comboBox1.DisplayMember = "显示的字段名";//(比方说这个是名称) comboBox1.Validating = "实际值"; //(比方说这个是ID或编号) comboBox1.DataSource =this.DTTableName.DefaultView; } comboBox1.Validating = "实际值"; 改成comboBox1.ValueMember = "实际值"; 2楼高手,我这样写,还是不行,我把代码COPY过来,你看看string connString = "Data Source=LEAHANDE;Initial Catalog=colin;User ID=mis ;Password=mis"; string sql = "select custcode,custname from S_customer"; DataSet ds = new DataSet(); SqlConnection conn = new SqlConnection(connString); SqlDataAdapter ada = new SqlDataAdapter(sql, conn); ada.Fill(ds); this.comboBox1.DataSource = ds; this.comboBox1.DisplayMember = "custcode"; this.comboBox1.ValueMember = "部门编码"; 总是提示,无法显示绑定新的成员。看看还有哪些不对呀, 不要用DataSet 用DataTable string connString = "Data Source=LEAHANDE;Initial Catalog=colin;User ID=mis ;Password=mis"; string sql = "select custcode,custname from S_customer"; DataTable dt = new DataTable (); SqlConnection conn = new SqlConnection(connString); conn.open(); SqlDataAdapter ada = new SqlDataAdapter(sql, conn); ada.Fill(dt); this.comboBox1.DisplayMember = "custcode"; this.comboBox1.ValueMember = "部门编码"; //这个显示的也是用数据库的字段,就是你没个编号都用对应名称的字段吧。 this.comboBox1.DataSource =dt.DefaultView; this.comboBox1.ValueMember = "custcode"; 也还是相同错误提示 string connString = "Data Source=LEAHANDE;Initial Catalog=colin;User ID=mis ;Password=mis"; string sql = "select custcode,custname from S_customer"; DataTable dt = new DataTable(); SqlConnection conn = new SqlConnection(connString); conn.Open(); SqlDataAdapter ada = new SqlDataAdapter(sql, conn); ada.Fill(dt); this.comboBox1.DisplayMember = "custname"; this.comboBox1.ValueMember = "custcode"; //这个显示的也是用数据库的字段,就是你没个编号都用对应名称的字段吧。 this.comboBox1.DataSource = dt.DefaultView; //打错,反了this.comboBox1.DisplayMember = "custname";//这个显示的也是用数据库的字段,就是你没个编号都用对应名称的字段吧。this.comboBox1.ValueMember = "custcode"; //这个是存在里面的值 楼主,你不能把ds直接绑定到下拉框啊,this.comboBox1.DataSource = ds; 这一句应该改成:this.comboBox1.DataSource = ds.table[0];这样就可以了 Winform引用dev菜单显示问题 正则表达式求救 请问有什么工具可以自动生成实体类的,或者其他自动生成代码的工具啊 调用存储过程出现ora:01036错误 请问各位高手PVM可以在C#下调用吗? 求教一个关于读和存config文件的问题,急!!! 急:关于线程与线程池的几个问题 关于编码问题 怎么用C#写网页啊,很迷惘也。没人教,自学好痛苦。 编写下载按钮时出错 不让网页新建窗口,但可以刷新 [VS2008]反映慢的问题!
//定义DataTable表
public DataTable DTTableName = new DataTable ("表名");
//获取表记录
private void GetList()
{
this.DTTableName = .....;//获取方法不写了,应该会吧。
}
//绑定列表
private void BindItemDeail()
{
comboBox1.DisplayMember = "显示的字段名";//(比方说这个是名称)
comboBox1.Validating = "实际值"; //(比方说这个是ID或编号)
comboBox1.DataSource =this.DTTableName.DefaultView;
}
改成comboBox1.ValueMember = "实际值";
string connString = "Data Source=LEAHANDE;Initial Catalog=colin;User ID=mis ;Password=mis";
string sql = "select custcode,custname from S_customer";
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(connString);
SqlDataAdapter ada = new SqlDataAdapter(sql, conn);
ada.Fill(ds);
this.comboBox1.DataSource = ds;
this.comboBox1.DisplayMember = "custcode";
this.comboBox1.ValueMember = "部门编码";
总是提示,无法显示绑定新的成员。看看还有哪些不对呀,
string sql = "select custcode,custname from S_customer";
DataTable dt = new DataTable ();
SqlConnection conn = new SqlConnection(connString);
conn.open();
SqlDataAdapter ada = new SqlDataAdapter(sql, conn);
ada.Fill(dt);
this.comboBox1.DisplayMember = "custcode";
this.comboBox1.ValueMember = "部门编码"; //这个显示的也是用数据库的字段,就是你没个编号都用对应名称的字段吧。
this.comboBox1.DataSource =dt.DefaultView;
也还是相同错误提示
string connString = "Data Source=LEAHANDE;Initial Catalog=colin;User ID=mis ;Password=mis";
string sql = "select custcode,custname from S_customer";
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlDataAdapter ada = new SqlDataAdapter(sql, conn);
ada.Fill(dt);
this.comboBox1.DisplayMember = "custname";
this.comboBox1.ValueMember = "custcode"; //这个显示的也是用数据库的字段,就是你没个编号都用对应名称的字段吧。
this.comboBox1.DataSource = dt.DefaultView;
this.comboBox1.DisplayMember = "custname";//这个显示的也是用数据库的字段,就是你没个编号都用对应名称的字段吧。
this.comboBox1.ValueMember = "custcode"; //这个是存在里面的值
this.comboBox1.DataSource = ds;
这一句应该改成:
this.comboBox1.DataSource = ds.table[0];
这样就可以了