日期:2014-05-18  浏览次数:20747 次

winform combox绑定问题
在load事件里给两个combox绑定值,代码如下:
  //combox1绑定值
  ocnn = new OleDbConnection(connections);
  ocnn.Open();
   
  ocmd = ocnn.CreateCommand();
  ocmd.CommandText = "select distinct Single_Date from MIS_InsertSingle";
  DataSet ds = new DataSet();
  oda = new OleDbDataAdapter(ocmd);
  oda.Fill(ds);
  this.cbo_Date.DataSource=ds.Tables[0];
  ocnn.Close();

  //combox2绑定值
  OleDbConnection cnn = new OleDbConnection(connections);
  cnn.Open();
  OleDbCommand cmd = cnn.CreateCommand();
  cmd.CommandText = "select Out_Date from MIS_OutSingle";
  DataSet set = new DataSet();
  OleDbDataAdapter da = new OleDbDataAdapter(cmd);
  da.Fill(set);
  this.cbo_OutDate.DataSource=set.Tables[0];
  cnn.Close();
combox1显示正常而combox2的下拉列表值全是 System.Data.DataRowView。请问这是为什么。

------解决方案--------------------
探讨

引用:
绑定的方法有问题!
那该怎么绑。我现在就会这一种方法

------解决方案--------------------
this.cbo_Date.DataSource=ds.Tables[0];
this.cbo_Date.DisplayMember = "显示字段";
this.cbo_Date.ValueMember = "ID(自增列或者编号)";
探讨
在load事件里给两个combox绑定值,代码如下:
//combox1绑定值
ocnn = new OleDbConnection(connections);
ocnn.Open();

ocmd = ocnn.CreateCommand();
……

------解决方案--------------------
楼上几位说的没错,楼主绑定数据源以后,设定一下DisplayMember ValueMember这两个属性就OK了