private  void  ListBind()
{
string  str="select Code,Name from UserInfo ";
     DataSet  ds=ACConn.DataSet(str);
  this.listBox1.DataSource=ds.Tables[0].DefaultView;
 this.listBox1.DisplayMember="Name";
this.listBox1.ValueMember= "Code";


} private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
 
 

int  code=Convert.ToInt32(this.listBox1.SelectedValue.ToString());
User  per=new User(code);
this.textBox1.Text=per.Name.ToString();
this.textBox2.Text=per.Code.ToString();
this.textBox3.Text=per.Password.ToString();
//
}

解决方案 »

  1.   

    this.listBox1.SelectedValue是什么类型的   DEbug看下就行了
      

  2.   

    this.listBox1.SelectedValue不是数字字符串
      

  3.   

    SelectedValue返回的是Code这一列的值 检查一下这列是不是有格式不是整数的
      

  4.   

    是datarowview  ,怎么改啊,急
      

  5.   

    tryDataRowView rowView = (DataRowView)listBox1.SelectedItem;
    int code = Convert.ToInt32(rowView.Row["Code"]);
      

  6.   

    SelectedIndexChanged发生在SelectedValue付值之前;意思是你发生这个事件的时候,SelectedValue还是为空(Null),或上一次选择的值;使用SelectedIndex或SelectedItem试试int  code=Convert.ToInt32(this.listBox1[listBox1.SelectedIndex].ToString());
      

  7.   

    int     code=Convert.ToInt32(this.listBox1.SelectItem[listBox1.SelectedIndex].ToString());小哥来补充一下答案
      

  8.   

    int    code=Convert.ToInt32(this.listBox1.SelectItems[listBox1.SelectedIndex].ToString()); 
     这个都是正确的,嘿嘿
      

  9.   

    我想问一下:怎样通过一个单击事件打开你的资源管理器,选择一个文件,而这个文件的路径传到listbox的Items中?