using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace ListView
{
public partial class Form1 : Form
{
private static string SQL = "Data Source=IBM-PC;Initial Catalog=OK;Integrated Security=True";
public static SqlConnection sc = new SqlConnection(SQL); public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
string sql = @"SELECT ee, aa, bb, cc, dd FROM meinv";
SqlCommand cm = new SqlCommand(sql, sc);
sc.Open();
SqlDataReader dr;
dr = cm.ExecuteReader();
while (dr.Read()) { ListViewItem it = new ListViewItem(dr["ee"].ToString());
lsv.Items.Add(it);
it.SubItems.AddRange(new string[] { dr["aa"].ToString(), dr["bb"].ToString(), dr["cc"].ToString(), dr["dd"].ToString() }); } dr.Close(); } private void lsv_SelectedIndexChanged(object sender, EventArgs e)
{ }
}
}
运行出来后listView空间的第一行是空的,每个单元格都是空的,从第二行开始加载数据。
为什么呢?
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace ListView
{
public partial class Form1 : Form
{
private static string SQL = "Data Source=IBM-PC;Initial Catalog=OK;Integrated Security=True";
public static SqlConnection sc = new SqlConnection(SQL); public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
string sql = @"SELECT ee, aa, bb, cc, dd FROM meinv";
SqlCommand cm = new SqlCommand(sql, sc);
sc.Open();
SqlDataReader dr;
dr = cm.ExecuteReader();
while (dr.Read()) { ListViewItem it = new ListViewItem(dr["ee"].ToString());
lsv.Items.Add(it);
it.SubItems.AddRange(new string[] { dr["aa"].ToString(), dr["bb"].ToString(), dr["cc"].ToString(), dr["dd"].ToString() }); } dr.Close(); } private void lsv_SelectedIndexChanged(object sender, EventArgs e)
{ }
}
}
运行出来后listView空间的第一行是空的,每个单元格都是空的,从第二行开始加载数据。
为什么呢?
item.Text = "";
item.SubItems.Add( "");
this.listView1.Items.Add(item);