
因为List集合中已添加两个对象userA,userB
所以:
//向ListView中添加数据
foreach(ClsUser var in list)
{
ListViewItem lvi = new ListViewItem(var.UserId.ToString())
lvi.SubItems.Add(var.UserName.ToString())
listView1.Items.Add(lvi)
}
//获取选中项
{
textBox1.Text = listView1.SelectedItems[0].Text.ToString()
textBox2.Text = listView1.SelectedItems[0].SubItems[1].Text.ToString()
}
ListBox控件显示较长的选项列表,用户可从中选择一项或多项。如果项总数超出可以显示的项数,则自动向ListBox控件添加滚动条。ListBox控件列表中的每个元素称为项。图1所示为ListBox控件
功能描述:
ListBox控件显示较长的选项列表,用户可从中选择一项或多项。如果项总数超出可以显示的项数,则自动向ListBox控件添加滚动条。ListBox控件列表中的每个元素称为项,
说明:
① 该属性使用户可以获取对当前存储在 ListBox 中的项列表的引用。通过此引用,可以在集合中添加项、移除项和获得项的计数。
② 可以使用DataSource属性来 *** 控ListBox的项。如果使用DataSource属性向ListBox添加项,则可以使用Items属性查看ListBox中的项,但不能使用ListBox.ObjectCollection的方法向该列表添加项或从中移除项。
C# 代码如下:
SqlConnection con = new SqlConnection("server=12uid=sapwd=database=test")
con.Open()
SqlCommand com = new SqlCommand("select * from table",con)
SqlDataReader dr = com.ExecuteReader()
this.listBox1.Items.Clear()
while (dr.Read())
{
// this.listBox1.Items.Add(dr[0].ToString())
this.listBox1.Items.Add(dr[1].ToString())
// this.listBox1.Items.Add(dr[2].ToString())
}
dr.Close()
con.Close()
首先使用listBox1.Items.Clear()清空控件原有数据。
然后使用 listBox1.Items.Add方法逐项添加数据。
代码放入窗体Load事件中。
外面开始的时候加上DataTable dt=null
然后//这里
myDataReader.Fill(dt)
if(dt!=null&&dt.rows.count>0)
{
for(int i=i<dt.rows.counti++)
{
listBox1.items.add(dt.rows[0].ToString())
}
}
ListBox控件显示较长的选项列表,用户可从中选择一项或多项。如果项总数超出可以显示的项数,则自动向ListBox控件添加滚动条。ListBox控件列表中的每个元素称为项。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)