1.将dll添加到自定义分页上的选项卡上
2.拖入界面窗体
3.后台添加逻辑代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Globalization; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using SearchScore.Entity; using SearchScore.Services; using WHC.Framework.Commons; using WHC.Pager.Entity; namespace SearchScore { public partial class Form1 : Form { private readonly StudentService service = new StudentService(); /// <summary>数据源</summary> private readonly List<StudentSignUploadInfo> dataSource = new List<StudentSignUploadInfo>(); /// <summary>数据源索引</summary> private List<int> dataSourceIndex = new List<int>(); public Form1() { InitializeComponent(); } private void Btn_Search_Click(object sender, EventArgs e) { Task.Factory.StartNew(BindData); } private void Form1_Load(object sender, EventArgs e) { this.winGridViewPager1.OnPageChanged += new EventHandler(winGridViewPager1_OnPageChanged); //页面改变事件 this.winGridViewPager1.OnStartExport += new EventHandler(winGridViewPager1_OnStartExport); //导出事件 this.winGridViewPager1.OnRefresh += new EventHandler(winGridViewPager1_OnRefresh); //刷新事件 //this.winGridViewPager1.OnEditSelected += new EventHandler(winGridViewPager1_OnEditSelected); //选择编辑事件 //this.winGridViewPager1.OnDeleteSelected += new EventHandler(winGridViewPager1_OnDeleteSelected); //选择删除事件 //this.winGridViewPager1.OnAddNew += new EventHandler(winGridViewPager1_OnAddNew); //添加新建事件 //this.winGridViewPager1.AppendedMenu = this.contextMenuStrip1; //新增绑定菜单事件 //this.winGridViewPager1.dataGridView1.CellFormatting += // new DataGridViewCellFormattingEventHandler(FormatData); //自定义格式化显示内容 this.winGridViewPager1.ShowLineNumber = true;//显示行号 // this.winGridViewPager1.PagerInfo.PageSize = 20;//页面大小 // this.winGridViewPager1.EventRowBackColor = Color.LightCyan;//间隔颜色 } /// <summary> /// 格式化列数据显示内容 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FormatData(object sender, DataGridViewCellFormattingEventArgs e) { if (e.ColumnIndex == 7) { e.Value = "合格"; e.FormattingApplied = true; } if (e.ColumnIndex == 6) { e.Value = "第一次"; } } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void winGridViewPager1_OnRefresh(object sender, EventArgs e) { BindData(); } /// <summary> /// 导出页面数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void winGridViewPager1_OnStartExport(object sender, EventArgs e) { string where = GetSearchSql(); PagerInfo info = new PagerInfo { CurrenetPageIndex = 1, PageSize = int.MaxValue }; this.winGridViewPager1.AllToExport = FindToDataTable(where, info); } /// <summary> /// 页面改变时操作返回数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void winGridViewPager1_OnPageChanged(object sender, EventArgs e) { BindData(); } /// <summary> /// 自定义显示列名 /// </summary> /// <returns></returns> public Dictionary<string, string> GetColumnNameAlias() { Dictionary<string, string> dic = new Dictionary<string, string>() { {"XM","姓名"}, {"ZJHM", "证件号码"}, {"Ksrq1", "考试日期"}, {"PARAMNAME", "培训驾校"}, {"KSCS", "考试次数"}, {"Ksjg", "成绩"}, {"KL_sign_upload_1", "第一次"}, {"UploadMessage_1", "内容"}, {"KL_sign_upload_2", "第二次"}, {"UploadMessage_2", "内容"} }; return dic; } /// <summary> /// 绑定显示数据 /// </summary> private void BindData() { //自定义显示所需列名,不需要直接删除某个列名 this.winGridViewPager1.DisplayColumns = "XM,zjhm,Ksrq1,PARAMNAME,KSCS,Ksjg,KL_sign_upload_1,UploadMessage_1,KL_sign_upload_2,UploadMessage_2"; //获取自定义列名 this.winGridViewPager1.ColumnNameAlias = GetColumnNameAlias(); //获取条件查询 string where = GetSearchSql(); //列宽自动填满容器空间 this.winGridViewPager1.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; if (this.winGridViewPager1.InvokeRequired) { this.Invoke(new Action(() => { //只读操作 this.winGridViewPager1.dataGridView1.ReadOnly = true; //取消多选操作 this.winGridViewPager1.dataGridView1.MultiSelect = false; //查询数据源 this.winGridViewPager1.DataSource = FindToDataTable(where, winGridViewPager1.PagerInfo); })); } else { this.winGridViewPager1.dataGridView1.ReadOnly = true; this.winGridViewPager1.dataGridView1.MultiSelect = false; this.winGridViewPager1.DataSource = FindToDataTable(where, winGridViewPager1.PagerInfo); } } /// <summary> /// 根据查询条件构造查询语句 /// </summary> /// <returns></returns> private string GetSearchSql() { SearchCondition condition = new SearchCondition(); condition.AddCondition("Ksrq1", this.dateTimePicker1.Value, SqlOperator.Equal); string where = condition.BuildConditionSql(DatabaseType.Oracle).Replace("Where", ""); where += " And AA.ksbh not in (select ksbh from w2_queuing)"; return where; } /// <summary> /// 标准的记录查询函数 /// </summary> /// <param name="where"></param> /// <param name="pagerInfo"></param> /// <returns></returns> private DataTable FindToDataTable(string where, PagerInfo pagerInfo) { string sql = "select re.id,re.ksbh,re.kl_sign_upload_1,re.kl_sign_upload_2,re.ksrq1,re.kcbh,re.kscs,re.ksjg,re.kssj1,re.jssj1,re.jgfs1, " + $" re.kfxx1,re.kssj2,re.jssj2,re.jgfs2,re.kfxx2,re.ksy1,vh.lsh,vh.jxdm,vh.zjhm,vh.xm,vh.yycs,sy.paramname from w2_records re left join " + $" w2_vh_drs vh on re.ksbh = vh.bh left join w2_system sy on vh.jxdm = sy.paramdm"; WHC.Pager.WinControl.PagerHelper helper = new WHC.Pager.WinControl.PagerHelper(sql, "*", "Ksrq1", pagerInfo.PageSize, pagerInfo.CurrenetPageIndex, true, where); string countSql = helper.GetPagingSql(WHC.Pager.WinControl.DatabaseType.Oracle, true); string dataSql = helper.GetPagingSql(WHC.Pager.WinControl.DatabaseType.Oracle, false); string value = service.SqlValueList(countSql); pagerInfo.RecordCount = Convert.ToInt32(value);//为了显示具体的信息,需要设置总记录数 DataTable dt = service.SqlTable(dataSql); return dt; } } }
4.连接数据库操作
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/// <summary> /// 查询数据源数量大小 /// </summary> /// <param name="countSql">Sql语句</param> /// <returns>string类型的数量大小</returns> public string SqlValueList(string countSql) { string result; try { result = LocalInterfaceFactory.GetExamInterface().SltPublic("Enterprise", countSql); } catch (Exception e) { throw new Exception(e.Message); } return result; } /// <summary> /// 查询数据库数据 /// </summary> /// <param name="dataSql">sql语句</param> /// <returns>数据源DataTable类型</returns> public DataTable SqlTable(string dataSql) { DataTable dt; try { dt = LocalInterfaceFactory.GetExamInterface().QryPublicDt("Enterprise", dataSql); } catch (Exception e) { throw new Exception(e.Message); } return dt; }