首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

int code=Convert.ToInt32(this.listBox1.SelectedValue.ToString());listbox取值异常,报格

2012-01-06 
intcodeConvert.ToInt32(this.listBox1.SelectedValue.ToString())listbox取值错误,报格式不对!!求助pri

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());

热点排行