本文实例讲述了Winform中GridView分组排序功能实现方法。分享给大家供大家参考。具体实现方法如下:
一、问题:
由于客户最近要扩充公司的业务,之前基于Winform+web开发混合式的系统已经不能满足他们的需求,需要从新对系统进行分区处理。
考虑到系统模块里面用到的GridView视图比较多,我就结合了DevExpress第三方GridControl简单的写了个Demo,对数据进行分组排序。
二、实现方法:
主程序源码:
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
}
///
/// 初始化GirdControl数据
///
private void InitLoad()
{
gdcSort.DataSource= DBHelp.GetTable(string.Format(@"SELECT ID,NAME,GET_YEAR,
URL_ADRRESS FROM TB_SORT_TEST"));
}
///
/// GirdView单击事件
///
///
///
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);
}
}
///
/// 月份转换成字符串
///
///
///
private string MonthToString(int month)
{
if (month >= 1 && month <= 9)
return "0" + month.ToString();
else
return month.ToString();
}
}
}
DBHelp类源码:
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);
}
///
/// 执行查询,返回DataTable对象
///
/// sql语句
/// 参数数组
/// Command类型
///
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);
}
///
/// 执行查询,返回DataSet对象
///
/// sql语句
/// 参数数组
/// Command类型
///
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语句
/// 增、删、改
///
/// 要执行的SQL语句
/// 参数列表,没有参数填入null
/// Command类型
///
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);
}
///
/// 执行SQL语句,返回第一行,第一列
///
/// 要执行的SQL语句
/// 参数列表,没有参数填入null
///
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 查询获取单个值------------------------------------
///
/// 调用不带参数的存储过程获取单个值
///
///
///
public static object GetObjectByProc(string ProcName)
{
return GetObjectByProc(ProcName, null);
}
///
/// 调用带参数的存储过程获取单个值
///
///
///
///
public static object GetObjectByProc(string ProcName, SqlParameter[] paras)
{
return GetObject(ProcName, paras, CommandType.StoredProcedure);
}
///
/// 根据sql语句获取单个值
///
///
///
public static object GetObject(string strSQL)
{
return GetObject(strSQL, null);
}
///
/// 根据sql语句 和 参数数组获取单个值
///
///
///
///
public static object GetObject(string strSQL, SqlParameter[] paras)
{
return GetObject(strSQL, paras, CommandType.Text);
}
///
/// 执行SQL语句,返回首行首列
///
/// 要执行的SQL语句
/// 参数列表,没有参数填入null
///
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------------------------------------
///
/// 调用不带参数的存储过程,返回DataReader对象
///
/// 存储过程名称
///
public static SqlDataReader GetReaderByProc(string procName)
{
return GetReaderByProc(procName, null);
}
///
/// 调用带有参数的存储过程,返回DataReader对象
///
/// 存储过程名
/// 参数数组
///
public static SqlDataReader GetReaderByProc(string procName, SqlParameter[] paras)
{
return GetReader(procName, paras, CommandType.StoredProcedure);
}
///
/// 根据sql语句返回DataReader对象
///
/// sql语句
///
public static SqlDataReader GetReader(string strSQL)
{
return GetReader(strSQL, null);
}
///
/// 根据sql语句和参数返回DataReader对象
///
/// sql语句
/// 参数数组
///
public static SqlDataReader GetReader(string strSQL, SqlParameter[] paras)
{
return GetReader(strSQL, paras, CommandType.Text);
}
///
/// 查询SQL语句获取DataReader
///
/// 查询的SQL语句
/// 参数列表,没有参数填入null
///
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 批量插入数据---------------------------------------------
///
/// 往数据库中批量插入数据
///
/// 数据源表
/// 服务器上目标表
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#程序设计有所帮助。