前期准备1
2019-04-01 本文已影响0人
czm123
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
void initLV(ListView l)
{
l.MultiSelect = false;
l.GridLines = true;
l.FullRowSelect = true;
l.View = View.Details;
}
void loadLV(ListView l,string sql)
{
try
{
con.Open();
SqlDataAdapter da=new SqlDataAdapter(sql,con);
DataTable dt=new DataTable();
da.Fill(dt);
l.Items.Clear();
l.Columns.Clear();
int i,j;
for (i = 0; i < dt.Columns.Count; i++)
l.Columns.Add(dt.Columns[i].Caption,(l.Width-4)/dt.Columns.Count);
for (i = 0; i < dt.Rows.Count; i++)
{
ListViewItem li = new ListViewItem();
li.SubItems[0].Text=dt.Rows[i][0].ToString().Trim();
for(j=1;j<dt.Columns.Count;j++)
li.SubItems.Add(dt.Rows[i][j].ToString().Trim());
l.Items.Add(li);
}
}
catch { }
finally { con.Close(); }
}
SqlConnection con = new SqlConnection();
void loadCB(ComboBox c,string sql)
{
try
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = sql;
SqlDataReader rd = cmd.ExecuteReader();
c.Items.Clear();
while (rd.Read())
{
string s = rd.GetValue(0).ToString().Trim() ;
if (s != "") c.Items.Add(s);
}
}
catch { }
finally { con.Close();}
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
con.ConnectionString =@"server=8L6CIZNUU7R8XA7\SQLEXPRESS;database=2012;integrated security=true";
}
catch { }
initLV(listView1);
initLV(listView2);
loadLV(listView1,"select wnum as 员工号,wname as 员工名,wplace as 所在地 from workers");
loadCB(comboBox1,"select wname from workers group by wname");
loadCB(comboBox2,"select cname from company group by cname");
//细节
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Text = "员工工作管理系统";
textBox1.MaxLength = 3;
textBox2.MaxLength = 10;
textBox3.MaxLength = 10;
}