程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> Winform中GridView分組排序功效完成辦法

Winform中GridView分組排序功效完成辦法

編輯:C#入門知識

Winform中GridView分組排序功效完成辦法。本站提示廣大學習愛好者:(Winform中GridView分組排序功效完成辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是Winform中GridView分組排序功效完成辦法正文


本文實例講述了Winform中GridView分組排序功效完成辦法。分享給年夜家供年夜家參考。詳細完成辦法以下:

1、成績:

因為客戶比來要擴大公司的營業,之前基於Winform+web開辟混雜式的體系曾經不克不及知足他們的需求,須要重新對體系停止分區處置。

斟酌到體系模塊外面用到的GridView視圖比擬多,我就聯合了DevExpress第三方GridControl簡略的寫了個Demo,對數據停止分組排序。

2、完成辦法:

主法式源碼:
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
    }
}

終究顯示後果圖以下:

願望本文所述對年夜家的C#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved