listView绑定数据
private void button1_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
listView1.Columns.Clear();
SqlConnection con = new SqlConnection("server=.;database=pubs;user id=sa;password=123");
con.Open();
DataSet dst = new DataSet();
SqlCommand cmd =new SqlCommand ("select title_id,type from titles",con );
SqlDataAdapter dap = new SqlDataAdapter(cmd);
dap.Fill(dst);
con.Close();
this.listView1.Columns.Add("ID编号", 80, HorizontalAlignment.Center);
this.listView1.Columns.Add("类型", 80, HorizontalAlignment.Center);//增加列标题
listView1.View = View.Details;
listView1.GridLines = true;//显示网格线。。
string[] str = new string[2];
foreach (DataRow row in dst.Tables[0].Rows)
{
str[0] = row["title_id"].ToString();
str[1] = row["type"].ToString();
ListViewItem item = new ListViewItem(str, 0);
listView1.Items.Add(item);
}
}