數據篩選和排序------的解析
一.TreeView
SelectedNode 選中的節點
Level 節點的深度(從0開始)
AfterSelect 節點選中後發生
1.獲取節點深度
this.tvlist.SelectNode.Level
2.獲取節點名稱
this.tvlist.SelectNode.Text
二.使用DataView篩選和排序數據
RowFilter指定篩選條件
Sort指定排序方式
//窗體運行時默認選中全部
if (this.tvstulist.SelectedNode.Level == 0) {
ShowStudent();
}
else if (this.tvstulist.SelectedNode.Level == 1) {
DataView dv = new DataView(ds.Tables["Student"]);
dv.RowFilter = "GradeName=‘"+this.tvstulist.SelectedNode.Text+"‘";
this.dgvstulist.DataSource = dv;
}
else if (this.tvstulist.SelectedNode.Level == 2) {
DataView dv = new DataView(ds.Tables["Student"]);
dv.RowFilter = "GradeName=‘" + this.tvstulist.SelectedNode.Parent.Text + "‘ and Sex=‘" + this.tvstulist.SelectedNode.Text + "‘";
dv.Sort = "StudentNo desc";
this.dgvstulist.DataSource=dv;
}
三.刪除數據
SelectionMode指定選中DataGradeView的選中方式
DialogResult result= MessageBox.Show("是否刪除","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Information);
if (result == DialogResult.Yes) {
//獲取選中的學號的值
int studentno=(int)this.dgvstulist.SelectedRows[0].Cells["StudentNo"].Value;
try
{
helper.OpenConnection();
string sql = "delete from Student where StudentNo=‘"+studentno+"‘";
SqlCommand cmd = new SqlCommand(sql,helper.Con);
int count=cmd.ExecuteNonQuery();
if (count > 0)
{
MessageBox.Show("刪除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
ShowStudent();
}
else {
MessageBox.Show("刪除失敗!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception x)
{
MessageBox.Show(x.Message);
}
}
數據篩選和排序------的解析