ASP.NET中实现DataGrid数据排序(4)
点击次数:25 次 发布日期:2008-11-26 14:32:51 作者:源代码网
|
源代码网推荐 源代码网推荐 使用Sql语句来实现对DataGrid组件中数据排序,在操作步骤上虽然相对复杂一点,但功能相对强大一点。其主要思路就是根据DataGrid组件的不同列名,形成不同的Sql语句,从而得到不同的DataSet实例,来实现对DataGrid中数据进行相应排序。下面就在上面完成的【在ASP.net页面中使用DataView实现DataGrid数据排序】项目基础上,加以修改从而完成使用Sql语句来实现DataGrid中数据排序。 源代码网推荐 源代码网推荐 1. 首先假设您已经成功完成上面项目,能够在ASP.NET使用DataView实现对DataGrid中数据进行排序。 源代码网推荐 源代码网推荐 2. 把Visual Stuido .Net的当前窗口切换到WebForm1.aspx.cs中,并WebForm1.aspx.cs文件的class代码区添加下列代码,下列代码是创建全局使用的实例: 源代码网推荐 源代码网推荐 public DataSet dataSet1 ; 源代码网推荐 public SqlDataAdapter sqlDataAdapter1 ; 源代码网推荐 源代码网推荐 3. 用下列代码替换WebForm1.aspx.cs中已经定义的sort过程,下面代码是重新定义sort过程,使其能够使用Sql语句实现对DataGrid中的数据进行排序: 源代码网推荐 源代码网推荐 private void Sort ( string sortString ) 源代码网推荐 { 源代码网推荐 SqlConnection sqlConnection1 = new SqlConnection ( "Server = localhost ; Database = NorthWind ; User ID = sa ; Password = ; " ) ; 源代码网推荐 switch ( sortString ) 源代码网推荐 { 源代码网推荐 case "序号" : 源代码网推荐 if ( blId ) 源代码网推荐 { 源代码网推荐 sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter ( "SELECT EmployeeID as 序号, LastName as 姓氏 , FirstName as 名字 , Title as 职务 , Birthdate as 生日 From Employees ORDER BY 序号 ASC" , sqlConnection1 ) ; 源代码网推荐 //以定义的数据库连接来初始化SqlDataAdapter实例 源代码网推荐 blId = false ; 源代码网推荐 } 源代码网推荐 else 源代码网推荐 { 源代码网推荐 sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter ( "SELECT EmployeeID as 序号, LastName as 姓氏 , FirstName as 名字 , Title as 职务 , Birthdate as 生日 From Employees ORDER BY 序号 DESC" , sqlConnection1 ) ; 源代码网推荐 //以定义的数据库连接来初始化SqlDataAdapter实例 源代码网推荐 blId = true ; 源代码网推荐 } 源代码网推荐 break ; 源代码网推荐 case "姓氏" : 源代码网推荐 if ( blLast ) 源代码网推荐 { 源代码网推荐 sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter ( "SELECT EmployeeID as 序号, LastName as 姓氏 , FirstName as 名字 , Title as 职务 , Birthdate as 生日 From Employees ORDER BY 姓氏 ASC" , sqlConnection1 ) ; 源代码网推荐 //以定义的数据库连接来初始化SqlDataAdapter实例 源代码网推荐 blLast = false ; 源代码网推荐 } 源代码网推荐 else 源代码网推荐 { 源代码网推荐 sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter ( "SELECT EmployeeID as 序号, LastName as 姓氏 , FirstName as 名字 , Title as 职务 , Birthdate as 生日 From Employees ORDER BY 姓氏 DESC" , sqlConnection1 ) ; 源代码网推荐 //以定义的数据库连接来初始化SqlDataAdapter实例 源代码网推荐 blLast = true ; 源代码网推荐 } 源代码网推荐 break ; 源代码网推荐 case "名字" : 源代码网推荐 if ( blFirst ) 源代码网推荐 { 源代码网推荐 sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter ( "SELECT EmployeeID as 序号, LastName as 姓氏 , FirstName as 名字 , Title as 职务 , Birthdate as 生日 From Employees ORDER BY 名字 ASC" , sqlConnection1 ) ; 源代码网推荐 //以定义的数据库连接来初始化SqlDataAdapter实例 源代码网推荐 blFirst = false ; 源代码网推荐 } 源代码网推荐 else 源代码网推荐 { 源代码网推荐 sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter ( "SELECT EmployeeID as 序号, LastName as 姓氏 , FirstName as 名字 , Title as 职务 , Birthdate as 生日 From Employees ORDER BY 名字 DESC" , sqlConnection1 ) ; 源代码网推荐 //以定义的数据库连接来初始化SqlDataAdapter实例 源代码网推荐 blFirst = true ; 源代码网推荐 } 源代码网推荐 break ; 源代码网推荐 case "职务" : 源代码网推荐 if ( blTitle ) 源代码网推荐 { 源代码网推荐 sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter ( "SELECT EmployeeID as 序号, LastName as 姓氏 , FirstName as 名字 , Title as 职务 , Birthdate as 生日 From Employees ORDER BY 职务 ASC" , sqlConnection1 ) ; 源代码网推荐 //以定义的数据库连接来初始化SqlDataAdapter实例 源代码网推荐 blTitle = false ; 源代码网推荐 } 源代码网推荐 else 源代码网推荐 { 源代码网推荐 sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter ( "SELECT EmployeeID as 序号, LastName as 姓氏 , FirstName as 名字 , Title as 职务 , Birthdate as 生日 From Employees ORDER BY 职务 DESC" , sqlConnection1 ) ; 源代码网推荐 //以定义的数据库连接来初始化SqlDataAdapter实例 源代码网推荐 blTitle = true ; 源代码网推荐 } 源代码网推荐 break ; 源代码网推荐 case "生日" : 源代码网推荐 if ( blBirth ) 源代码网推荐 { 源代码网推荐 sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter ( "SELECT EmployeeID as 序号, LastName as 姓氏 , FirstName as 名字 , Title as 职务 , Birthdate as 生日 From Employees ORDER BY 生日 ASC" , sqlConnection1 ) ; 源代码网推荐 //以定义的数据库连接来初始化SqlDataAdapter实例 源代码网推荐 blBirth = false ; 源代码网推荐 } 源代码网推荐 else 源代码网推荐 { 源代码网推荐 sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter ( "SELECT EmployeeID as 序号, LastName as 姓氏 , FirstName as 名字 , Title as 职务 , Birthdate as 生日 From Employees ORDER BY 生日 DESC" , sqlConnection1 ) ; 源代码网推荐 //以定义的数据库连接来初始化SqlDataAdapter实例 源代码网推荐 blBirth = true ; 源代码网推荐 } 源代码网推荐 break ; 源代码网推荐 } 源代码网推荐 dataSet1 = new DataSet ( ) ; 源代码网推荐 sqlDataAdapter1.Fill ( dataSet1 , "employee" ) ; 源代码网推荐 //以SqlDataAdapter实例来填充本地DataSet数据集 源代码网推荐 DataGrid1.DataSource = dataSet1 ; 源代码网推荐 DataGrid1.DataBind ( ) ; 源代码网推荐 //实现数据绑定 源代码网推荐 } 源代码网推荐 源代码网推荐 4. 保存上面的修改步骤,这样就实现了从DataView到Sql语句实现DataGrid数据排序的转换。单击快捷键F5,就可以看到图06和图07所示界面。 源代码网推荐 源代码网推荐 六.总结: 源代码网推荐 源代码网推荐 通过以上内容的介绍,我们不仅了解、掌握了在ASP.NET页面中实现DataGrid中数据排序的二种方法,还应该了解并掌握下面内容: 源代码网推荐 源代码网推荐 1. 在ASP.NET中的DataGrid组件的数据绑定。 源代码网推荐 源代码网推荐 2. 改变ASP.NET页面中的DataGrid组件中的表头提示内容。 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
