数据填充:
private void btnGetCardNo_Click(object sender, EventArgs e)
{string SQLString = "select empid,empname,cardno from employee where cardno is not null and cardno !=''";
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
DataTable dt = ds.Tables[0];
if (dt.Rows.Count > 0)
{
new Thread((ThreadStart)(delegate()
{
foreach (DataRow dr in dt.Rows)
{
listView1.Invoke((MethodInvoker)delegate()
{
listView1.Items.Add(dr["empid"].ToString()).SubItems.AddRange(new string[] { dr["empname"].ToString(), dr["cardno"].ToString() });
});
lblCountSum.Invoke((MethodInvoker)delegate()
{
this.lblCountSum.Text = listView1.Items.Count.ToString();
});
}
}))
.Start();
}
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
}
}
解决闪屏:
class DoubleBufferListView : ListView
{
public DoubleBufferListView()
{
SetStyle(ControlStyles.DoubleBuffer |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint, true);
UpdateStyles();
}
}
designer.cs 内声明 private DoubleBufferListView listView1;
this.listView1 = new DoubleBufferListView();