DataGirdView存储过程分页 For Winfrom .

来源:互联网 发布:买一个淘宝店铺 编辑:程序博客网 时间:2024/06/04 18:48
 

在Winfrom中,网格控件DataGridView并不像DataGrid自带分页功能,正好工作需要,写了下分页,只是满足项目要求。

存储过程:

CREATE  PROC pro_userDefined_DataGridPage@TotalRecords   INT OUTPUT,   --记录总数@TotalTableName VARCHAR(100), --查询记录总数的表名@TotalJoinStr VARCHAR (1000), --查询记录总数连接语句@TotalRecordsCondition VARCHAR(500),--查询记录总数的条件语句@CurrentPage  INT = 1,        --当前页数@PageSize     INT = 10,          --每页显示的行数@TableName    VARCHAR (100),--表名,视图,存储过程@FieldNames   VARCHAR (1000),--需要显示的字段名@JoinStr      VARCHAR (1000),--连接语句(左连接,右连接,内连接,交叉连接)@PrimaryField VARCHAR (100), --主键字段@ConditionString VARCHAR (1000),--条件语句@FilterString VARCHAR (1000),--过滤条件语句@OrderField   VARCHAR (100) --根据字段排序ASDECLARE@queryStr NVARCHAR(3000), --SQL查询语句@FilterRecordCount INT   --过滤的记录数IF @ConditionString = '' SET  @ConditionString =' 1=1 'IF @FilterString = ''SET @FilterString = ' 1=1 'IF @TotalJoinStr = ''SET @TotalJoinStr = ''
BEGINSET @queryStr = 'SELECT @TotalRecords = COUNT(' + @PrimaryField + ')FROM ' + @TotalTableName + ' ' + @TotalJoinStr + ' WHERE ' +  @TotalRecordsConditionEXEC sp_executesql @queryStr,N'@TotalRecords int output',@TotalRecords outputPRINT @TotalRecordsENDIF @CurrentPage = 1 --第一页BEGINSET @queryStr = ' SELECT TOP ' + CAST(@PageSize AS VARCHAR(10)) + ' ' + @fieldNames + ' FROM ' + @TableName + ' ' + @JoinStr + ' WHERE ' +  @ConditionString + ' ORDER BY ' + @OrderFieldEXEC (@queryStr)ENDELSEBEGINSET @CurrentPage = @CurrentPage - 1SET @FilterRecordCount = CAST(@CurrentPage * @PageSize AS VARCHAR(10))SET @queryStr =' SELECT TOP ' + CAST(@PageSize AS VARCHAR(10)) + ' ' + @fieldNames + ' FROM ' + @TableName + ' ' + @JoinStr + ' WHERE ' + @PrimaryField +        ' NOT IN ( SELECT TOP ' + CAST(@FilterRecordCount AS VARCHAR(10)) + ' ' + @PrimaryField + ' FROM ' + @TableName + ' WHERE ' + @FilterString + ' ORDER BY ' + @PrimaryField + ') AND ' +  @ConditionString +       ' ORDER BY ' + @PrimaryFieldEXEC (@queryStr)
ENDGO
 
用户控件:

导航菜单,我就用了ToolStrip控件进行创建各个按钮

代码实现:

声明变量,属性

view plaincopy to clipboardprint?
  1. #region 变量   
  2.   
  3.         private int _PageSum = 0;   
  4.   
  5.        private int _TotalRecords = 0;  
  6.   
  7.        private int _CurrentPage = 1;  
  8.   
  9.        private int _PageSize = 10;  
  10.   
  11.        private string _TableName = null;  
  12.   
  13.        private string _TotalTableName = null;  
  14.   
  15.        private string _TotalJoinStr = null;  
  16.   
  17.        private string _TotalRecordsCondition = null;  
  18.   
  19.        private string _FieldNames = null;  
  20.   
  21.        private string _JoinStr = null;  
  22.   
  23.        private string _PrimaryField = null;  
  24.   
  25.        private string _ConditionString = null;  
  26.   
  27.        private string _FilterString = null;  
  28.   
  29.        private string _OrderField = null;  
  30.   
  31.        private DataTable _GridSource;  
  32.   
  33.        private SqlParameter[] _SqlParameter;  
  34.   
  35.        private SqlCommand     _SqlCommand;  
  36.   
  37.        private SqlDataAdapter _SqlDataAdatpter;  
  38.   
  39.        private DataSet   _girdDataSet;  
  40.   
  41.        private DataTable _gridTable;  
  42.   
  43.        private BindingSource _bingingSource = new BindingSource();  
  44.   
  45.        private string _currentpageStr = "{0}";  
  46.   
  47.        private string _pagesumStr = "共{0}条记录";  
  48.  
  49.        #endregion  
  50.  
  51.  
  52.  
  53.        #region 属性   
  54.          /// <summary>   
  55.         /// 设置当前页数   
  56.          /// </summary>   
  57. public int SetCurrentPage  
  58.        {  
  59.            set { _CurrentPage = value; }  
  60.        }  
  61.   
  62.        /// <summary>   
  63.        /// 设置每页显示的行数   
  64.         /// </summary>   
  65.        public int SetPageSize  
  66.        {  
  67.            set { _PageSize = value; }  
  68.        }  
  69.   
  70.        /// <summary>   
  71.        /// 设置表名,视图,存储过程   
  72.         /// </summary>   
  73.        public string SetTableName  
  74.        {  
  75.            set { _TableName = value;}  
  76.        }  
view plaincopy to clipboardprint?
  1. /// <summary>   
  2. /// 设置查询记录总数的表名   
  3. /// </summary>   
  4. public string SetTotalTableName  
  5. {  
  6.     set { _TotalTableName = value; }  
  7. }  
  8.   
  9. /// <summary>   
  10. /// 设置查询总记录数连接语句(左连接,右连接,内连接,交叉连接)   
  11. /// </summary>   
  12. public string SetTotalJoinStr  
  13. {  
  14.     set { _TotalJoinStr = value; }  
  15. }  
  16.   
  17. /// <summary>   
  18. /// 设置查询记录总数的条件语句   
  19.  /// </summary>   
  20. public string SetTotalRecordsCondition   <PRE class=csharp name="code">        {  
  21.     set { _TotalRecordsCondition = value; }  
  22. }  
  23.   
  24. /// <summary>   
  25. /// 设置需要DataGirdView显示的字段,以逗号分开   
  26.  /// </summary>   
  27. public string SetFieldNames  
  28. {  
  29.     set { _FieldNames = value; }  
  30. }  
  31.   
  32. /// <summary>   
  33. /// 连接语句(左连接,右连接,内连接,交叉连接)   
  34. /// </summary>   
  35. public string SetJoinStr  
  36. {  
  37.     set { _JoinStr = value; }  
  38. }  
  39.   
  40. /// <summary>   
  41. /// 设置主键字段   
  42. /// </summary>   
  43. public string SetPrimaryField  
  44. {  
  45.     set { _PrimaryField = value; }  
  46. }  
  47.   
  48. /// <summary>   
  49. /// 设置查询条件语句</PRE>   
  50. ss=csharp name="code">        /// </summary>   
  51. public string SetConditionString  
  52. {  
  53.     set { _ConditionString = value; }  
  54. }  
  55.   
  56. /// <summary>   
  57. /// 设置过滤查询条件语句   
  58. /// </summary>   
  59. public string SetFilterString  
  60. {  
  61.     set { _FilterString = value; }  
  62. }  
  63.   
  64. /// <summary>   
  65. /// 设置排序字段   
  66.  /// </summary>   
  67. public string SetOrderField  
  68. {  
  69.     set { _OrderField = value; }  
  70. }  
  71.   
  72. /// <summary>   
  73. /// 获得DataGridView数据源   
  74.  /// </summary>   
  75. public DataTable GetGridSource  
  76. {  
  77.     get { return _gridTable; }  
  78. }  
  79.   
  80. public BindingSource GetBingingSource  
  81. {  
  82.     get { return _bingingSource; }  
  83. }</PRE>  
view plaincopy to clipboardprint?
  1.    
view plaincopy to clipboardprint?
  1. <FONT color=#008080>toolStrip点击事件ItemClick</FONT>  
view plaincopy to clipboardprint?
  1. <PRE class=csharp name="code">        #endregion</PRE>  
  2. <PRE class=csharp name="code">        /// <summary>   
  3.         /// 导航条点击事件   
  4.          /// </summary>   
  5.         /// <param name="sender"></param>   
  6.         /// <param name="e"></param>   
  7.         private void strNavigationBar_ItemClicked(object sender, ToolStripItemClickedEventArgs e)  
  8.         {  
  9.             int m_currentPage = Convert.ToInt32(txtCurrentPage.Text);  
  10.   
  11.             int m_pageNumber = 1;//转到页数   
  12.   
  13.               int m_pageSize = 10;//设置显示行数   
  14.   
  15.               bool m_isValid = false;//输入的行数和页数是否合法   
  16.   
  17.               if (dataGridView.DataSource == null)  
  18.             {  
  19.                 return;  
  20.             }  
  21.   
  22.             try  
  23.             {  
  24.                 if (e.ClickedItem.Text == "上一页")  
  25.                 {  
  26.                     m_currentPage--;  
  27.   
  28.                     if (m_currentPage <= 0)  
  29.                     {  
  30.   
  31.                         MessageBox.Show("已经是第一页,请点击‘下一页’查看!""提示", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  32.                         return;  
  33.                     }  
  34.                     else  
  35.                     {  
  36.                         SetCurrentPage = m_currentPage;  
  37.   
  38.                         PreviousPage(_CurrentPage);  
  39.   
  40.                         txtCurrentPage.Text = m_currentPage.ToString();  
  41.                     }  
  42.                 }  
  43.   
  44.                 if (e.ClickedItem.Text == "下一页")  
  45.                 {  
  46.                     m_currentPage++;  
  47.   
  48.                     if (m_currentPage > _PageSum)  
  49.                     {  
  50.                         MessageBox.Show("已经是最后页,请点击‘上一页’查看!""提示", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  51.                         return;  
  52.                     }  
  53.                     else  
  54.                     {  
  55.                         SetCurrentPage = m_currentPage;  
  56.   
  57.                         NextPage(_CurrentPage);  
  58.   
  59.                         txtCurrentPage.Text = m_currentPage.ToString();  
  60.                     }  
  61.                 }  
  62.   
  63.                 if (e.ClickedItem.Text == "首页")  
  64.                 {  
  65.                     SetCurrentPage = 1;  
  66.   
  67.                     FirstPage(_CurrentPage);  
  68.   
  69.                     txtCurrentPage.Text = "1";  
  70.                 }  
  71.   
  72.                 if (e.ClickedItem.Text == "尾页")  
  73.                 {  
  74.                     SetCurrentPage = _PageSum;  
  75.   
  76.                     FirstPage(_CurrentPage);  
  77.   
  78.                     txtCurrentPage.Text = _PageSum.ToString();  
  79.                 }  
  80.   
  81.                 if (e.ClickedItem.Text == "Go")  
  82.                 {  
  83.                     m_isValid = isValidContent(txtGotoPageNumber.Text.Trim());  
  84.   
  85.                     if (!m_isValid)  
  86.                     {  
  87.                         MessageBox.Show("输入有效的页码!""提示", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  88.   
  89.                         txtGotoPageNumber.Focus();  
  90.   
  91.                         return;  
  92.                     }  
  93.   
  94.                     if (_TotalRecords > 0)  
  95.                     {  
  96.                         m_pageNumber = Convert.ToInt32(txtGotoPageNumber.Text.Trim());  
  97.   
  98.                         if (m_pageNumber < 0 || m_pageNumber > _PageSum)  
  99.                         {  
  100.                             MessageBox.Show("输入有效的页码!""提示", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  101.   
  102.                             txtGotoPageNumber.Focus();  
  103.   
  104.                             return;  
  105.                         }  
  106.   
  107.                         GotoPageNumber(m_pageNumber);  
  108.   
  109.                         txtCurrentPage.Text = m_pageNumber.ToString();  
  110.                     }  
  111.                 }  
  112.                 if (e.ClickedItem.Text == "确定")  
  113.                 {  
  114.   
  115.                     m_isValid = isValidContent(txtPageSize.Text.Trim());  
  116.   
  117.                     if (!m_isValid)  
  118.                     {  
  119.                         MessageBox.Show("输入有效的行数!""提示", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  120.   
  121.                         txtPageSize.Focus();  
  122.   
  123.                         return;  
  124.                     }  
  125.   
  126.                     m_pageSize = Convert.ToInt32(txtPageSize.Text.Trim());  
  127.   
  128.                     if (m_pageNumber < 0)  
  129.                     {  
  130.                         MessageBox.Show("输入有效的行数!""提示", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  131.   
  132.                         txtPageSize.Focus();  
  133.   
  134.                         return;  
  135.                     }  
  136.                     SetPageSize = m_pageSize;  
  137.   
  138.                     InitDataGrid();  
  139.                 }  
  140.   
  141.                 if (e.ClickedItem.Name == "btnHideNavigation")  
  142.                 {  
  143.                     btnShowHideNavigation.Visible = true;  
  144.   
  145.                     strNavigationBar.Visible = false;  
  146.                 }  
  147.             }  
  148.             catch (Exception CommonException)  
  149.             {  
  150.                 MessageBox.Show("窗口加载出错!" + CommonException.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);</PRE>  
view plaincopy to clipboardprint?
  1. <PRE class=csharp name="code">            }  
  2.         } </PRE>  
  3. <PRE class=csharp name="code"> </PRE>  
  4. <PRE class=csharp name="code"><FONT color=#008080>按钮事件</FONT></PRE>  
  5. <PRE class=csharp name="code"> </PRE>  
  6. <PRE class=csharp name="code">        /// <summary>   
  7.         /// 首页   
  8.          /// </summary>   
  9.        /// <param name="CurrentPage"></param>   
  10.         private void FirstPage(int CurrentPage)  
  11.         {  
  12.             try  
  13.             {  
  14.                 SetCurrentPage = CurrentPage;  
  15.                 InitDataGrid();  
  16.             }  
  17.             catch (Exception CommonException)  
  18.             {  
  19.                 throw new Exception(CommonException.Message);  
  20.             }  
  21.         }  
  22.   
  23.         /// <summary>   
  24.         /// 尾页   
  25.          /// </summary>   
  26.         /// <param name="CurrentPage"></param>   
  27.         private void LastPage(int CurrentPage)  
  28.         {  
  29.             try  
  30.             {  
  31.                 SetCurrentPage = CurrentPage;  
  32.                 InitDataGrid();  
  33.             }  
  34.             catch (Exception CommonException)  
  35.             {  
  36.                 throw new Exception(CommonException.Message);  
  37.             }  
  38.         }  
  39.   
  40.         /// <summary>   
  41.         /// 下一页   
  42.          /// </summary>   
  43.         /// <param name="CurrentPage"></param>   
  44.         private void NextPage(int CurrentPage)  
  45.         {  
  46.             try</PRE>  
  47. <PRE class=csharp name="code">            {  
  48.                 SetCurrentPage = CurrentPage;  
  49.                 InitDataGrid();  
  50.             }  
  51.             catch (Exception CommonException)  
  52.             {  
  53.                throw new Exception(CommonException.Message);  
  54.             }  
  55.         }  
  56.   
  57.         /// <summary>   
  58.         /// 上一页   
  59.          /// </summary>   
  60.         /// <param name="CurrentPage"></param>   
  61.         private void PreviousPage(int CurrentPage)  
  62.         {  
  63.             try  
  64.             {  
  65.                 SetCurrentPage = CurrentPage;  
  66.                 InitDataGrid();  
  67.             }  
  68.             catch (Exception CommonException)  
  69.             {  
  70.                 throw new Exception(CommonException.Message);  
  71.             }  
  72.         }  
  73.   
  74.         /// <summary>   
  75.         /// 转页   
  76.          /// </summary>   
  77.         /// <param name="pageNumber"></param>   
  78.         private void GotoPageNumber(int pageNumber)  
  79.         {  
  80.             try  
  81.             {  
  82.                 SetCurrentPage = pageNumber;  
  83.                 InitDataGrid();  
  84.             }  
  85.             catch (Exception CommonException)  
  86.             {  
  87.                 throw new Exception(CommonException.Message);  
  88.             }  
  89.         } </PRE>  
  90. <PRE class=csharp name="code"> </PRE>  
  91. <PRE class=csharp name="code"><FONT color=#008080>对输入的页数等信息进行验证</FONT></PRE>  
  92. <PRE class=csharp name="code"> </PRE>  
  93. <PRE class=csharp name="code"><PRE class=csharp name="code">        /// <summary>       
  94.         /// 验证是否是有效的内容     
  95.         /// </summary>   
  96.         /// <param name="content"></param>   
  97.         /// <returns></returns>   
  98.         private bool isValidContent(string content)  
  99.         {  
  100.             bool m_isValid = true;  
  101.   
  102.             for (int index = 0; index < content.Trim().Length; index++)  
  103.             {  
  104.                 if (!Char.IsNumber(content.Trim(), index))  
  105.                 {  
  106.                     m_isValid = false;  
  107.                 }  
  108.             }  
  109.             return m_isValid;  
  110.         }</PRE>  
  111. <PRE class=csharp name="code"> </PRE>  
  112. <PRE class=csharp name="code"><FONT color=#008080>初始化DataGridView方法</FONT></PRE>  
  113. <PRE class=csharp name="code"> </PRE>  
  114. <PRE class=csharp name="code">        /// <summary>   
  115.         /// 初始化DataGirdView   
  116.         /// </summary>   
  117.         public void InitDataGrid()  
  118.         {  
  119.             _SqlDataAdatpter = new SqlDataAdapter();  
  120.   
  121.             _SqlCommand = new SqlCommand();  
  122.   
  123.             _girdDataSet = new DataSet();  
  124.   
  125.             _SqlParameter = new SqlParameter[13];  
  126.   
  127.             try  
  128.             {  
  129.                 _SqlParameter[0] = new SqlParameter();  
  130.   
  131.                 _SqlParameter[0].ParameterName = "@TotalRecords";  
  132.   
  133.                 _SqlParameter[0].Size = 4;  
  134.   
  135.                 _SqlParameter[0].Direction = ParameterDirection.Output;  
  136.   
  137.                 _SqlParameter[1] = new SqlParameter();  
  138.   
  139.                 _SqlParameter[1].ParameterName = "@CurrentPage";  
  140.   
  141.                 _SqlParameter[1].Size = 4;  
  142.   
  143.                 _SqlParameter[1].Value = _CurrentPage;  
  144.   
  145.                 _SqlParameter[1].Direction = ParameterDirection.Input;  
  146.   
  147.                 _SqlParameter[2] = new SqlParameter();  
  148.   
  149.                 _SqlParameter[2].ParameterName = "@PageSize";  
  150.   
  151.                 _SqlParameter[2].Size = 4;  
  152.   
  153.                 _SqlParameter[2].Value = _PageSize;  
  154.   
  155.                 _SqlParameter[2].Direction = ParameterDirection.Input;  
  156.   
  157.                 _SqlParameter[3] = new SqlParameter();  
  158.   
  159.                 _SqlParameter[3].ParameterName = "@TableName";  
  160.   
  161.                 _SqlParameter[3].Size = 100;  
  162.   
  163.                 _SqlParameter[3].Value = _TableName;  
  164.   
  165.                 _SqlParameter[3].Direction = ParameterDirection.Input;  
  166.   
  167.                 _SqlParameter[4] = new SqlParameter();  
  168.   
  169.                 _SqlParameter[4].ParameterName = "@TotalTableName";  
  170.   
  171.                 _SqlParameter[4].Size = 100;  
  172.   
  173.                 _SqlParameter[4].Value = _TotalTableName;  
  174.   
  175.                 _SqlParameter[4].Direction = ParameterDirection.Input;  
  176.   
  177.                 _SqlParameter[5] = new SqlParameter();  
  178.   
  179.                 _SqlParameter[5].ParameterName = "@TotalJoinStr";  
  180.   
  181.                 _SqlParameter[5].Size = 1000;  
  182.   
  183.                 _SqlParameter[5].Value =_TotalJoinStr;  
  184.   
  185.                 _SqlParameter[5].Direction = ParameterDirection.Input;  
  186.   
  187.                 _SqlParameter[6] = new SqlParameter();  
  188.   
  189.                 _SqlParameter[6].ParameterName = "@TotalRecordsCondition";  
  190.   
  191.                 _SqlParameter[6].Size = 500;  
  192.   
  193.                 _SqlParameter[6].Value = _TotalRecordsCondition;  
  194.   
  195.                 _SqlParameter[6].Direction = ParameterDirection.Input;  
  196.   
  197.                 _SqlParameter[7] = new SqlParameter();  
  198.   
  199.                 _SqlParameter[7].ParameterName = "@FieldNames";  
  200.   
  201.                 _SqlParameter[7].Size = 1000;  
  202.   
  203.                 _SqlParameter[7].Value = _FieldNames;  
  204.   
  205.                 _SqlParameter[7].Direction = ParameterDirection.Input;  
  206.   
  207.                 _SqlParameter[8] = new SqlParameter();  
  208.   
  209.                 _SqlParameter[8].ParameterName = "@JoinStr";  
  210.   
  211.                 _SqlParameter[8].Size = 1000;  
  212.   
  213.                 _SqlParameter[8].Value = _JoinStr;  
  214.   
  215.                 _SqlParameter[8].Direction = ParameterDirection.Input;  
  216.   
  217.                 _SqlParameter[9] = new SqlParameter();  
  218.   
  219.                 _SqlParameter[9].ParameterName = "@PrimaryField";  
  220.   
  221.                 _SqlParameter[9].Size = 100;  
  222.   
  223.                 _SqlParameter[9].Value = _PrimaryField;  
  224.   
  225.                 _SqlParameter[9].Direction = ParameterDirection.Input;  
  226.   
  227.                 _SqlParameter[10] = new SqlParameter();  
  228.   
  229.                 _SqlParameter[10].ParameterName = "@ConditionString";  
  230.   
  231.                 _SqlParameter[10].Size = 1000;  
  232.   
  233.                 _SqlParameter[10].Value = _ConditionString;  
  234.   
  235.                 _SqlParameter[10].Direction = ParameterDirection.Input;  
  236.   
  237.                 _SqlParameter[11] = new SqlParameter();  
  238.   
  239.                 _SqlParameter[11].ParameterName = "@FilterString";  
  240.   
  241.                 _SqlParameter[11].Size = 1000;  
  242.   
  243.                 _SqlParameter[11].Value = _FilterString;  
  244.   
  245.                 _SqlParameter[11].Direction = ParameterDirection.Input;  
  246.   
  247.                 _SqlParameter[12] = new SqlParameter();  
  248.   
  249.                 _SqlParameter[12].ParameterName = "@OrderField";  
  250.   
  251.                 _SqlParameter[12].Size = 100;  
  252.   
  253.                 _SqlParameter[12].Value = _OrderField;  
  254.   
  255.                 _SqlParameter[12].Direction = ParameterDirection.Input;  
  256.   
  257.                 _girdDataSet = DBOperation.RunProcedure("pro_userDefined_DataGridPage", _SqlParameter, "gridtable");  
  258.   
  259.                 _gridTable = _girdDataSet.Tables["gridtable"];  
  260.   
  261.                 _bingingSource.DataSource = _gridTable;  
  262.   
  263.                 dataGridView.AutoGenerateColumns = false;  
  264.   
  265.                 dataGridView.EditMode = DataGridViewEditMode.EditOnEnter;  
  266.   
  267.                 dataGridView.DataSource = _bingingSource;  
  268.   
  269.                 _GridSource = _gridTable;  
  270.   
  271.   
  272.                 //初始化状态栏   
  273.                    _TotalRecords = Convert.ToInt32(_SqlParameter[0].Value);  
  274.   
  275.                 lblTotalRecords.Text = string.Format(_pagesumStr, new object[] { _TotalRecords });//记录总数   
  276.   
  277.                    _PageSum = _TotalRecords - _TotalRecords % _PageSize;  
  278.   
  279.                 _PageSum /= _PageSize;  
  280.   
  281.                 _PageSum++;  
  282.   
  283.                 lblPageCount.Text = string.Format(_currentpageStr, new object[] { _PageSum });//页数   
  284.               }  
  285.             catch (Exception CommonException)  
  286.             {  
  287.                 MessageBox.Show("窗口加载出错!" + CommonException.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);            </PRE>  
  288. </PRE>  
  289. <PRE class=csharp name="code"><PRE class=csharp name="code"><PRE class=csharp name="code">            }  
  290.         } </PRE>  
  291. <PRE class=csharp name="code"> </PRE>  
  292. <PRE class=csharp name="code"><FONT color=#008080>执行存储过程方法</FONT></PRE>  
  293. <PRE class=csharp name="code"> </PRE>  
  294. <PRE class=csharp name="code">         /// <summary>   
  295.         /// 执行存储过程   
  296.          /// </summary>   
  297.         /// <param name="storedProcName">存储过程名</param>   
  298.         /// <param name="parameters">存储过程参数</param>   
  299.         /// <param name="tableName">DataSet结果中的表名</param>   
  300.         /// <returns>DataSet</returns>   
  301.         public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName)  
  302.         {  
  303.             using (SqlConnection connection = dbconn.getDBConnection())  
  304.             {  
  305.                 DataSet dataSet = new DataSet();</PRE>  
  306. <PRE class=csharp name="code"> </PRE>  
  307. <PRE class=csharp name="code">                connection.Open();  
  308.   
  309.                 SqlDataAdapter sqlDA = new SqlDataAdapter();  
  310.   
  311.                 sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);  
  312.   
  313.                 sqlDA.Fill(dataSet, tableName);  
  314.   
  315.                 connection.Close();  
  316.   
  317.                 return dataSet;  
  318.             }  
  319.         }</PRE>  
  320. </PRE>  
  321. </PRE>  
view plaincopy to clipboardprint?
  1. <PRE class=csharp name="code"><FONT color=#000000>这样用户控件完成了,但如何调用呢? </FONT></PRE>  
  2. <PRE class=csharp name="code"><FONT color=#008080><FONT color=#000000>首先需要给参数赋值,其次,需要给DataGridView创建列</FONT> </FONT></PRE>  
  3. <PRE class=csharp name="code"><FONT color=#008080>参数赋值</FONT> </PRE>  
  4. <PRE class=csharp name="code"><PRE class=csharp name="code">        /// <summary>   
  5.         /// 设置DataGridView参数   
  6.          /// </summary>   
  7.         private void SetDataGridParameters()  
  8.         {  
  9.             dataGridViewControl.SetTableName = " sys_User ";  
  10.   
  11.             dataGridViewControl.SetTotalTableName = " sys_User ";  
  12.   
  13.             dataGridViewControl.SetTotalJoinStr = " LEFT JOIN sys_Awards ON sys_User.EmployeeNo = sys_Awards.UserID collate Chinese_PRC_CI_AI_WS ";  
  14.   
  15.             dataGridViewControl.SetTotalRecordsCondition = " sys_User.isVaild = 1 AND sys_Awards.Annual = '" + OverallYear + "'";              
  16.   
  17.             dataGridViewControl.SetFieldNames = " sys_User.[ID],sys_User.EmployeeNo,sys_User.UserName,sys_Duty.DutyName,sys_Dept.DeptName,sys_Awards.EndofYearAward,sys_Awards.HolidayAward ";  
  18.   
  19.             dataGridViewControl.SetJoinStr = " LEFT JOIN sys_Awards ON  sys_Awards.UserID = sys_User.EmployeeNo collate Chinese_PRC_CI_AI_WS"  
  20.   
  21.                                                          + " LEFT JOIN sys_Duty ON sys_User.Duty = sys_Duty.[ID]"  
  22.   
  23.                                                          + " LEFT JOIN sys_EmployeeDeploy ON sys_User.EmployeeNo = sys_EmployeeDeploy.UserID"  
  24.   
  25.                                                          + " LEFT JOIN sys_Dept ON sys_EmployeeDeploy.CurrentDept = sys_Dept.[ID] ";  
  26.   
  27.             dataGridViewControl.SetPrimaryField = " sys_User.[ID] ";  
  28.   
  29.             dataGridViewControl.SetConditionString = " sys_EmployeeDeploy.IsValid = 1 AND sys_User.isVaild = 1 AND sys_Awards.Annual = '" + OverallYear + "'";  
  30.   
  31.             dataGridViewControl.SetFilterString = "  sys_User.isVaild = 1 ";  
  32.   
  33.             dataGridViewControl.SetOrderField = " sys_User.[ID] ";  
  34.         } </PRE>  
  35. <PRE class=csharp name="code"><FONT color=#000000>创建DataGridView列</FONT>   
  36. </PRE>  
  37. <PRE class=csharp name="code">        /// <summary>   
  38.         /// 创建加列   
  39.          /// </summary>   
  40.         private void CreateGridColumns()  
  41.         {  
  42.             DataGridViewCell cell = new DataGridViewTextBoxCell();  
  43.   
  44.             DataGridViewColumn[] columns = new DataGridViewColumn[7];  
  45.   
  46.             columns[0] = new DataGridViewColumn();  
  47.   
  48.             columns[0].Name = "ID";  
  49.   
  50.             columns[0].HeaderText = "ID";  
  51.   
  52.             columns[0].DataPropertyName = "ID";  
  53.   
  54.             columns[0].CellTemplate = cell;  
  55.   
  56.             columns[0].ReadOnly = true;  
  57.   
  58.             columns[0].Visible = false;  
  59.   
  60.             columns[1] = new DataGridViewColumn();  
  61.   
  62.             columns[1].Name = "EmployeeNo";  
  63.   
  64.             columns[1].HeaderText = "员工编号";  
  65.   
  66.             columns[1].DataPropertyName = "EmployeeNo";  
  67.   
  68.             columns[1].CellTemplate = cell;  
  69.   
  70.             columns[1].ReadOnly = true;  
  71.   
  72.             columns[1].Visible = true;  
  73.   
  74.             columns[2] = new DataGridViewColumn();  
  75.   
  76.             columns[2].Name = "UserName";  
  77.   
  78.             columns[2].HeaderText = "员工姓名";  
  79.   
  80.             columns[2].DataPropertyName = "UserName";  
  81.   
  82.             columns[2].CellTemplate = cell;  
  83.   
  84.             columns[2].ReadOnly = true;  
  85.   
  86.             columns[2].Visible = true;  
  87.   
  88.             columns[3] = new DataGridViewColumn();  
  89.   
  90.             columns[3].Name = "DutyName";  
  91.   
  92.             columns[3].HeaderText = "职位";  
  93.   
  94.             columns[3].DataPropertyName = "DutyName";  
  95.   
  96.             columns[3].CellTemplate = cell;  
  97.   
  98.             columns[3].ReadOnly = true;  
  99.   
  100.             columns[3].Visible = true;  
  101.   
  102.             columns[4] = new DataGridViewColumn();  
  103.   
  104.             columns[4].Name = "DeptName";  
  105.   
  106.             columns[4].HeaderText = "部门";  
  107.   
  108.             columns[4].DataPropertyName = "DeptName";  
  109.   
  110.             columns[4].CellTemplate = cell;  
  111.   
  112.             columns[4].ReadOnly = true;  
  113.   
  114.             columns[4].Visible = true;  
  115.   
  116.             columns[5] = new DataGridViewColumn();  
  117.   
  118.             columns[5].Name = "EndofYearAward";  
  119.   
  120.             columns[5].HeaderText = "年终奖";  
  121.   
  122.             columns[5].DataPropertyName = "EndofYearAward";  
  123.   
  124.             columns[5].CellTemplate = cell;  
  125.   
  126.             columns[5].DefaultCellStyle.Format = "F";  
  127.   
  128.             columns[5].ReadOnly = false;  
  129.   
  130.             columns[5].Visible = true;  
  131.   
  132.             columns[6] = new DataGridViewColumn();  
  133.   
  134.             columns[6].Name = "HolidayAward";  
  135.   
  136.             columns[6].HeaderText = "假日奖";  
  137.   
  138.             columns[6].DataPropertyName = "HolidayAward";  
  139.   
  140.             columns[6].CellTemplate = cell;  
  141.   
  142.             columns[6].DefaultCellStyle.Format = "F";  
  143.   
  144.             columns[6].ReadOnly = false;  
  145.   
  146.             columns[6].Visible = true;  
  147.   
  148.             foreach (DataGridViewColumn column in columns)  
  149.             {  
  150.                 dataGridViewControl.dataGridView.Columns.Add(column);  
  151.             }  
  152.         } </PRE>  
  153. <PRE class=csharp name="code"> </PRE>  
  154. <PRE class=csharp name="code">当然了,这2个方法都是在页面Load的时候调用的   
  155. 写到这结束了,这个DataGridView分页控件,个人觉得只是满足了自己项目的需求,但是我想肯定还有不足的地方,或者编码不规范或者有更好的方法,</PRE>  
  156. <PRE class=csharp name="code"><PRE class=csharp name="code"><PRE class=csharp name="code">希望有经验的朋友能提出来,好让能改进改进,也可以从中学习下!~<IMG alt="" src="/Editor/FCKeditor/editor/images/smiley/msn/teeth_smile.gif"></PRE>  
  157. </PRE>  
  158. </PRE>  
  159. </PRE>  
原创粉丝点击