现在用了listbox绑定了一个数据源,代码如下:
                lst_DepartCars.DataSource = ds.Tables[0];
                lst_DepartCars.DisplayMember = "carsnumber";
                lst_DepartCars.ValueMember = "id";
  最初始化的时候绑定了,但后面用lst_DepartCars.SelectedItem;访问得到的全是:
System.Data.DataRowView,郁闷死了,用lst_DepartCars.Items[]得到的值也是一样,
  但是这样添加值 
               for(int i=0;i<ds.Tables[0].Rows.Count;i++)
                {
                    lst_DepartCars.Items.Add((object)ds.Tables[0].Rows[i]["carsnumber"]);
                }
就能正确访问,请用过的指点一下

解决方案 »

  1.   

    应该是 你的字段名不对吧,绑定之后最好在调用一下DataBinding()
      

  2.   

    字段名正确,能在ListBox中正常显示出来,没有DataBinding()方法
    SelectValue能显示出绑定的“id”,但相应的名称怎么得不到
    MessageBox.Show(lst_DepartCars.SelectedValue.ToString());//显示相应的数值
    MessageBox.Show(lst_DepartCars.SelectedItem.ToString());//显示System.Data.DataRowView
    郁闷,
      

  3.   

    lst_DepartCars.DisplayMember = "carsnumber";  ————————————————————————应该是这里字段名错了
      

  4.   

    搞定了,搜了好久啊,WinForm的ListBox绑定太麻烦了,还是web简单
    搞定方法:Sample code as follows:
    for( int i = 0; i < yourListBox.Items.Count; i++ )
    {
         DataRowView drv = yourListBox.Items[i] as DataRowView;
         if( drv != null )
         {
               MessageBox.Show( "Text:" + drv[yourListBox.DisplayMember].ToString() );
               MessageBox.Show( "Value:" + drv[yourListBox.ValueMember].ToString() );
         }
    }
    参考地址:http://community.csdn.net/Expert/topic/4836/4836613.xml?temp=.8181879
    Knight94(愚翁) 给的答案,五星的就是不一样,呵呵