欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Winform中GridView分组排序功能实现方法

程序员文章站 2023-12-14 14:54:04
本文实例讲述了winform中gridview分组排序功能实现方法。分享给大家供大家参考。具体实现方法如下: 一、问题: 由于客户最近要扩充公司的业务,之前基于winf...

本文实例讲述了winform中gridview分组排序功能实现方法。分享给大家供大家参考。具体实现方法如下:

一、问题:

由于客户最近要扩充公司的业务,之前基于winform+web开发混合式的系统已经不能满足他们的需求,需要从新对系统进行分区处理。

考虑到系统模块里面用到的gridview视图比较多,我就结合了devexpress第三方gridcontrol简单的写了个demo,对数据进行分组排序。

二、实现方法:

主程序源码:

复制代码 代码如下:
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using system.windows.forms;
using devexpress.xtragrid.columns;
using devexpress.data;
namespace gridviewsorttest
{
    public partial class form1 : form
    {
        public form1()
        {
            initializecomponent();
        }
        private void form1_load(object sender, eventargs e)
        {
            initload();
            #region 分组排序代码
            gridcolumnsortinfo[] sortinfo = {
                new gridcolumnsortinfo(gdvsort.columns["get_year"], columnsortorder.ascending),
                new gridcolumnsortinfo(gdvsort.columns["id"], columnsortorder.descending),
                                 };
            gdvsort.sortinfo.clearandaddrange(sortinfo,1);
            #endregion
        }
        /// <summary>
        /// 初始化girdcontrol数据
        /// </summary>
        private void initload()
        {
           gdcsort.datasource= dbhelp.gettable(string.format(@"select id,name,get_year,
                        url_adrress  from  tb_sort_test"));
        }
        /// <summary>
        /// girdview单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gdvsort_click(object sender, eventargs e)
        {
            if (gdvsort.focusedcolumn.fieldname.equals(@"name") && gdvsort.getfocusedrowcellvalue("name") != null)
            {
                string url = 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>
        /// <param name="month"></param>
        /// <returns></returns>
        private string monthtostring(int month)
        {
            if (month >= 1 && month <= 9)
                return "0" + month.tostring();
            else
                return month.tostring();
        }
    }
}

dbhelp类源码:
复制代码 代码如下:
using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.data;
using system.data.sqlclient;
namespace gridviewsorttest
{
  public class dbhelp
    {
        //连接字符串
      static string strconn =@"server=.\sqlexpress; database= sysbusiness; user id=sa; password=123456;trusted_connection = false ";

        #region 执行查询,返回datatable对象-----------------------

        public static datatable gettable(string strsql)
        {
            return gettable(strsql, null);
        }
        public static datatable gettable(string strsql, sqlparameter[] pas)
        {
            return gettable(strsql, pas, commandtype.text);
        }
        /// <summary>
        /// 执行查询,返回datatable对象
        /// </summary>
        /// <param name="strsql">sql语句</param>
        /// <param name="pas">参数数组</param>
        /// <param name="cmdtype">command类型</param>
        /// <returns>datatable对象</returns>
        public static datatable gettable(string strsql, sqlparameter[] pas, commandtype cmdtype)
        {
            datatable dt = new datatable(); ;
            using (sqlconnection conn = new sqlconnection(strconn))
            {
                sqldataadapter da = new sqldataadapter(strsql, conn);
                da.selectcommand.commandtype = cmdtype;
                if (pas != null)
                {
                    da.selectcommand.parameters.addrange(pas);
                }
                da.fill(dt);
            }
            return dt;
        }

        #endregion

        #region 执行查询,返回dataset对象-------------------------

        public static dataset getdataset(string strsql)
        {
            return getdataset(strsql, null);
        }
        public static dataset getdataset(string strsql, sqlparameter[] pas)
        {
            return getdataset(strsql, pas, commandtype.text);
        }
        /// <summary>
        /// 执行查询,返回dataset对象
        /// </summary>
        /// <param name="strsql">sql语句</param>
        /// <param name="pas">参数数组</param>
        /// <param name="cmdtype">command类型</param>
        /// <returns>dataset对象</returns>
        public static dataset getdataset(string strsql, sqlparameter[] pas, commandtype cmdtype)
        {
            dataset dt = new dataset(); ;
            using (sqlconnection conn = new sqlconnection(strconn))
            {
                sqldataadapter da = new sqldataadapter(strsql, conn);
                da.selectcommand.commandtype = cmdtype;
                if (pas != null)
                {
                    da.selectcommand.parameters.addrange(pas);
                }
                da.fill(dt);
            }
            return dt;
        }
        #endregion

        #region 执行非查询存储过程和sql语句-----------------------------

        public static int excuteproc(string procname)
        {
            return excutesql(procname, null, commandtype.storedprocedure);
        }
        public static int excuteproc(string procname, sqlparameter[] pars)
        {
            return excutesql(procname, pars, commandtype.storedprocedure);
        }
        public static int excutesql(string strsql)
        {
            return excutesql(strsql, null);
        }
        public static int excutesql(string strsql, sqlparameter[] paras)
        {
            return excutesql(strsql, paras, commandtype.text);
        }
        /// 执行非查询存储过程和sql语句
        /// 增、删、改
        /// </summary>
        /// <param name="strsql">要执行的sql语句</param>
        /// <param name="paras">参数列表,没有参数填入null</param>
        /// <param name="cmdtype">command类型</param>
        /// <returns>返回影响行数</returns>
        public static int excutesql(string strsql, sqlparameter[] paras, commandtype cmdtype)
        {
            int i = 0;
            using (sqlconnection conn = new sqlconnection(strconn))
            {
                sqlcommand cmd = new sqlcommand(strsql, conn);
                cmd.commandtype = cmdtype;
                if (paras != null)
                {
                    cmd.parameters.addrange(paras);
                }
                conn.open();
                i = cmd.executenonquery();
                conn.close();
            }
            return i;
        }

        #endregion

        #region 执行查询返回第一行,第一列---------------------------------

        public static int excutescalarsql(string strsql)
        {
            return excutescalarsql(strsql, null);
        }
        public static int excutescalarsql(string strsql, sqlparameter[] paras)
        {
            return excutescalarsql(strsql, paras, commandtype.text);
        }
        public static int excutescalarproc(string strsql, sqlparameter[] paras)
        {
            return excutescalarsql(strsql, paras, commandtype.storedprocedure);
        }
        /// <summary>
        /// 执行sql语句,返回第一行,第一列
        /// </summary>
        /// <param name="strsql">要执行的sql语句</param>
        /// <param name="paras">参数列表,没有参数填入null</param>
        /// <returns>返回影响行数</returns>
        public static int excutescalarsql(string strsql, sqlparameter[] paras, commandtype cmdtype)
        {
            int i = 0;
            using (sqlconnection conn = new sqlconnection(strconn))
            {
                sqlcommand cmd = new sqlcommand(strsql, conn);
                cmd.commandtype = cmdtype;
                if (paras != null)
                {
                    cmd.parameters.addrange(paras);
                }
                conn.open();
                i = convert.toint32(cmd.executescalar());
                conn.close();
            }
            return i;
        }

        #endregion

        #region 查询获取单个值------------------------------------

        /// <summary>
        /// 调用不带参数的存储过程获取单个值
        /// </summary>
        /// <param name="procname"></param>
        /// <returns></returns>
        public static object getobjectbyproc(string procname)
        {
            return getobjectbyproc(procname, null);
        }
        /// <summary>
        /// 调用带参数的存储过程获取单个值
        /// </summary>
        /// <param name="procname"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public static object getobjectbyproc(string procname, sqlparameter[] paras)
        {
            return getobject(procname, paras, commandtype.storedprocedure);
        }
        /// <summary>
        /// 根据sql语句获取单个值
        /// </summary>
        /// <param name="strsql"></param>
        /// <returns></returns>
        public static object getobject(string strsql)
        {
            return getobject(strsql, null);
        }
        /// <summary>
        /// 根据sql语句 和 参数数组获取单个值
        /// </summary>
        /// <param name="strsql"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public static object getobject(string strsql, sqlparameter[] paras)
        {
            return getobject(strsql, paras, commandtype.text);
        }
        /// <summary>
        /// 执行sql语句,返回首行首列
        /// </summary>
        /// <param name="strsql">要执行的sql语句</param>
        /// <param name="paras">参数列表,没有参数填入null</param>
        /// <returns>返回的首行首列</returns>
        public static object getobject(string strsql, sqlparameter[] paras, commandtype cmdtype)
        {
            object o = null;
            using (sqlconnection conn = new sqlconnection(strconn))
            {
                sqlcommand cmd = new sqlcommand(strsql, conn);
                cmd.commandtype = cmdtype;
                if (paras != null)
                {
                    cmd.parameters.addrange(paras);
                }
                conn.open();
                o = cmd.executescalar();
                conn.close();
            }
            return o;
        }

        #endregion

        #region 查询获取datareader------------------------------------

        /// <summary>
        /// 调用不带参数的存储过程,返回datareader对象
        /// </summary>
        /// <param name="procname">存储过程名称</param>
        /// <returns>datareader对象</returns>
        public static sqldatareader getreaderbyproc(string procname)
        {
            return getreaderbyproc(procname, null);
        }
        /// <summary>
        /// 调用带有参数的存储过程,返回datareader对象
        /// </summary>
        /// <param name="procname">存储过程名</param>
        /// <param name="paras">参数数组</param>
        /// <returns>datareader对象</returns>
        public static sqldatareader getreaderbyproc(string procname, sqlparameter[] paras)
        {
            return getreader(procname, paras, commandtype.storedprocedure);
        }
        /// <summary>
        /// 根据sql语句返回datareader对象
        /// </summary>
        /// <param name="strsql">sql语句</param>
        /// <returns>datareader对象</returns>
        public static sqldatareader getreader(string strsql)
        {
            return getreader(strsql, null);
        }
        /// <summary>
        /// 根据sql语句和参数返回datareader对象
        /// </summary>
        /// <param name="strsql">sql语句</param>
        /// <param name="paras">参数数组</param>
        /// <returns>datareader对象</returns>
        public static sqldatareader getreader(string strsql, sqlparameter[] paras)
        {
            return getreader(strsql, paras, commandtype.text);
        }
        /// <summary>
        /// 查询sql语句获取datareader
        /// </summary>
        /// <param name="strsql">查询的sql语句</param>
        /// <param name="paras">参数列表,没有参数填入null</param>
        /// <returns>查询到的datareader(关闭该对象的时候,自动关闭连接)</returns>
        public static sqldatareader getreader(string strsql, sqlparameter[] paras, commandtype cmdtype)
        {
            sqldatareader sqldr = null;
            sqlconnection conn = new sqlconnection(strconn);
            sqlcommand cmd = new sqlcommand(strsql, conn);
            cmd.commandtype = cmdtype;
            if (paras != null)
            {
                cmd.parameters.addrange(paras);
            }
            conn.open();
            //commandbehavior.closeconnection的作用是如果关联的datareader对象关闭,则连接自动关闭
            sqldr = cmd.executereader(commandbehavior.closeconnection);
            return sqldr;
        }
        #endregion

        #region 批量插入数据---------------------------------------------

        /// <summary>
        /// 往数据库中批量插入数据
        /// </summary>
        /// <param name="sourcedt">数据源表</param>
        /// <param name="targettable">服务器上目标表</param>
        public static void bulktodb(datatable sourcedt, string targettable)
        {
            sqlconnection conn = new sqlconnection(strconn);
            sqlbulkcopy bulkcopy = new sqlbulkcopy(conn);   //用其它源的数据有效批量加载sql server表中
            bulkcopy.destinationtablename = targettable;    //服务器上目标表的名称
            bulkcopy.batchsize = sourcedt.rows.count;   //每一批次中的行数
            try
            {
                conn.open();
                if (sourcedt != null && sourcedt.rows.count != 0)
                    bulkcopy.writetoserver(sourcedt);   //将提供的数据源中的所有行复制到目标表中
            }
            catch (exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.close();
                if (bulkcopy != null)
                    bulkcopy.close();
            }
        }
        #endregion
    }
}

最终显示效果图如下:

Winform中GridView分组排序功能实现方法

希望本文所述对大家的c#程序设计有所帮助。

上一篇:

下一篇: