在列表建立属性和访问数据库时,对应好每一字段,就不会有问题了
public void InitListView()
{
listView.View = View.Details;
listView.GridLines = true;
listView.FullRowSelect = true;
ColumnHeader header1 = new ColumnHeader();
ColumnHeader header2 = new ColumnHeader();
ColumnHeader header3 = new ColumnHeader();
ColumnHeader header4 = new ColumnHeader();
ColumnHeader header5 = new ColumnHeader();
header1.Text = "编号";
header2.Text = "姓名";
header3.Text = "年龄";
header4.Text = "工号";
header5.Text = "体检时间";
header1.Width = 125;
header2.Width = 125;
header3.Width = 125;
header4.Width = 125;
header5.Width = 125;
listView.Columns.Add(header1);
listView.Columns.Add(header2);
listView.Columns.Add(header3);
listView.Columns.Add(header4);
listView.Columns.Add(header5);
}
public void Query()
{
String sql = "select * from 体检信息"
using (SqlDataReader reader = db.GetDataReader(sql))
{
while (reader.Read())
{
ListViewItem lv = new ListViewItem(Convert.ToString(reader["编号"]));
lv.SubItems.Add(Convert.ToString(reader["姓名"]));
lv.SubItems.Add(Convert.ToString(reader["年龄"]));
lv.SubItems.Add(Convert.ToString(reader["工号"]));
lv.SubItems.Add(Convert.ToString(reader["体检时间"]));
listView.Items.Add(lv);
}
}
}
//该事件是选中列表某项
private void listView_SelectedIndexChanged(object sender, EventArgs e)
{
isModify = true;//修改状态
if (listView.SelectedItems.Count != 0)
{
ListViewItem MyItem = listView.SelectedItems[0];
m_strNum = listView.Items[MyItem.Index].Text;//被选行的编号
}
}
不会不同的,你需要把列和数据库字段绑定好,就可以了
查询数据库中表名的语句为
SELECT name FROM sysobjects WHERE (xtype = 'U')
sysobjects 这是系统表的表名
再有DbNameDs更本没有添加数据。