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);
}
}
{
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);
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询