本项目主要让学生学习"TREEVIEW的显示排序"和"LISTVIEW的(分组)显示和查找"
(一)界面的设计
数据库表设计
输入相关数据
(二)实现TREEVIEW里学生的排序
//自定义访问数据库表的方法GetView
private DataView GetView(string connString, string SQLString, string tableName)
{
DataSet ds = new DataSet();
SqlDataAdapter sqlDA = new SqlDataAdapter(SQLString, new SqlConnection(connString));
sqlDA.Fill(ds, tableName);
return ds.Tables[tableName].DefaultView;
}
//在窗体类中定义数据视图类对象dvStudent,准备存放数据
DataView dvStudent;
//自定义GenStudentView用以通过调用GetView方法将返回的数据存放在dvStudent中
private void GenStudentView()
{
dvStudent = GetView(@"Data Source=./SQLEXPRESS;AttachDbFilename=|DataDirectory|/DBStudent.mdf;Integrated Security=True;User Instance=True", "select * from StudentBase", "StudentBase");
}
//自定义ShowTree方法显示树
private void ShowTree(int choose)
{
TreeNode rootNode;
TreeNode newNode;
tvwStud.ShowNodeToolTips = true;
tvwStud.ShowPlusMinus = true;
tvwStud.Nodes.Clear();
rootNode = tvwStud.Nodes.Add("-1", "学生基本信息");
rootNode.ImageIndex = 0;
switch (choose)
{
case 0:
dvStudent.Sort = "";
break;
case 1:
dvStudent.Sort = "sex Asc,no Asc";
break;
case 2:
dvStudent.Sort = "sex Asc,no DESC";
break;
}
foreach (DataRowView drv in dvStudent)
{
newNode = rootNode.Nodes.Add(drv["no"].ToString()+" "+drv["name"].ToString());
newNode.Tag = drv;
newNode.ImageIndex = ((bool)drv["sex"]) ? 1 : 2;
newNode.SelectedImageIndex = ((bool)drv["sex"]) ? 1 : 2;
}
tvwStud.ExpandAll();
}
//在FrmMain_Load方法中调用GenStudentView方法和ShowTree方法
GenStudentView();
ShowTree(0);
//按钮的CLICK事件方法
private void btnAsc_Click(object sender, EventArgs e)
{
ShowTree(1);
}
private void btnDesc_Click(object sender, EventArgs e)
{
ShowTree(2);
}
private void btnNormal_Click(object sender, EventArgs e)
{
ShowTree(0);
}
(三)用LISTVIEW显示学生成绩
//在窗体类中定义数据视图类对象dvStudent,准备存放数据
DataView dvScores;
//自定义GenScoresView用以通过调用GetView方法将返回的数据存放在dvScores中
private void GenScoresView()
{
dvScores = GetView(@"Data Source=./SQLEXPRESS;AttachDbFilename=|DataDirectory|/DBStudent.mdf;Integrated Security=True;User Instance=True", "select * from Scores", "Scores");
}
//tvwStud_AfterSelect事件处理方法显示列表视图
private void tvwStud_AfterSelect(object sender, TreeViewEventArgs e)
{
txbName.Text = string.Empty;
lvStu.Clear();
lvStu.Columns.Add("考试名称", 120);
lvStu.Columns.Add("语文");
lvStu.Columns.Add("数学");
lvStu.Columns.Add("外语");
if (e.Node.Parent == null)
{
dvScores.RowFilter = "";
foreach (DataRowView drv in dvScores)
{
if (!lvStu.Groups.Contains(new ListViewGroup(drv["no"].ToString())))
{
ListViewGroup lvg = new ListViewGroup(string.Format("{0}", drv["no"].ToString()), HorizontalAlignment.Center);
lvStu.Groups.Add(lvg);
lvg.Name = drv["no"].ToString();
}
ListViewItem lvi = new ListViewItem(drv["TestName"].ToString(), 0);
lvi.UseItemStyleForSubItems = false; //
lvi.BackColor = Color.WhiteSmoke;
ListViewItem.ListViewSubItem lvsi=lvi.SubItems.Add(drv["Chinese"].ToString());
lvsi.ForeColor = Color.RoyalBlue;
lvi.SubItems.Add(drv["Maths"].ToString());
lvi.SubItems.Add(drv["English"].ToString());
lvi.ToolTipText = string.Format("语文:{0} 数学:{1} 外语:{2}", drv["Chinese"].ToString(), drv["Maths"].ToString(), drv["English"].ToString());
lvi.Group = lvStu.Groups[drv["no"].ToString()];
lvStu.Items.Add(lvi);
}
}
else
{
dvScores.RowFilter = "no='" + ((DataRowView)e.Node.Tag)["no"] + "'";
lvStu.Groups.Clear();
foreach (DataRowView drv in dvScores)
{
ListViewItem lvi = new ListViewItem(drv["TestName"].ToString(), 0);
lvi.SubItems.Add(drv["Chinese"].ToString());
lvi.SubItems.Add(drv["Maths"].ToString());
lvi.SubItems.Add(drv["English"].ToString());
lvi.ToolTipText = string.Format("语文:{0} 数学:{1} 外语:{2}", drv["Chinese"].ToString(), drv["Maths"].ToString(), drv["English"].ToString());
lvStu.Items.Add(lvi);
}
}
}
//在FrmMain_Load方法中设置相关属性
lvStu.View = View.Details;
lvStu.SmallImageList = ilSmall;
lvStu.LargeImageList = ilLarge;
//拖放上下文菜单
private void 大图标ToolStripMenuItem_Click(object sender, EventArgs e)
{
lvStu.View = View.LargeIcon;
}
private void 详细信息ToolStripMenuItem_Click(object sender, EventArgs e)
{
lvStu.View = View.Details;
}
private void 小图标ToolStripMenuItem_Click(object sender, EventArgs e)
{
lvStu.View = View.SmallIcon;
}
private void 列表ToolStripMenuItem_Click(object sender, EventArgs e)
{
lvStu.View = View.List;
}
private void 平铺ToolStripMenuItem_Click(object sender, EventArgs e)
{
lvStu.View = View.Tile;
}
(四)查询LISTVIEW中相关信息
//在窗体类中自定义MyField类
class MyField
{
string engName;
string chnName;
public MyField(string eName,string cName)
{
engName = eName;
chnName = cName;
}
public string EngName
{
get
{
return engName;
}
}
public string ChnName
{
get
{
return chnName;
}
}
}
//自定义显示查询字段名称
private void ShowCombox()
{
List<MyField> myList = new List<MyField>();
myList.Add(new MyField("Chinese", "语文"));
myList.Add(new MyField("Maths", "数学"));
myList.Add(new MyField("English", "外语"));
myList.Add(new MyField("TestName", "考试类型"));
combName.Items.Clear();
combName.DataSource = myList;
combName.DisplayMember="chnName";
combName.ValueMember = "engName";
combName.SelectedIndex = 0;
}
//自定义显示查询运算符
private void ShowCompare()
{
combCompare.Items.Clear();
combCompare.Items.Add("=");
combCompare.Items.Add("<>");
combCompare.Items.Add(">");
combCompare.Items.Add(">=");
combCompare.Items.Add("<");
combCompare.Items.Add("<=");
combCompare.SelectedIndex = 0;
}
//自定义显示查询运算符2
private void ShowCompare2()
{
combCompare.Items.Clear();
combCompare.Items.Add("like");
combCompare.Items.Add("not like");
combCompare.SelectedIndex = 0;
}
//当查询字段名称改变时的事件处理方法
private void combName_SelectedIndexChanged(object sender, EventArgs e)
{
if (combName.SelectedIndex == combName.Items.Count - 1)
ShowCompare2();
else
ShowCompare();
}
//查找按钮的CLICK事件处理方法
private void btnSearch_Click(object sender, EventArgs e)
{
if (txbName.Text == string.Empty)
{
MessageBox.Show("请输入一个条件");
txbName.Focus();
return;
}
lvStu.Clear();
lvStu.Columns.Add("考试名称", 120);
lvStu.Columns.Add("语文");
lvStu.Columns.Add("数学");
lvStu.Columns.Add("外语");
if (combName.SelectedIndex == combName.Items.Count - 1)
{
dvScores.RowFilter = combName.SelectedValue + " " + combCompare.SelectedItem.ToString() + " '%" + txbName.Text.Trim() + "%'";
}
else
{
dvScores.RowFilter = combName.SelectedValue + combCompare.SelectedItem.ToString() + txbName.Text.Trim();
}
foreach (DataRowView drv in dvScores)
{
if (!lvStu.Groups.Contains(new ListViewGroup(drv["no"].ToString())))
{
ListViewGroup lvg = new ListViewGroup(string.Format("{0}", drv["no"].ToString()), HorizontalAlignment.Center);
lvStu.Groups.Add(lvg);
lvg.Name = drv["no"].ToString();
}
ListViewItem lvi = new ListViewItem(drv["TestName"].ToString(), 0);
lvi.UseItemStyleForSubItems = false; //
lvi.BackColor = Color.WhiteSmoke;
ListViewItem.ListViewSubItem lvsi = lvi.SubItems.Add(drv["Chinese"].ToString());
lvsi.ForeColor = Color.RoyalBlue;
lvi.SubItems.Add(drv["Maths"].ToString());
lvi.SubItems.Add(drv["English"].ToString());
lvi.ToolTipText = string.Format("语文:{0} 数学:{1} 外语:{2}", drv["Chinese"].ToString(), drv["Maths"].ToString(), drv["English"].ToString());
lvi.Group = lvStu.Groups[drv["no"].ToString()];
lvStu.Items.Add(lvi);
}
}
(五)运行效果图: