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

int code=Convert.ToInt32(this.listBox1.SelectedValue.ToString());listbox取值错误,报格式不对!!求助
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();
//
}

------解决方案--------------------
try

DataRowView rowView = (DataRowView)listBox1.SelectedItem;
int code = Convert.ToInt32(rowView.Row[ "Code "]);
------解决方案--------------------
SelectedIndexChanged发生在SelectedValue付值之前;

意思是你发生这个事件的时候,SelectedValue还是为空(Null),或上一次选择的值;

使用SelectedIndex或SelectedItem试试

int code=Convert.ToInt32(this.listBox1[listBox1.SelectedIndex].ToString());