这是我的一个使用listView的列子,,你看看string sql = string.Format("select id,nickName,sex,age,username from users order by id");
try
{
SqlCommand cmd = new SqlCommand(sql, DBHelper.con);
DBHelper.con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string id = dr["id"].ToString();
string nickName = dr["nickName"].ToString();
string sex = dr["sex"].ToString();
string age = dr["age"].ToString();
string name = dr["username"].ToString();
ListViewItem lvi = new ListViewItem(); //首先创建一个ListView项item
lvi.Text = id; //该项的文本
lvi.SubItems.AddRange(new string[] { nickName, sex, age, name }); //添加该项的子项,,也就是第2,3,4列的值
lvUsers.Items.Add(lvi); //最后把创建的项加到ListView控件的项中
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
throw;
}
finally
{
DBHelper.con.Close();
}