C#winform listbox多选的取值,采用DataRowView
C#winform listbox多选的取值,C# listbox取值
(特注:listBox的item类型是System.Data.DataRowView而不是ListItem)
foreach(System.Data.DataRowView row in listBox1.SelectedItems)
{
planIDArr += row["planID"].ToString()+",";
//if(item.Selected==true)
//{
// planIDArr=item.Value+",";
//}
}
DataTable dtbl = new DataTable();
dtbl = ...
实现第一种方法
添加:
foreach( System.Data.DataRowView row in dtbl.Rows )
{
this.listBox1.Items.Add( row["SymptomID"].ToString() );
}
选择:
for( int i=0; i < this.listBox1.SelectedItems.Count; i++ )
{
MessageBox.Show( this.listBox1.SelectedItems[i].ToString() );
}
实现第二种方法
绑定:
this.listBox1.DataSource = dtbl;
this.listBox1.DisplayMember = "SymptomName";
this.listBox1.ValueMember = "SymptomID";
选择:
for( int i=0; i< this.listBox1.SelectedIndices.Count; i++ )
{
MessageBox.Show(((DataRowView)(this.listBox1.SelectedItems[i])).Row["SymptomID"].ToString());
}