简体   繁体   中英

Listbox: Event SelectedIndexChanged called when add one item

I have this code behind a winforms which simply has a listbox as its only control:

private void Form1_Load(object sender, EventArgs e)
{
listBox1.DataSource = dtv;
listBox1.DisplayMember = "Name";
listBox1.ValueMember = "IDName";
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DataSet dss = UseDatabase.FillDataSet("Select * From Table Where IDName=" + listBox1.SelectedValue);
string st = dss.Tables[0].Rows[0][0].ToString();
MessageBox.Show(st);
}

Run it say: "Additional information: There is no row at position 0." Debug I see Event SelectedIndexChanged called when add one item. Why user have not select item, that event is called And how to fix this?

据我所知,解决此问题的唯一方法是在对事件处理程序方法执行任何其他操作之前,检查SelectedIndex!= -1。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM