当前位置:文档之家› ListView控件及combox绑定数据的使用

ListView控件及combox绑定数据的使用

using System;
using System.Collections.Generic;
using https://www.doczj.com/doc/e93599586.html,ponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;

namespace 北京中学学籍管理系统
{
public partial class frmStudent : Form
{
public frmStudent()
{
InitializeComponent();
}
//加载数据到listview的方法
public void NewLoad(string sql1)
{
SqlCommand cmd = new SqlCommand(sql1, DBHelper.con);
DBHelper.con.Open();

SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
ListViewItem item = new ListViewItem();
item.Text = dr[0].ToString();
for (int i = 1; i < dr.FieldCount; i++)
{

item.SubItems.AddRange(new string[] { dr[i].ToString() });

}
listView1.Items.Add(item);
}
dr.Close();
DBHelper.con.Close();
}
//窗体加载事件
private void frmStudent_Load(object sender, EventArgs e)
{
string sql = "select s.studentId,c.classname,https://www.doczj.com/doc/e93599586.html,,s.sex,s.saddress from student as s inner join class as c on s.classId=c.classID";
NewLoad(sql);

string sql1 = "select classname from class";
SqlCommand cmd1 = new SqlCommand(sql1, DBHelper.con);
DBHelper.con.Open();
SqlDataReader dr1 = cmd1.ExecuteReader();
while (dr1.Read())
{
comboBox1.Items.Add(dr1[0]);
}
dr1.Close();
DBHelper.con.Close();

}
//单击查询按钮事件
private void button1_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
if (comboBox1.Text=="")
{
string sql = string.Format("select s.studentId,c.classname,https://www.doczj.com/doc/e93599586.html,,s.sex,s.saddress from student as s inner join class as c on s.classId=c.classID where https://www.doczj.com/doc/e93599586.html, like'%{0}%'", textBox1.Text.Trim());
NewLoad(sql);
}
else
{
listView1.Items.Clear();
string sql = string.Format("select s.studentId,c.classname,https://www.doczj.com/doc/e93599586.html,,s.sex,s.saddress from student as s inner join class as c on s.classId=c.classID where https://www.doczj.com/doc/e93599586.html, like'%{0}%' and c.classname='{1}'",textBox1.Text.Trim(),https://www.doczj.com/doc/e93599586.html,boBox1.Text);
NewLoad(sql);
}
}
//退出按钮
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
//combox值改变所引发的事件
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
listView1.Items.Clear();
string sql = string.Format("selec

t s.studentId,c.classname,https://www.doczj.com/doc/e93599586.html,,s.sex,s.saddress from student as s inner join class as c on s.classId=c.classID where ClassName='{0}'", https://www.doczj.com/doc/e93599586.html,boBox1.Text);
NewLoad(sql);
}
}
}




注意:还需创建一个DBHelper类;listview的View属性应该选择Details

相关主题
文本预览
相关文档 最新文档