winform中ListView绑定问题 15
最简单的绑定为什么我绑定后数据总是不对代码如下listView1.GridLines=true;listView1.FullRowSelect=true;listView...
最简单的绑定 为什么我绑定后数据总是不对
代码如下
listView1.GridLines = true;
listView1.FullRowSelect = true;
listView1.View = View.Details;
listView1.Scrollable = true;
listView1.MultiSelect = false;
listView1.HeaderStyle = ColumnHeaderStyle.Clickable;
SqlDataAdapter sda = new SqlDataAdapter("select * from Student_Basic", cn);
listView1.Columns.Add("学号",50,HorizontalAlignment.Center);
listView1.Columns.Add("姓名", 50, HorizontalAlignment.Center);
listView1.Columns.Add("性别", 50, HorizontalAlignment.Center);
listView1.Columns.Add("班级", 50, HorizontalAlignment.Center);
listView1.Columns.Add("专业", 150, HorizontalAlignment.Center);
listView1.Columns.Add("职业", 150, HorizontalAlignment.Center);
listView1.Columns.Add("城市", 150, HorizontalAlignment.Center);
DataTable dt = new DataTable();
sda.Fill(dt);
for (int i = 0; i < dt.Rows.Count;i++ )
{
ListViewItem itme = new ListViewItem();
itme.SubItems.Clear();
itme.SubItems.Add(dt.Rows[i][0].ToString());
itme.SubItems.Add(dt.Rows[i][1].ToString());
itme.SubItems.Add(dt.Rows[i][2].ToString());
itme.SubItems.Add(dt.Rows[i][4].ToString());
itme.SubItems.Add(dt.Rows[i][6].ToString());
itme.SubItems.Add(dt.Rows[i][8].ToString());
itme.SubItems.Add(dt.Rows[i][13].ToString());
listView1.Items.Add(itme);
} 展开
代码如下
listView1.GridLines = true;
listView1.FullRowSelect = true;
listView1.View = View.Details;
listView1.Scrollable = true;
listView1.MultiSelect = false;
listView1.HeaderStyle = ColumnHeaderStyle.Clickable;
SqlDataAdapter sda = new SqlDataAdapter("select * from Student_Basic", cn);
listView1.Columns.Add("学号",50,HorizontalAlignment.Center);
listView1.Columns.Add("姓名", 50, HorizontalAlignment.Center);
listView1.Columns.Add("性别", 50, HorizontalAlignment.Center);
listView1.Columns.Add("班级", 50, HorizontalAlignment.Center);
listView1.Columns.Add("专业", 150, HorizontalAlignment.Center);
listView1.Columns.Add("职业", 150, HorizontalAlignment.Center);
listView1.Columns.Add("城市", 150, HorizontalAlignment.Center);
DataTable dt = new DataTable();
sda.Fill(dt);
for (int i = 0; i < dt.Rows.Count;i++ )
{
ListViewItem itme = new ListViewItem();
itme.SubItems.Clear();
itme.SubItems.Add(dt.Rows[i][0].ToString());
itme.SubItems.Add(dt.Rows[i][1].ToString());
itme.SubItems.Add(dt.Rows[i][2].ToString());
itme.SubItems.Add(dt.Rows[i][4].ToString());
itme.SubItems.Add(dt.Rows[i][6].ToString());
itme.SubItems.Add(dt.Rows[i][8].ToString());
itme.SubItems.Add(dt.Rows[i][13].ToString());
listView1.Items.Add(itme);
} 展开
2个回答
展开全部
应该指出错在哪里?这样代码好像没问题。。itme.SubItems.Add(dt.Rows[i][13].ToString());把序号改为确定的字段吧,数据多时考虑用foreach不要for了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询