Winform中GridView分组排序功能实现方法
本文实例讲述了Winform中GridView分组排序功能实现方法。分享给大家供大家参考。具体实现方法如下:
一、问题:
由于客户最近要扩充公司的业务,之前基于Winform+web开发混合式的系统已经不能满足他们的需求,需要从新对系统进行分区处理。
考虑到系统模块里面用到的GridView视图比较多,我就结合了DevExpress第三方GridControl简单的写了个Demo,对数据进行分组排序。
二、实现方法:
主程序源码:
usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Linq; usingSystem.Text; usingSystem.Windows.Forms; usingDevExpress.XtraGrid.Columns; usingDevExpress.Data; namespaceGridViewSortTest { publicpartialclassForm1:Form { publicForm1() { InitializeComponent(); } privatevoidForm1_Load(objectsender,EventArgse) { InitLoad(); #region分组排序代码 GridColumnSortInfo[]sortInfo={ newGridColumnSortInfo(gdvSort.Columns["GET_YEAR"],ColumnSortOrder.Ascending), newGridColumnSortInfo(gdvSort.Columns["ID"],ColumnSortOrder.Descending), }; gdvSort.SortInfo.ClearAndAddRange(sortInfo,1); #endregion } ///<summary> ///初始化GirdControl数据 ///</summary> privatevoidInitLoad() { gdcSort.DataSource=DBHelp.GetTable(string.Format(@"SELECTID,NAME,GET_YEAR, URL_ADRRESS FROM TB_SORT_TEST")); } ///<summary> ///GirdView单击事件 ///</summary> ///<paramname="sender"></param> ///<paramname="e"></param> privatevoidgdvSort_Click(objectsender,EventArgse) { if(gdvSort.FocusedColumn.FieldName.Equals(@"NAME")&&gdvSort.GetFocusedRowCellValue("NAME")!=null) { stringurl=gdvSort.GetFocusedRowCellValue("URL_ADRRESS").ToString()+ gdvSort.GetFocusedRowCellValue("GET_YEAR").ToString()+ MonthToString(int.Parse(gdvSort.GetFocusedRowCellValue("ID").ToString()))+ gdvSort.GetFocusedRowCellValue("NAME").ToString()+".html"; webBrowser.Navigate(url); } } ///<summary> ///月份转换成字符串 ///</summary> ///<paramname="month"></param> ///<returns></returns> privatestringMonthToString(intmonth) { if(month>=1&&month<=9) return"0"+month.ToString(); else returnmonth.ToString(); } } }
DBHelp类源码:
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Data; usingSystem.Data.SqlClient; namespaceGridViewSortTest { publicclassDBHelp { //连接字符串 staticstringstrConn=@"Server=.\SQLEXPRESS;Database=SysBusiness;UserId=sa;Password=123456;Trusted_Connection=False";
#region执行查询,返回DataTable对象-----------------------
publicstaticDataTableGetTable(stringstrSQL) { returnGetTable(strSQL,null); } publicstaticDataTableGetTable(stringstrSQL,SqlParameter[]pas) { returnGetTable(strSQL,pas,CommandType.Text); } ///<summary> ///执行查询,返回DataTable对象 ///</summary> ///<paramname="strSQL">sql语句</param> ///<paramname="pas">参数数组</param> ///<paramname="cmdtype">Command类型</param> ///<returns>DataTable对象</returns> publicstaticDataTableGetTable(stringstrSQL,SqlParameter[]pas,CommandTypecmdtype) { DataTabledt=newDataTable();; using(SqlConnectionconn=newSqlConnection(strConn)) { SqlDataAdapterda=newSqlDataAdapter(strSQL,conn); da.SelectCommand.CommandType=cmdtype; if(pas!=null) { da.SelectCommand.Parameters.AddRange(pas); } da.Fill(dt); } returndt; }
#endregion
#region执行查询,返回DataSet对象-------------------------
publicstaticDataSetGetDataSet(stringstrSQL) { returnGetDataSet(strSQL,null); } publicstaticDataSetGetDataSet(stringstrSQL,SqlParameter[]pas) { returnGetDataSet(strSQL,pas,CommandType.Text); } ///<summary> ///执行查询,返回DataSet对象 ///</summary> ///<paramname="strSQL">sql语句</param> ///<paramname="pas">参数数组</param> ///<paramname="cmdtype">Command类型</param> ///<returns>DataSet对象</returns> publicstaticDataSetGetDataSet(stringstrSQL,SqlParameter[]pas,CommandTypecmdtype) { DataSetdt=newDataSet();; using(SqlConnectionconn=newSqlConnection(strConn)) { SqlDataAdapterda=newSqlDataAdapter(strSQL,conn); da.SelectCommand.CommandType=cmdtype; if(pas!=null) { da.SelectCommand.Parameters.AddRange(pas); } da.Fill(dt); } returndt; } #endregion
#region执行非查询存储过程和SQL语句-----------------------------
publicstaticintExcuteProc(stringProcName) { returnExcuteSQL(ProcName,null,CommandType.StoredProcedure); } publicstaticintExcuteProc(stringProcName,SqlParameter[]pars) { returnExcuteSQL(ProcName,pars,CommandType.StoredProcedure); } publicstaticintExcuteSQL(stringstrSQL) { returnExcuteSQL(strSQL,null); } publicstaticintExcuteSQL(stringstrSQL,SqlParameter[]paras) { returnExcuteSQL(strSQL,paras,CommandType.Text); } ///执行非查询存储过程和SQL语句 ///增、删、改 ///</summary> ///<paramname="strSQL">要执行的SQL语句</param> ///<paramname="paras">参数列表,没有参数填入null</param> ///<paramname="cmdType">Command类型</param> ///<returns>返回影响行数</returns> publicstaticintExcuteSQL(stringstrSQL,SqlParameter[]paras,CommandTypecmdType) { inti=0; using(SqlConnectionconn=newSqlConnection(strConn)) { SqlCommandcmd=newSqlCommand(strSQL,conn); cmd.CommandType=cmdType; if(paras!=null) { cmd.Parameters.AddRange(paras); } conn.Open(); i=cmd.ExecuteNonQuery(); conn.Close(); } returni; }
#endregion
#region执行查询返回第一行,第一列---------------------------------
publicstaticintExcuteScalarSQL(stringstrSQL) { returnExcuteScalarSQL(strSQL,null); } publicstaticintExcuteScalarSQL(stringstrSQL,SqlParameter[]paras) { returnExcuteScalarSQL(strSQL,paras,CommandType.Text); } publicstaticintExcuteScalarProc(stringstrSQL,SqlParameter[]paras) { returnExcuteScalarSQL(strSQL,paras,CommandType.StoredProcedure); } ///<summary> ///执行SQL语句,返回第一行,第一列 ///</summary> ///<paramname="strSQL">要执行的SQL语句</param> ///<paramname="paras">参数列表,没有参数填入null</param> ///<returns>返回影响行数</returns> publicstaticintExcuteScalarSQL(stringstrSQL,SqlParameter[]paras,CommandTypecmdType) { inti=0; using(SqlConnectionconn=newSqlConnection(strConn)) { SqlCommandcmd=newSqlCommand(strSQL,conn); cmd.CommandType=cmdType; if(paras!=null) { cmd.Parameters.AddRange(paras); } conn.Open(); i=Convert.ToInt32(cmd.ExecuteScalar()); conn.Close(); } returni; }
#endregion
#region查询获取单个值------------------------------------
///<summary> ///调用不带参数的存储过程获取单个值 ///</summary> ///<paramname="ProcName"></param> ///<returns></returns> publicstaticobjectGetObjectByProc(stringProcName) { returnGetObjectByProc(ProcName,null); } ///<summary> ///调用带参数的存储过程获取单个值 ///</summary> ///<paramname="ProcName"></param> ///<paramname="paras"></param> ///<returns></returns> publicstaticobjectGetObjectByProc(stringProcName,SqlParameter[]paras) { returnGetObject(ProcName,paras,CommandType.StoredProcedure); } ///<summary> ///根据sql语句获取单个值 ///</summary> ///<paramname="strSQL"></param> ///<returns></returns> publicstaticobjectGetObject(stringstrSQL) { returnGetObject(strSQL,null); } ///<summary> ///根据sql语句和参数数组获取单个值 ///</summary> ///<paramname="strSQL"></param> ///<paramname="paras"></param> ///<returns></returns> publicstaticobjectGetObject(stringstrSQL,SqlParameter[]paras) { returnGetObject(strSQL,paras,CommandType.Text); } ///<summary> ///执行SQL语句,返回首行首列 ///</summary> ///<paramname="strSQL">要执行的SQL语句</param> ///<paramname="paras">参数列表,没有参数填入null</param> ///<returns>返回的首行首列</returns> publicstaticobjectGetObject(stringstrSQL,SqlParameter[]paras,CommandTypecmdtype) { objecto=null; using(SqlConnectionconn=newSqlConnection(strConn)) { SqlCommandcmd=newSqlCommand(strSQL,conn); cmd.CommandType=cmdtype; if(paras!=null) { cmd.Parameters.AddRange(paras); } conn.Open(); o=cmd.ExecuteScalar(); conn.Close(); } returno; }
#endregion
#region查询获取DataReader------------------------------------
///<summary> ///调用不带参数的存储过程,返回DataReader对象 ///</summary> ///<paramname="procName">存储过程名称</param> ///<returns>DataReader对象</returns> publicstaticSqlDataReaderGetReaderByProc(stringprocName) { returnGetReaderByProc(procName,null); } ///<summary> ///调用带有参数的存储过程,返回DataReader对象 ///</summary> ///<paramname="procName">存储过程名</param> ///<paramname="paras">参数数组</param> ///<returns>DataReader对象</returns> publicstaticSqlDataReaderGetReaderByProc(stringprocName,SqlParameter[]paras) { returnGetReader(procName,paras,CommandType.StoredProcedure); } ///<summary> ///根据sql语句返回DataReader对象 ///</summary> ///<paramname="strSQL">sql语句</param> ///<returns>DataReader对象</returns> publicstaticSqlDataReaderGetReader(stringstrSQL) { returnGetReader(strSQL,null); } ///<summary> ///根据sql语句和参数返回DataReader对象 ///</summary> ///<paramname="strSQL">sql语句</param> ///<paramname="paras">参数数组</param> ///<returns>DataReader对象</returns> publicstaticSqlDataReaderGetReader(stringstrSQL,SqlParameter[]paras) { returnGetReader(strSQL,paras,CommandType.Text); } ///<summary> ///查询SQL语句获取DataReader ///</summary> ///<paramname="strSQL">查询的SQL语句</param> ///<paramname="paras">参数列表,没有参数填入null</param> ///<returns>查询到的DataReader(关闭该对象的时候,自动关闭连接)</returns> publicstaticSqlDataReaderGetReader(stringstrSQL,SqlParameter[]paras,CommandTypecmdtype) { SqlDataReadersqldr=null; SqlConnectionconn=newSqlConnection(strConn); SqlCommandcmd=newSqlCommand(strSQL,conn); cmd.CommandType=cmdtype; if(paras!=null) { cmd.Parameters.AddRange(paras); } conn.Open(); //CommandBehavior.CloseConnection的作用是如果关联的DataReader对象关闭,则连接自动关闭 sqldr=cmd.ExecuteReader(CommandBehavior.CloseConnection); returnsqldr; } #endregion
#region批量插入数据---------------------------------------------
///<summary> ///往数据库中批量插入数据 ///</summary> ///<paramname="sourceDt">数据源表</param> ///<paramname="targetTable">服务器上目标表</param> publicstaticvoidBulkToDB(DataTablesourceDt,stringtargetTable) { SqlConnectionconn=newSqlConnection(strConn); SqlBulkCopybulkCopy=newSqlBulkCopy(conn); //用其它源的数据有效批量加载sqlserver表中 bulkCopy.DestinationTableName=targetTable; //服务器上目标表的名称 bulkCopy.BatchSize=sourceDt.Rows.Count; //每一批次中的行数 try { conn.Open(); if(sourceDt!=null&&sourceDt.Rows.Count!=0) bulkCopy.WriteToServer(sourceDt); //将提供的数据源中的所有行复制到目标表中 } catch(Exceptionex) { throwex; } finally { conn.Close(); if(bulkCopy!=null) bulkCopy.Close(); } } #endregion } }