精华内容
下载资源
问答
  • winform表格分页控件

    2018-12-04 09:35:34
    支持各种表格控件分页,可自行修改不同的分页方式。。
  • ReoGrid .NET Spreadsheet Component C# 和 VB.net 开源免费的表格控件, 界面很漂亮, 功能很强大 其它的自己搜一下吧,当前版本是2.0的使用示例
  • 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git 如果...

    前提

    入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

    GitHub:https://github.com/kwwwvagaa/NetWinformControl

    码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

    如果觉得写的还行,请点个 star 支持一下吧

    欢迎前来交流探讨: 企鹅群568015492 企鹅群568015492

    目录

    https://blog.csdn.net/kwwwvagaa/article/details/100586547

    准备工作

    表格控件将拆分为2部分,1:行元素控件,2:列表控件

    为了具有更好的扩展性,更加的open,使用接口对行元素进行约束,当行样式或功能不满足你的需求的时候,可以自定义一个行元素,实现接口控件,然后将类型指定给列表控件即可

    表格控件用到了分页控件,如果你还没有对分页控件进行了解,请移步查看

    (十二)c#Winform自定义控件-分页控件

    开始

    定义一些辅助东西

    复制代码

    1  public class DataGridViewCellEntity
    2     {
    3         public string Title { get; set; }
    4         public int Width { get; set; }
    5         public System.Windows.Forms.SizeType WidthType { get; set; }
    6 
    7     }

    复制代码

    复制代码

    1     public class DataGridViewEventArgs : EventArgs
    2     {
    3         public Control CellControl { get; set; }
    4         public int CellIndex { get; set; }
    5         public int RowIndex { get; set; }
    6 
    7 
    8     }

    复制代码

    1     [Serializable]
    2     [ComVisible(true)]
    3     public delegate void DataGridViewEventHandler(object sender, DataGridViewEventArgs e);

    复制代码

    1   public class DataGridViewColumnEntity
    2     {
    3         public string HeadText { get; set; }
    4         public int Width { get; set; }
    5         public System.Windows.Forms.SizeType WidthType { get; set; }
    6         public string DataField { get; set; }
    7         public Func<object, string> Format { get; set; }
    8     }

    复制代码

    定义行接口

    复制代码

     1  public interface IDataGridViewRow
     2     {
     3         /// <summary>
     4         /// CheckBox选中事件
     5         /// </summary>
     6         event DataGridViewEventHandler CheckBoxChangeEvent;
     7         /// <summary>
     8         /// 点击单元格事件
     9         /// </summary>
    10         event DataGridViewEventHandler CellClick;
    11         /// <summary>
    12         /// 数据源改变事件
    13         /// </summary>
    14         event DataGridViewEventHandler SourceChanged;
    15         /// <summary>
    16         /// 列参数,用于创建列数和宽度
    17         /// </summary>
    18         List<DataGridViewColumnEntity> Columns { get; set; }
    19         bool IsShowCheckBox { get; set; }
    20         /// <summary>
    21         /// 是否选中
    22         /// </summary>
    23         bool IsChecked { get; set; }
    24 
    25         /// <summary>
    26         /// 数据源
    27         /// </summary>
    28         object DataSource { get; set; }
    29         /// <summary>
    30         /// 添加单元格元素,仅做添加控件操作,不做数据绑定,数据绑定使用BindingCells
    31         /// </summary>
    32         void ReloadCells();
    33         /// <summary>
    34         /// 绑定数据到Cell
    35         /// </summary>
    36         /// <param name="intIndex">cell下标</param>
    37         /// <returns>返回true则表示已处理过,否则将进行默认绑定(通常只针对有Text值的控件)</returns>
    38         void BindingCellData();
    39         /// <summary>
    40         /// 设置选中状态,通常为设置颜色即可
    41         /// </summary>
    42         /// <param name="blnSelected">是否选中</param>
    43         void SetSelect(bool blnSelected);
    44     }

    复制代码

    创建行控件

    添加一个用户控件,命名UCDataGridViewRow,实现接口IDataGridViewRow

    属性

    复制代码

     1   #region 属性
     2         public event DataGridViewEventHandler CheckBoxChangeEvent;
     3 
     4         public event DataGridViewEventHandler CellClick;
     5 
     6         public event DataGridViewEventHandler SourceChanged;
     7 
     8         public List<DataGridViewColumnEntity> Columns
     9         {
    10             get;
    11             set;
    12         }
    13 
    14         public object DataSource
    15         {
    16             get;
    17             set;
    18         }
    19 
    20         public bool IsShowCheckBox
    21         {
    22             get;
    23             set;
    24         }
    25         private bool m_isChecked;
    26         public bool IsChecked
    27         {
    28             get
    29             {
    30                 return m_isChecked;
    31             }
    32 
    33             set
    34             {
    35                 if (m_isChecked != value)
    36                 {
    37                     m_isChecked = value;
    38                     (this.panCells.Controls.Find("check", false)[0] as UCCheckBox).Checked = value;
    39                 }
    40             }
    41         }
    42 
    43 
    44         #endregion

    复制代码

    实现接口

    复制代码

      1    public void BindingCellData()
      2         {
      3             for (int i = 0; i < Columns.Count; i++)
      4             {
      5                 DataGridViewColumnEntity com = Columns[i];
      6                 var cs = this.panCells.Controls.Find("lbl_" + com.DataField, false);
      7                 if (cs != null && cs.Length > 0)
      8                 {
      9                     var pro = DataSource.GetType().GetProperty(com.DataField);
     10                     if (pro != null)
     11                     {
     12                         var value = pro.GetValue(DataSource, null);
     13                         if (com.Format != null)
     14                         {
     15                             cs[0].Text = com.Format(value);
     16                         }
     17                         else
     18                         {
     19                             cs[0].Text = value.ToStringExt();
     20                         }
     21                     }
     22                 }
     23             }
     24         }
     25 
     26  public void SetSelect(bool blnSelected)
     27         {
     28             if (blnSelected)
     29             {
     30                 this.BackColor = Color.FromArgb(255, 247, 245);
     31             }
     32             else
     33             {
     34                 this.BackColor = Color.Transparent;
     35             }
     36         }
     37 
     38         public void ReloadCells()
     39         {
     40             try
     41             {
     42                 ControlHelper.FreezeControl(this, true);
     43                 this.panCells.Controls.Clear();
     44                 this.panCells.ColumnStyles.Clear();
     45 
     46                 int intColumnsCount = Columns.Count();
     47                 if (Columns != null && intColumnsCount > 0)
     48                 {
     49                     if (IsShowCheckBox)
     50                     {
     51                         intColumnsCount++;
     52                     }
     53                     this.panCells.ColumnCount = intColumnsCount;
     54                     for (int i = 0; i < intColumnsCount; i++)
     55                     {
     56                         Control c = null;
     57                         if (i == 0 && IsShowCheckBox)
     58                         {
     59                             this.panCells.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(SizeType.Absolute, 30F));
     60 
     61                             UCCheckBox box = new UCCheckBox();
     62                             box.Name = "check";
     63                             box.TextValue = "";
     64                             box.Size = new Size(30, 30);
     65                             box.Dock = DockStyle.Fill;
     66                             box.CheckedChangeEvent += (a, b) =>
     67                             {
     68                                 IsChecked = box.Checked;
     69                                 if (CheckBoxChangeEvent != null)
     70                                 {
     71                                     CheckBoxChangeEvent(a, new DataGridViewEventArgs()
     72                                     {
     73                                         CellControl = box,
     74                                         CellIndex = 0
     75                                     });
     76                                 }
     77                             };
     78                             c = box;
     79                         }
     80                         else
     81                         {
     82                             var item = Columns[i - (IsShowCheckBox ? 1 : 0)];
     83                             this.panCells.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(item.WidthType, item.Width));
     84 
     85                             Label lbl = new Label();
     86                             lbl.Tag = i - (IsShowCheckBox ? 1 : 0);
     87                             lbl.Name = "lbl_" + item.DataField;
     88                             lbl.Font = new Font("微软雅黑", 12);
     89                             lbl.ForeColor = Color.Black;
     90                             lbl.AutoSize = false;
     91                             lbl.Dock = DockStyle.Fill;
     92                             lbl.TextAlign = ContentAlignment.MiddleCenter;
     93                             lbl.MouseDown += (a, b) =>
     94                             {
     95                                 Item_MouseDown(a, b);
     96                             };
     97                             c = lbl;
     98                         }
     99                         this.panCells.Controls.Add(c, i, 0);
    100                     }
    101 
    102                 }
    103             }
    104             finally
    105             {
    106                 ControlHelper.FreezeControl(this, false);
    107             }
    108         }

    复制代码

    节点选中事件

    复制代码

     1    void Item_MouseDown(object sender, MouseEventArgs e)
     2         {
     3             if (CellClick != null)
     4             {
     5                 CellClick(sender, new DataGridViewEventArgs()
     6                 {
     7                     CellControl = this,
     8                     CellIndex = (sender as Control).Tag.ToInt()
     9                 });
    10             }
    11         }

    复制代码

    完整的代码

     View Code

     View Code

     

    接下来就是列表控件了

    添加一个用户控件,命名UCDataGridView

    属性

    复制代码

      1  #region 属性
      2         private Font m_headFont = new Font("微软雅黑", 12F);
      3         /// <summary>
      4         /// 标题字体
      5         /// </summary>
      6         [Description("标题字体"), Category("自定义")]
      7         public Font HeadFont
      8         {
      9             get { return m_headFont; }
     10             set { m_headFont = value; }
     11         }
     12         private Color m_headTextColor = Color.Black;
     13         /// <summary>
     14         /// 标题字体颜色
     15         /// </summary>
     16         [Description("标题文字颜色"), Category("自定义")]
     17         public Color HeadTextColor
     18         {
     19             get { return m_headTextColor; }
     20             set { m_headTextColor = value; }
     21         }
     22 
     23         private bool m_isShowHead = true;
     24         /// <summary>
     25         /// 是否显示标题
     26         /// </summary>
     27         [Description("是否显示标题"), Category("自定义")]
     28         public bool IsShowHead
     29         {
     30             get { return m_isShowHead; }
     31             set
     32             {
     33                 m_isShowHead = value;
     34                 panHead.Visible = value;
     35                 if (m_page != null)
     36                 {
     37                     ResetShowCount();
     38                     m_page.PageSize = m_showCount;
     39                 }
     40             }
     41         }
     42         private int m_headHeight = 40;
     43         /// <summary>
     44         /// 标题高度
     45         /// </summary>
     46         [Description("标题高度"), Category("自定义")]
     47         public int HeadHeight
     48         {
     49             get { return m_headHeight; }
     50             set
     51             {
     52                 m_headHeight = value;
     53                 panHead.Height = value;
     54             }
     55         }
     56 
     57         private bool m_isShowCheckBox = false;
     58         /// <summary>
     59         /// 是否显示复选框
     60         /// </summary>
     61         [Description("是否显示选择框"), Category("自定义")]
     62         public bool IsShowCheckBox
     63         {
     64             get { return m_isShowCheckBox; }
     65             set
     66             {
     67                 if (value != m_isShowCheckBox)
     68                 {
     69                     m_isShowCheckBox = value;
     70                     LoadColumns();
     71                 }
     72             }
     73         }
     74 
     75         private int m_rowHeight = 40;
     76         /// <summary>
     77         /// 行高
     78         /// </summary>
     79         [Description("数据行高"), Category("自定义")]
     80         public int RowHeight
     81         {
     82             get { return m_rowHeight; }
     83             set { m_rowHeight = value; }
     84         }
     85 
     86         private int m_showCount = 0;
     87         /// <summary>
     88         /// 
     89         /// </summary>
     90         [Description("可显示个数"), Category("自定义")]
     91         public int ShowCount
     92         {
     93             get { return m_showCount; }
     94             private set
     95             {
     96                 m_showCount = value;
     97                 if (m_page != null)
     98                 {
     99                     m_page.PageSize = value;
    100                 }
    101             }
    102         }
    103 
    104         private List<DataGridViewColumnEntity> m_columns;
    105         /// <summary>
    106         /// 列
    107         /// </summary>
    108         [Description("列"), Category("自定义")]
    109         public List<DataGridViewColumnEntity> Columns
    110         {
    111             get { return m_columns; }
    112             set
    113             {
    114                 m_columns = value;
    115                 LoadColumns();
    116             }
    117         }
    118 
    119         private object m_dataSource;
    120         /// <summary>
    121         /// 数据源,支持列表或table,如果使用翻页控件,请使用翻页控件的DataSource
    122         /// </summary>
    123         [Description("数据源,支持列表或table,如果使用翻页控件,请使用翻页控件的DataSource"), Category("自定义")]
    124         public object DataSource
    125         {
    126             get { return m_dataSource; }
    127             set
    128             {
    129                 if (value == null)
    130                     return;
    131                 if (!(m_dataSource is DataTable) && (!typeof(IList).IsAssignableFrom(value.GetType())))
    132                 {
    133                     throw new Exception("数据源不是有效的数据类型,请使用Datatable或列表");
    134                 }
    135 
    136                 m_dataSource = value;
    137                 ReloadSource();
    138             }
    139         }
    140 
    141         public List<IDataGridViewRow> Rows { get; private set; }
    142 
    143         private Type m_rowType = typeof(UCDataGridViewRow);
    144         /// <summary>
    145         /// 行元素类型,默认UCDataGridViewItem
    146         /// </summary>
    147         [Description("行控件类型,默认UCDataGridViewRow,如果不满足请自定义行控件实现接口IDataGridViewRow"), Category("自定义")]
    148         public Type RowType
    149         {
    150             get { return m_rowType; }
    151             set
    152             {
    153                 if (value == null)
    154                     return;
    155                 if (!typeof(IDataGridViewRow).IsAssignableFrom(value) || !value.IsSubclassOf(typeof(Control)))
    156                     throw new Exception("行控件没有实现IDataGridViewRow接口");
    157                 m_rowType = value;
    158             }
    159         }
    160         IDataGridViewRow m_selectRow = null;
    161         /// <summary>
    162         /// 选中的节点
    163         /// </summary>
    164         [Description("选中行"), Category("自定义")]
    165         public IDataGridViewRow SelectRow
    166         {
    167             get { return m_selectRow; }
    168             private set { m_selectRow = value; }
    169         }
    170 
    171 
    172         /// <summary>
    173         /// 选中的行,如果显示CheckBox,则以CheckBox选中为准
    174         /// </summary>
    175         [Description("选中的行,如果显示CheckBox,则以CheckBox选中为准"), Category("自定义")]
    176         public List<IDataGridViewRow> SelectRows
    177         {
    178             get
    179             {
    180                 if (m_isShowCheckBox)
    181                 {
    182                     return Rows.FindAll(p => p.IsChecked);
    183                 }
    184                 else
    185                     return new List<IDataGridViewRow>() { m_selectRow };
    186             }
    187         }
    188 
    189 
    190         private UCPagerControlBase m_page = null;
    191         /// <summary>
    192         /// 翻页控件
    193         /// </summary>
    194         [Description("翻页控件,如果UCPagerControl不满足你的需求,请自定义翻页控件并继承UCPagerControlBase"), Category("自定义")]
    195         public UCPagerControlBase Page
    196         {
    197             get { return m_page; }
    198             set
    199             {
    200                 m_page = value;
    201                 if (value != null)
    202                 {
    203                     if (!typeof(IPageControl).IsAssignableFrom(value.GetType()) || !value.GetType().IsSubclassOf(typeof(UCPagerControlBase)))
    204                         throw new Exception("翻页控件没有继承UCPagerControlBase");
    205                     panPage.Visible = value != null;
    206                     m_page.ShowSourceChanged += page_ShowSourceChanged;
    207                     m_page.Dock = DockStyle.Fill;
    208                     this.panPage.Controls.Clear();
    209                     this.panPage.Controls.Add(m_page);
    210                     ResetShowCount();
    211                     m_page.PageSize = ShowCount;
    212                     this.DataSource = m_page.GetCurrentSource();
    213                 }
    214                 else
    215                 {
    216                     m_page = null;
    217                 }
    218             }
    219         }
    220 
    221         void page_ShowSourceChanged(object currentSource)
    222         {
    223             this.DataSource = currentSource;
    224         }
    225 
    226         #region 事件
    227         [Description("选中标题选择框事件"), Category("自定义")]
    228         public EventHandler HeadCheckBoxChangeEvent;
    229         [Description("标题点击事件"), Category("自定义")]
    230         public EventHandler HeadColumnClickEvent;
    231         [Description("项点击事件"), Category("自定义")]
    232         public event DataGridViewEventHandler ItemClick;
    233         [Description("数据源改变事件"), Category("自定义")]
    234         public event DataGridViewEventHandler SourceChanged;
    235         #endregion
    236         #endregion

    复制代码

    一些私有的方法

    复制代码

     1   #region 私有方法
     2         #region 加载column
     3         /// <summary>
     4         /// 功能描述:加载column
     5         /// 作  者:HZH
     6         /// 创建日期:2019-08-08 17:51:50
     7         /// 任务编号:POS
     8         /// </summary>
     9         private void LoadColumns()
    10         {
    11             try
    12             {
    13                 if (DesignMode)
    14                 { return; }
    15 
    16                 ControlHelper.FreezeControl(this.panHead, true);
    17                 this.panColumns.Controls.Clear();
    18                 this.panColumns.ColumnStyles.Clear();
    19 
    20                 if (m_columns != null && m_columns.Count() > 0)
    21                 {
    22                     int intColumnsCount = m_columns.Count();
    23                     if (m_isShowCheckBox)
    24                     {
    25                         intColumnsCount++;
    26                     }
    27                     this.panColumns.ColumnCount = intColumnsCount;
    28                     for (int i = 0; i < intColumnsCount; i++)
    29                     {
    30                         Control c = null;
    31                         if (i == 0 && m_isShowCheckBox)
    32                         {
    33                             this.panColumns.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(SizeType.Absolute, 30F));
    34 
    35                             UCCheckBox box = new UCCheckBox();
    36                             box.TextValue = "";
    37                             box.Size = new Size(30, 30);
    38                             box.CheckedChangeEvent += (a, b) =>
    39                             {
    40                                 Rows.ForEach(p => p.IsChecked = box.Checked);
    41                                 if (HeadCheckBoxChangeEvent != null)
    42                                 {
    43                                     HeadCheckBoxChangeEvent(a, b);
    44                                 }
    45                             };
    46                             c = box;
    47                         }
    48                         else
    49                         {
    50                             var item = m_columns[i - (m_isShowCheckBox ? 1 : 0)];
    51                             this.panColumns.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(item.WidthType, item.Width));
    52                             Label lbl = new Label();
    53                             lbl.Name = "dgvColumns_" + i;
    54                             lbl.Text = item.HeadText;
    55                             lbl.Font = m_headFont;
    56                             lbl.ForeColor = m_headTextColor;
    57                             lbl.TextAlign = ContentAlignment.MiddleCenter;
    58                             lbl.AutoSize = false;
    59                             lbl.Dock = DockStyle.Fill;
    60                             lbl.MouseDown += (a, b) =>
    61                             {
    62                                 if (HeadColumnClickEvent != null)
    63                                 {
    64                                     HeadColumnClickEvent(a, b);
    65                                 }
    66                             };
    67                             c = lbl;
    68                         }
    69                         this.panColumns.Controls.Add(c, i, 0);
    70                     }
    71 
    72                 }
    73             }
    74             finally
    75             {
    76                 ControlHelper.FreezeControl(this.panHead, false);
    77             }
    78         }
    79         #endregion
    80 
    81         /// <summary>
    82         /// 功能描述:获取显示个数
    83         /// 作  者:HZH
    84         /// 创建日期:2019-03-05 10:02:58
    85         /// 任务编号:POS
    86         /// </summary>
    87         /// <returns>返回值</returns>
    88         private void ResetShowCount()
    89         {
    90             if (DesignMode)
    91             { return; }
    92             ShowCount = this.panRow.Height / (m_rowHeight);
    93             int intCha = this.panRow.Height % (m_rowHeight);
    94             m_rowHeight += intCha / ShowCount;
    95         }
    96         #endregion

    复制代码

    几个事件

    复制代码

     1  #region 事件
     2         void RowSourceChanged(object sender, DataGridViewEventArgs e)
     3         {
     4             if (SourceChanged != null)
     5                 SourceChanged(sender, e);
     6         }
     7         private void SetSelectRow(Control item, DataGridViewEventArgs e)
     8         {
     9             try
    10             {
    11                 ControlHelper.FreezeControl(this, true);
    12                 if (item == null)
    13                     return;
    14                 if (item.Visible == false)
    15                     return;
    16                 this.FindForm().ActiveControl = this;
    17                 this.FindForm().ActiveControl = item;
    18                 if (m_selectRow != null)
    19                 {
    20                     if (m_selectRow == item)
    21                         return;
    22                     m_selectRow.SetSelect(false);
    23                 }
    24                 m_selectRow = item as IDataGridViewRow;
    25                 m_selectRow.SetSelect(true);
    26                 if (ItemClick != null)
    27                 {
    28                     ItemClick(item, e);
    29                 }
    30                 if (this.panRow.Controls.Count > 0)
    31                 {
    32                     if (item.Location.Y < 0)
    33                     {
    34                         this.panRow.AutoScrollPosition = new Point(0, Math.Abs(this.panRow.Controls[this.panRow.Controls.Count - 1].Location.Y) + item.Location.Y);
    35                     }
    36                     else if (item.Location.Y + m_rowHeight > this.panRow.Height)
    37                     {
    38                         this.panRow.AutoScrollPosition = new Point(0, Math.Abs(this.panRow.AutoScrollPosition.Y) + item.Location.Y - this.panRow.Height + m_rowHeight);
    39                     }
    40                 }
    41             }
    42             finally
    43             {
    44                 ControlHelper.FreezeControl(this, false);
    45             }
    46         }
    47         private void UCDataGridView_Resize(object sender, EventArgs e)
    48         {
    49             ResetShowCount();
    50             ReloadSource();
    51         }
    52         #endregion

    复制代码

    对外公开的函数

    复制代码

      1  #region 公共函数
      2         /// <summary>
      3         /// 刷新数据
      4         /// </summary>
      5         public void ReloadSource()
      6         {
      7             if (DesignMode)
      8             { return; }
      9             try
     10             {
     11                 if (m_columns == null || m_columns.Count <= 0)
     12                     return;
     13 
     14                 ControlHelper.FreezeControl(this.panRow, true);
     15                 this.panRow.Controls.Clear();
     16                 Rows = new List<IDataGridViewRow>();
     17                 if (m_dataSource != null)
     18                 {
     19                     int intIndex = 0;
     20                     Control lastItem = null;
     21 
     22                     int intSourceCount = 0;
     23                     if (m_dataSource is DataTable)
     24                     {
     25                         intSourceCount = (m_dataSource as DataTable).Rows.Count;
     26                     }
     27                     else if (typeof(IList).IsAssignableFrom(m_dataSource.GetType()))
     28                     {
     29                         intSourceCount = (m_dataSource as IList).Count;
     30                     }
     31 
     32                     foreach (Control item in this.panRow.Controls)
     33                     {
     34 
     35 
     36                         if (intIndex >= intSourceCount)
     37                         {
     38                             item.Visible = false;
     39                         }
     40                         else
     41                         {
     42                             var row = (item as IDataGridViewRow);
     43                             row.IsShowCheckBox = m_isShowCheckBox;
     44                             if (m_dataSource is DataTable)
     45                             {
     46                                 row.DataSource = (m_dataSource as DataTable).Rows[intIndex];
     47                             }
     48                             else
     49                             {
     50                                 row.DataSource = (m_dataSource as IList)[intIndex];
     51                             }
     52                             row.BindingCellData();
     53                             item.Height = m_rowHeight;
     54                             item.Visible = true;
     55                             item.BringToFront();
     56                             if (lastItem == null)
     57                                 lastItem = item;
     58                             Rows.Add(row);
     59                         }
     60                         intIndex++;
     61                     }
     62 
     63                     if (intIndex < intSourceCount)
     64                     {
     65                         for (int i = intIndex; i < intSourceCount; i++)
     66                         {
     67                             IDataGridViewRow row = (IDataGridViewRow)Activator.CreateInstance(m_rowType);
     68                             if (m_dataSource is DataTable)
     69                             {
     70                                 row.DataSource = (m_dataSource as DataTable).Rows[i];
     71                             }
     72                             else
     73                             {
     74                                 row.DataSource = (m_dataSource as IList)[i];
     75                             }
     76                             row.Columns = m_columns;
     77                             List<Control> lstCells = new List<Control>();
     78                             row.IsShowCheckBox = m_isShowCheckBox;
     79                             row.ReloadCells();
     80                             row.BindingCellData();
     81 
     82 
     83                             Control rowControl = (row as Control);
     84                             rowControl.Height = m_rowHeight;
     85                             this.panRow.Controls.Add(rowControl);
     86                             rowControl.Dock = DockStyle.Top;
     87                             row.CellClick += (a, b) => { SetSelectRow(rowControl, b); };
     88                             row.CheckBoxChangeEvent += (a, b) => { SetSelectRow(rowControl, b); };
     89                             row.SourceChanged += RowSourceChanged;
     90                             rowControl.BringToFront();
     91                             Rows.Add(row);
     92 
     93                             if (lastItem == null)
     94                                 lastItem = rowControl;
     95                         }
     96                     }
     97                     if (lastItem != null && intSourceCount == m_showCount)
     98                     {
     99                         lastItem.Height = this.panRow.Height - (m_showCount - 1) * m_rowHeight;
    100                     }
    101                 }
    102             }
    103             finally
    104             {
    105                 ControlHelper.FreezeControl(this.panRow, false);
    106             }
    107         }
    108 
    109 
    110         /// <summary>
    111         /// 快捷键
    112         /// </summary>
    113         /// <param name="msg"></param>
    114         /// <param name="keyData"></param>
    115         /// <returns></returns>
    116         protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    117         {
    118             if (keyData == Keys.Up)
    119             {
    120                 Previous();
    121             }
    122             else if (keyData == Keys.Down)
    123             {
    124                 Next();
    125             }
    126             else if (keyData == Keys.Home)
    127             {
    128                 First();
    129             }
    130             else if (keyData == Keys.End)
    131             {
    132                 End();
    133             }
    134             return base.ProcessCmdKey(ref msg, keyData);
    135         }
    136         /// <summary>
    137         /// 选中第一个
    138         /// </summary>
    139         public void First()
    140         {
    141             if (Rows == null || Rows.Count <= 0)
    142                 return;
    143             Control c = null;
    144             c = (Rows[0] as Control);
    145             SetSelectRow(c, new DataGridViewEventArgs() { RowIndex = 0 });
    146         }
    147         /// <summary>
    148         /// 选中上一个
    149         /// </summary>
    150         public void Previous()
    151         {
    152             if (Rows == null || Rows.Count <= 0)
    153                 return;
    154             Control c = null;
    155 
    156             int index = Rows.IndexOf(m_selectRow);
    157             if (index - 1 >= 0)
    158             {
    159                 c = (Rows[index - 1] as Control);
    160                 SetSelectRow(c, new DataGridViewEventArgs() { RowIndex = index - 1 });
    161             }
    162         }
    163         /// <summary>
    164         /// 选中下一个
    165         /// </summary>
    166         public void Next()
    167         {
    168             if (Rows == null || Rows.Count <= 0)
    169                 return;
    170             Control c = null;
    171 
    172             int index = Rows.IndexOf(m_selectRow);
    173             if (index + 1 < Rows.Count)
    174             {
    175                 c = (Rows[index + 1] as Control);
    176                 SetSelectRow(c, new DataGridViewEventArgs() { RowIndex = index + 1 });
    177             }
    178         }
    179         /// <summary>
    180         /// 选中最后一个
    181         /// </summary>
    182         public void End()
    183         {
    184             if (Rows == null || Rows.Count <= 0)
    185                 return;
    186             Control c = null;
    187             c = (Rows[Rows.Count - 1] as Control);
    188             SetSelectRow(c, new DataGridViewEventArgs() { RowIndex = Rows.Count - 1 });
    189         }
    190 
    191         #endregion

    复制代码

    完整代码

    // 版权所有  黄正辉  交流群:568015492   QQ:623128629
    // 文件名称:UCDataGridView.cs
    // 创建日期:2019-08-15 15:59:25
    // 功能描述:DataGridView
    // 项目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections;
    
    namespace HZH_Controls.Controls
    {
        public partial class UCDataGridView : UserControl
        {
            #region 属性
            private Font m_headFont = new Font("微软雅黑", 12F);
            /// <summary>
            /// 标题字体
            /// </summary>
            [Description("标题字体"), Category("自定义")]
            public Font HeadFont
            {
                get { return m_headFont; }
                set { m_headFont = value; }
            }
            private Color m_headTextColor = Color.Black;
            /// <summary>
            /// 标题字体颜色
            /// </summary>
            [Description("标题文字颜色"), Category("自定义")]
            public Color HeadTextColor
            {
                get { return m_headTextColor; }
                set { m_headTextColor = value; }
            }
    
            private bool m_isShowHead = true;
            /// <summary>
            /// 是否显示标题
            /// </summary>
            [Description("是否显示标题"), Category("自定义")]
            public bool IsShowHead
            {
                get { return m_isShowHead; }
                set
                {
                    m_isShowHead = value;
                    panHead.Visible = value;
                    if (m_page != null)
                    {
                        ResetShowCount();
                        m_page.PageSize = m_showCount;
                    }
                }
            }
            private int m_headHeight = 40;
            /// <summary>
            /// 标题高度
            /// </summary>
            [Description("标题高度"), Category("自定义")]
            public int HeadHeight
            {
                get { return m_headHeight; }
                set
                {
                    m_headHeight = value;
                    panHead.Height = value;
                }
            }
    
            private bool m_isShowCheckBox = false;
            /// <summary>
            /// 是否显示复选框
            /// </summary>
            [Description("是否显示选择框"), Category("自定义")]
            public bool IsShowCheckBox
            {
                get { return m_isShowCheckBox; }
                set
                {
                    if (value != m_isShowCheckBox)
                    {
                        m_isShowCheckBox = value;
                        LoadColumns();
                    }
                }
            }
    
            private int m_rowHeight = 40;
            /// <summary>
            /// 行高
            /// </summary>
            [Description("数据行高"), Category("自定义")]
            public int RowHeight
            {
                get { return m_rowHeight; }
                set { m_rowHeight = value; }
            }
    
            private int m_showCount = 0;
            /// <summary>
            /// 
            /// </summary>
            [Description("可显示个数"), Category("自定义")]
            public int ShowCount
            {
                get { return m_showCount; }
                private set
                {
                    m_showCount = value;
                    if (m_page != null)
                    {
                        m_page.PageSize = value;
                    }
                }
            }
    
            private List<DataGridViewColumnEntity> m_columns;
            /// <summary>
            /// 列
            /// </summary>
            [Description("列"), Category("自定义")]
            public List<DataGridViewColumnEntity> Columns
            {
                get { return m_columns; }
                set
                {
                    m_columns = value;
                    LoadColumns();
                }
            }
    
            private object m_dataSource;
            /// <summary>
            /// 数据源,支持列表或table,如果使用翻页控件,请使用翻页控件的DataSource
            /// </summary>
            [Description("数据源,支持列表或table,如果使用翻页控件,请使用翻页控件的DataSource"), Category("自定义")]
            public object DataSource
            {
                get { return m_dataSource; }
                set
                {
                    if (value == null)
                        return;
                    if (!(m_dataSource is DataTable) && (!typeof(IList).IsAssignableFrom(value.GetType())))
                    {
                        throw new Exception("数据源不是有效的数据类型,请使用Datatable或列表");
                    }
    
                    m_dataSource = value;
                    ReloadSource();
                }
            }
    
            public List<IDataGridViewRow> Rows { get; private set; }
    
            private Type m_rowType = typeof(UCDataGridViewRow);
            /// <summary>
            /// 行元素类型,默认UCDataGridViewItem
            /// </summary>
            [Description("行控件类型,默认UCDataGridViewRow,如果不满足请自定义行控件实现接口IDataGridViewRow"), Category("自定义")]
            public Type RowType
            {
                get { return m_rowType; }
                set
                {
                    if (value == null)
                        return;
                    if (!typeof(IDataGridViewRow).IsAssignableFrom(value) || !value.IsSubclassOf(typeof(Control)))
                        throw new Exception("行控件没有实现IDataGridViewRow接口");
                    m_rowType = value;
                }
            }
            IDataGridViewRow m_selectRow = null;
            /// <summary>
            /// 选中的节点
            /// </summary>
            [Description("选中行"), Category("自定义")]
            public IDataGridViewRow SelectRow
            {
                get { return m_selectRow; }
                private set { m_selectRow = value; }
            }
    
    
            /// <summary>
            /// 选中的行,如果显示CheckBox,则以CheckBox选中为准
            /// </summary>
            [Description("选中的行,如果显示CheckBox,则以CheckBox选中为准"), Category("自定义")]
            public List<IDataGridViewRow> SelectRows
            {
                get
                {
                    if (m_isShowCheckBox)
                    {
                        return Rows.FindAll(p => p.IsChecked);
                    }
                    else
                        return new List<IDataGridViewRow>() { m_selectRow };
                }
            }
    
    
            private UCPagerControlBase m_page = null;
            /// <summary>
            /// 翻页控件
            /// </summary>
            [Description("翻页控件,如果UCPagerControl不满足你的需求,请自定义翻页控件并继承UCPagerControlBase"), Category("自定义")]
            public UCPagerControlBase Page
            {
                get { return m_page; }
                set
                {
                    m_page = value;
                    if (value != null)
                    {
                        if (!typeof(IPageControl).IsAssignableFrom(value.GetType()) || !value.GetType().IsSubclassOf(typeof(UCPagerControlBase)))
                            throw new Exception("翻页控件没有继承UCPagerControlBase");
                        panPage.Visible = value != null;
                        m_page.ShowSourceChanged += page_ShowSourceChanged;
                        m_page.Dock = DockStyle.Fill;
                        this.panPage.Controls.Clear();
                        this.panPage.Controls.Add(m_page);
                        ResetShowCount();
                        m_page.PageSize = ShowCount;
                        this.DataSource = m_page.GetCurrentSource();
                    }
                    else
                    {
                        m_page = null;
                    }
                }
            }
    
            void page_ShowSourceChanged(object currentSource)
            {
                this.DataSource = currentSource;
            }
    
            #region 事件
            [Description("选中标题选择框事件"), Category("自定义")]
            public EventHandler HeadCheckBoxChangeEvent;
            [Description("标题点击事件"), Category("自定义")]
            public EventHandler HeadColumnClickEvent;
            [Description("项点击事件"), Category("自定义")]
            public event DataGridViewEventHandler ItemClick;
            [Description("数据源改变事件"), Category("自定义")]
            public event DataGridViewEventHandler SourceChanged;
            #endregion
            #endregion
    
            public UCDataGridView()
            {
                InitializeComponent();
            }
    
            #region 私有方法
            #region 加载column
            /// <summary>
            /// 功能描述:加载column
            /// 作  者:HZH
            /// 创建日期:2019-08-08 17:51:50
            /// 任务编号:POS
            /// </summary>
            private void LoadColumns()
            {
                try
                {
                    if (DesignMode)
                    { return; }
    
                    ControlHelper.FreezeControl(this.panHead, true);
                    this.panColumns.Controls.Clear();
                    this.panColumns.ColumnStyles.Clear();
    
                    if (m_columns != null && m_columns.Count() > 0)
                    {
                        int intColumnsCount = m_columns.Count();
                        if (m_isShowCheckBox)
                        {
                            intColumnsCount++;
                        }
                        this.panColumns.ColumnCount = intColumnsCount;
                        for (int i = 0; i < intColumnsCount; i++)
                        {
                            Control c = null;
                            if (i == 0 && m_isShowCheckBox)
                            {
                                this.panColumns.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(SizeType.Absolute, 30F));
    
                                UCCheckBox box = new UCCheckBox();
                                box.TextValue = "";
                                box.Size = new Size(30, 30);
                                box.CheckedChangeEvent += (a, b) =>
                                {
                                    Rows.ForEach(p => p.IsChecked = box.Checked);
                                    if (HeadCheckBoxChangeEvent != null)
                                    {
                                        HeadCheckBoxChangeEvent(a, b);
                                    }
                                };
                                c = box;
                            }
                            else
                            {
                                var item = m_columns[i - (m_isShowCheckBox ? 1 : 0)];
                                this.panColumns.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(item.WidthType, item.Width));
                                Label lbl = new Label();
                                lbl.Name = "dgvColumns_" + i;
                                lbl.Text = item.HeadText;
                                lbl.Font = m_headFont;
                                lbl.ForeColor = m_headTextColor;
                                lbl.TextAlign = ContentAlignment.MiddleCenter;
                                lbl.AutoSize = false;
                                lbl.Dock = DockStyle.Fill;
                                lbl.MouseDown += (a, b) =>
                                {
                                    if (HeadColumnClickEvent != null)
                                    {
                                        HeadColumnClickEvent(a, b);
                                    }
                                };
                                c = lbl;
                            }
                            this.panColumns.Controls.Add(c, i, 0);
                        }
    
                    }
                }
                finally
                {
                    ControlHelper.FreezeControl(this.panHead, false);
                }
            }
            #endregion
    
            /// <summary>
            /// 功能描述:获取显示个数
            /// 作  者:HZH
            /// 创建日期:2019-03-05 10:02:58
            /// 任务编号:POS
            /// </summary>
            /// <returns>返回值</returns>
            private void ResetShowCount()
            {
                if (DesignMode)
                { return; }
                ShowCount = this.panRow.Height / (m_rowHeight);
                int intCha = this.panRow.Height % (m_rowHeight);
                m_rowHeight += intCha / ShowCount;
            }
            #endregion
    
            #region 公共函数
            /// <summary>
            /// 刷新数据
            /// </summary>
            public void ReloadSource()
            {
                if (DesignMode)
                { return; }
                try
                {
                    if (m_columns == null || m_columns.Count <= 0)
                        return;
    
                    ControlHelper.FreezeControl(this.panRow, true);
                    this.panRow.Controls.Clear();
                    Rows = new List<IDataGridViewRow>();
                    if (m_dataSource != null)
                    {
                        int intIndex = 0;
                        Control lastItem = null;
    
                        int intSourceCount = 0;
                        if (m_dataSource is DataTable)
                        {
                            intSourceCount = (m_dataSource as DataTable).Rows.Count;
                        }
                        else if (typeof(IList).IsAssignableFrom(m_dataSource.GetType()))
                        {
                            intSourceCount = (m_dataSource as IList).Count;
                        }
    
                        foreach (Control item in this.panRow.Controls)
                        {
    
    
                            if (intIndex >= intSourceCount)
                            {
                                item.Visible = false;
                            }
                            else
                            {
                                var row = (item as IDataGridViewRow);
                                row.IsShowCheckBox = m_isShowCheckBox;
                                if (m_dataSource is DataTable)
                                {
                                    row.DataSource = (m_dataSource as DataTable).Rows[intIndex];
                                }
                                else
                                {
                                    row.DataSource = (m_dataSource as IList)[intIndex];
                                }
                                row.BindingCellData();
                                item.Height = m_rowHeight;
                                item.Visible = true;
                                item.BringToFront();
                                if (lastItem == null)
                                    lastItem = item;
                                Rows.Add(row);
                            }
                            intIndex++;
                        }
    
                        if (intIndex < intSourceCount)
                        {
                            for (int i = intIndex; i < intSourceCount; i++)
                            {
                                IDataGridViewRow row = (IDataGridViewRow)Activator.CreateInstance(m_rowType);
                                if (m_dataSource is DataTable)
                                {
                                    row.DataSource = (m_dataSource as DataTable).Rows[i];
                                }
                                else
                                {
                                    row.DataSource = (m_dataSource as IList)[i];
                                }
                                row.Columns = m_columns;
                                List<Control> lstCells = new List<Control>();
                                row.IsShowCheckBox = m_isShowCheckBox;
                                row.ReloadCells();
                                row.BindingCellData();
    
    
                                Control rowControl = (row as Control);
                                rowControl.Height = m_rowHeight;
                                this.panRow.Controls.Add(rowControl);
                                rowControl.Dock = DockStyle.Top;
                                row.CellClick += (a, b) => { SetSelectRow(rowControl, b); };
                                row.CheckBoxChangeEvent += (a, b) => { SetSelectRow(rowControl, b); };
                                row.SourceChanged += RowSourceChanged;
                                rowControl.BringToFront();
                                Rows.Add(row);
    
                                if (lastItem == null)
                                    lastItem = rowControl;
                            }
                        }
                        if (lastItem != null && intSourceCount == m_showCount)
                        {
                            lastItem.Height = this.panRow.Height - (m_showCount - 1) * m_rowHeight;
                        }
                    }
                }
                finally
                {
                    ControlHelper.FreezeControl(this.panRow, false);
                }
            }
    
    
            /// <summary>
            /// 快捷键
            /// </summary>
            /// <param name="msg"></param>
            /// <param name="keyData"></param>
            /// <returns></returns>
            protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
            {
                if (keyData == Keys.Up)
                {
                    Previous();
                }
                else if (keyData == Keys.Down)
                {
                    Next();
                }
                else if (keyData == Keys.Home)
                {
                    First();
                }
                else if (keyData == Keys.End)
                {
                    End();
                }
                return base.ProcessCmdKey(ref msg, keyData);
            }
            /// <summary>
            /// 选中第一个
            /// </summary>
            public void First()
            {
                if (Rows == null || Rows.Count <= 0)
                    return;
                Control c = null;
                c = (Rows[0] as Control);
                SetSelectRow(c, new DataGridViewEventArgs() { RowIndex = 0 });
            }
            /// <summary>
            /// 选中上一个
            /// </summary>
            public void Previous()
            {
                if (Rows == null || Rows.Count <= 0)
                    return;
                Control c = null;
    
                int index = Rows.IndexOf(m_selectRow);
                if (index - 1 >= 0)
                {
                    c = (Rows[index - 1] as Control);
                    SetSelectRow(c, new DataGridViewEventArgs() { RowIndex = index - 1 });
                }
            }
            /// <summary>
            /// 选中下一个
            /// </summary>
            public void Next()
            {
                if (Rows == null || Rows.Count <= 0)
                    return;
                Control c = null;
    
                int index = Rows.IndexOf(m_selectRow);
                if (index + 1 < Rows.Count)
                {
                    c = (Rows[index + 1] as Control);
                    SetSelectRow(c, new DataGridViewEventArgs() { RowIndex = index + 1 });
                }
            }
            /// <summary>
            /// 选中最后一个
            /// </summary>
            public void End()
            {
                if (Rows == null || Rows.Count <= 0)
                    return;
                Control c = null;
                c = (Rows[Rows.Count - 1] as Control);
                SetSelectRow(c, new DataGridViewEventArgs() { RowIndex = Rows.Count - 1 });
            }
    
            #endregion
    
            #region 事件
            void RowSourceChanged(object sender, DataGridViewEventArgs e)
            {
                if (SourceChanged != null)
                    SourceChanged(sender, e);
            }
            private void SetSelectRow(Control item, DataGridViewEventArgs e)
            {
                try
                {
                    ControlHelper.FreezeControl(this, true);
                    if (item == null)
                        return;
                    if (item.Visible == false)
                        return;
                    this.FindForm().ActiveControl = this;
                    this.FindForm().ActiveControl = item;
                    if (m_selectRow != null)
                    {
                        if (m_selectRow == item)
                            return;
                        m_selectRow.SetSelect(false);
                    }
                    m_selectRow = item as IDataGridViewRow;
                    m_selectRow.SetSelect(true);
                    if (ItemClick != null)
                    {
                        ItemClick(item, e);
                    }
                    if (this.panRow.Controls.Count > 0)
                    {
                        if (item.Location.Y < 0)
                        {
                            this.panRow.AutoScrollPosition = new Point(0, Math.Abs(this.panRow.Controls[this.panRow.Controls.Count - 1].Location.Y) + item.Location.Y);
                        }
                        else if (item.Location.Y + m_rowHeight > this.panRow.Height)
                        {
                            this.panRow.AutoScrollPosition = new Point(0, Math.Abs(this.panRow.AutoScrollPosition.Y) + item.Location.Y - this.panRow.Height + m_rowHeight);
                        }
                    }
                }
                finally
                {
                    ControlHelper.FreezeControl(this, false);
                }
            }
            private void UCDataGridView_Resize(object sender, EventArgs e)
            {
                ResetShowCount();
                ReloadSource();
            }
            #endregion
        }
    }
    namespace HZH_Controls.Controls
    {
        partial class UCDataGridView
        {
            /// <summary> 
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary> 
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region 组件设计器生成的代码
    
            /// <summary> 
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.panHead = new System.Windows.Forms.Panel();
                this.panColumns = new System.Windows.Forms.TableLayoutPanel();
                this.ucSplitLine_H1 = new HZH_Controls.Controls.UCSplitLine_H();
                this.panRow = new System.Windows.Forms.Panel();
                this.panPage = new System.Windows.Forms.Panel();
                this.panHead.SuspendLayout();
                this.SuspendLayout();
                // 
                // panHead
                // 
                this.panHead.Controls.Add(this.panColumns);
                this.panHead.Controls.Add(this.ucSplitLine_H1);
                this.panHead.Dock = System.Windows.Forms.DockStyle.Top;
                this.panHead.Location = new System.Drawing.Point(0, 0);
                this.panHead.Name = "panHead";
                this.panHead.Size = new System.Drawing.Size(1061, 40);
                this.panHead.TabIndex = 0;
                // 
                // panColumns
                // 
                this.panColumns.ColumnCount = 1;
                this.panColumns.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
                this.panColumns.Dock = System.Windows.Forms.DockStyle.Fill;
                this.panColumns.Location = new System.Drawing.Point(0, 0);
                this.panColumns.Name = "panColumns";
                this.panColumns.RowCount = 1;
                this.panColumns.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
                this.panColumns.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
                this.panColumns.Size = new System.Drawing.Size(1061, 39);
                this.panColumns.TabIndex = 1;
                // 
                // ucSplitLine_H1
                // 
                this.ucSplitLine_H1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(232)))), ((int)(((byte)(232)))), ((int)(((byte)(232)))));
                this.ucSplitLine_H1.Dock = System.Windows.Forms.DockStyle.Bottom;
                this.ucSplitLine_H1.Location = new System.Drawing.Point(0, 39);
                this.ucSplitLine_H1.Name = "ucSplitLine_H1";
                this.ucSplitLine_H1.Size = new System.Drawing.Size(1061, 1);
                this.ucSplitLine_H1.TabIndex = 0;
                this.ucSplitLine_H1.TabStop = false;
                // 
                // panRow
                // 
                this.panRow.AutoScroll = true;
                this.panRow.Dock = System.Windows.Forms.DockStyle.Fill;
                this.panRow.Location = new System.Drawing.Point(0, 40);
                this.panRow.Name = "panRow";
                this.panRow.Size = new System.Drawing.Size(1061, 475);
                this.panRow.TabIndex = 1;
                // 
                // panPage
                // 
                this.panPage.Dock = System.Windows.Forms.DockStyle.Bottom;
                this.panPage.Location = new System.Drawing.Point(0, 515);
                this.panPage.Name = "panPage";
                this.panPage.Size = new System.Drawing.Size(1061, 50);
                this.panPage.TabIndex = 0;
                this.panPage.Visible = false;
                // 
                // UCDataGridView
                // 
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
                this.BackColor = System.Drawing.Color.White;
                this.Controls.Add(this.panRow);
                this.Controls.Add(this.panPage);
                this.Controls.Add(this.panHead);
                this.Name = "UCDataGridView";
                this.Size = new System.Drawing.Size(1061, 565);
                this.Resize += new System.EventHandler(this.UCDataGridView_Resize);
                this.panHead.ResumeLayout(false);
                this.ResumeLayout(false);
    
            }
    
            #endregion
    
            private System.Windows.Forms.Panel panHead;
            private System.Windows.Forms.TableLayoutPanel panColumns;
            private UCSplitLine_H ucSplitLine_H1;
            private System.Windows.Forms.Panel panRow;
            private System.Windows.Forms.Panel panPage;
    
        }
    }

    如果你仔细看,你会发现行我用了类型进行传入,当你需要更丰富的行内容的时候,可以自定义行控件,然后通过RowType属性传入

    分页控件我使用了分页控件基类UCPagerControlBase,这样做的好处就是你同样可以扩展分页控件

    用处及效果

    调用示例

    复制代码

     1  List<DataGridViewColumnEntity> lstCulumns = new List<DataGridViewColumnEntity>();
     2             lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "ID", HeadText = "编号", Width = 70, WidthType = SizeType.Absolute });
     3             lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Name", HeadText = "姓名", Width = 50, WidthType = SizeType.Percent });
     4             lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Age", HeadText = "年龄", Width = 50, WidthType = SizeType.Percent });
     5             lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Birthday", HeadText = "生日", Width = 50, WidthType = SizeType.Percent, Format = (a) => { return ((DateTime)a).ToString("yyyy-MM-dd"); } });
     6             lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Sex", HeadText = "性别", Width = 50, WidthType = SizeType.Percent, Format = (a) => { return ((int)a) == 0 ? "女" : "男"; } });
     7             this.ucDataGridView1.Columns = lstCulumns;
     8             this.ucDataGridView1.IsShowCheckBox = true;
     9             List<object> lstSource = new List<object>();
    10             for (int i = 0; i < 200; i++)
    11             {
    12                 TestModel model = new TestModel()
    13                 {
    14                     ID = i.ToString(),
    15                     Age = 3 * i,
    16                     Name = "姓名——" + i,
    17                     Birthday = DateTime.Now.AddYears(-10),
    18                     Sex = i % 2
    19                 };
    20                 lstSource.Add(model);
    21             }
    22 
    23             var page = new UCPagerControl2();
    24             page.DataSource = lstSource;
    25             this.ucDataGridView1.Page = page;
    26             this.ucDataGridView1.First();

    复制代码

    如果使用分页控件,则将数据源指定给分页控件,否则直接指定给表格控件数据源

    最后的话

    如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星 星吧

    展开全文
  • XPTable-winform很强大的表格控件,可以实现很多DATAGRIDVIEW没有的功能,如进度条,图片,下拉框等很多
  • WinForm例子\控件透明背景及自定义控件实现无边框按钮和可加背景的表格控件
  • Xceed Grid For .NET是一款高级的,多功能的、扩展性极强的数据表格控件,具有分组、主从表、多种主题外观、固定列和行、Excel导出、支持Vista风格,交互的外观样式,内嵌报表功能,支持导出为PDF、HTML、TIFF、JPEG...

    Xceed Grid For .NET是一款高级的,多功能的、扩展性极强的数据表格控件,具有分组、主从表、多种主题外观、固定列和行、Excel导出、支持Vista风格,交互的外观样式,内嵌报表功能,支持导出为PDF、HTML、TIFF、JPEG,以及打印报表等多种功能,是现在业界最强大的表格控件。

    特殊功能:

    • 控件使用Cell UI虚拟化,大大提高了表格数据加载性能,减少了内存花销,表格加载10列10000数据行比以前快了3倍,内存花销少了55%。
    • 如果控件使用懒加载加载数据,比以前快了15.75倍
    • 该控件被Microsoft Dynamics in Microsoft Office所使用
    • 完全所见所得的设计时表格编辑器,完全设计时设计表头、列,分组,主从表、插入行等操作
    • 可以指定任何Windows Form控件作为单元格编辑器
    • 控件提供的所有类都是可继承的,开发人员完全可以根据自己需要扩展表格功能
    • 每一个可视的表格元素都是独立的对象,具有自己特殊行为,像一个独立的子控件:单元格、行、列、分组、行选择器,表头等,都具有自己的鼠标、快捷键、绘制事件,可以被自定义对象所取代
    • 表格可以作为一个强大的绑定和非绑定树型控件所使用,不限制层和子元素
    • 各式各样专业的行、单元格以及管理对象允许你合并文本以及图像
    • 提供了两种方法进行自定义绘制表格元素:一种是添加代码到控件原来的绘制事件里,二是自己继承重写表格绘制事件
    • 控件为表格提供了多种外观样式:Office 2007 blue, Office 2007 silver
    • 表格支持导出为Excel文件
    • 可以控制导出的电子表格样式:列头样式、表格单元格显示、导出表格样式

    报表功能:

    • 控件集成了报表引擎,用户可以从表格程序生成、修改、打印报表,对于开发人员添加该功能只需要几分钟
    • 支持导出报表为PDF, HTML, Multi-page TIFF, JPEG
    • 基于矢量的打印:Xceed Grid for .NET不像其他表格控件一样发送实际的表格位图到打印机,而是发送基于矢量的数据到打印机
    • 开发人员仅需要一句代码就可以添加“产生报表”以及“自定义报表样式”功能到您的表格中,使用户可以在打印和导出报表前修改报表样式
    • 最终用户还可以为所有报表元素设置打印外观,选择颜色、字体,添加图片,定义哪些字段可以打印,字段宽度,自定义表头、表尾区域。
    • 所有报表功能,开发人员都可以通过代码控制
    • 样式表可以保存为XML文件或者加载已有的XML样式表
    • 自定义表头、表尾:添加文字、图片,标题,各种日期格式,为页数设置宏命令等
    • 完全的列格式化:支持多列跨越多行,单元格合并
    • 页跨越:当有太多字段在一页时,字段可以跨越到多页
    • 控件提供了统计行:内嵌的14种统计学函数,该统计行可以放置在页尾,或者每个分组的末尾或者是顶端,或者是你想要放的位置
    • 任何行在每页都可以重复:特别是列头行、分组行头、统计行
    • 支持在任何分组或者行间插入空白块
    • 支持在行、表头、分组、统计行等前面或者后面添加水平横线

    主要功能:

    • 内嵌数据输入验证
    • 滚动时固定列
    • 支持OutLook分组样式或者自定义分组样式
    • 支持每行或者层具有多个子表
    • 支持列或者多列排序
    • 支持滚动表格到任何单元格、行、列、分组
    • 快速显示、快速排序
    • 单击进入编辑模式
    • 实时滚动并且和滚动条同步
    • 支持改变每个单元格样式以及格式
    • 支持插入行
    • 当滚动时,支持固定表头,表尾
    • 打印和打印预览
    • 支持为表格设置背景图片
    • 支持添加自定义金融函数到单元格
    • 数据库和表格间数据同步
    • 表格每个可视元素都完全支持鼠标事件、快捷键、拖拉操作
    • 表格元素只读模式
    • 数据过滤
    • 单元格支持内嵌多种编辑器:buttons, resizeable combobox, multi-column combobox, date picker, multi-month resizeable calendar, resizeable and masked textbox, numeric textbox, calculator, check boxes, scrollbars,  panel building block
    • 单元格输入自动匹配功能
    • 支持添加、复制、删除、取消、拖拉操作
    • 支持显示,编辑,工作于绑定数据以及非绑定数据或者是两个的集合
    • 主从表支持绑定、非绑定数据
    • 自动获得和同步绑定数据从任何.NET源:数组、IListSource、IList、DataTable、DataSet、DataView、DataViewManager对象等

     控件技术:

    • 控件完全由C#编写,100%可托管
    • 包含了丰富的Visual Studio 2005 和Visual Studio 2008的VB.NET和C#事例
    • 控件针对.NET framework面向对象设计
    • 提供了完整的帮助文档,完全集成于Visual Studio
    • 兼容CLS
    原文来自龙博方案网http://www.fanganwang.com/product/1200转载请注明出处
    Xceed Grid For .NET
    Xceed Grid For .NET

    产品下载由龙博方案网www.fanganwang.com提供

    XceedSoftware 试用版完整代码  (307 MB)

    试用版下载

    转载于:https://www.cnblogs.com/platfarm/p/3722865.html

    展开全文
  • 控件用于处理数据表格,可以进行一些常用的处理方式,例如分页显示功能,以及处理一些常用的技巧 :例如可以对不同的数据行进行颜色的处理
  • C# WinForm TreeListView控件用法

    热门讨论 2011-07-28 10:30:02
    C# WinForm TreeListView控件用法初步
  • 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git 如果...

    前提

    入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

    GitHub:https://github.com/kwwwvagaa/NetWinformControl

    码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

    如果觉得写的还行,请点个 star 支持一下吧

    欢迎前来交流探讨: 企鹅群568015492 企鹅群568015492

    麻烦博客下方点个【推荐】,谢谢

    NuGet

    Install-Package HZH_Controls

    目录

    https://blog.csdn.net/kwwwvagaa/article/details/100586547

    用处及效果

    复制代码

     1   List<DataGridViewColumnEntity> lstCulumns = new List<DataGridViewColumnEntity>();
     2             lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "ID", HeadText = "编号", Width = 70, WidthType = SizeType.Absolute });
     3             lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Name", HeadText = "姓名", Width = 100, WidthType = SizeType.Absolute });
     4             lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Age", HeadText = "年龄", Width = 100, WidthType = SizeType.Absolute });
     5             lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Birthday", HeadText = "生日", Width = 120, WidthType = SizeType.Absolute, Format = (a) => { return ((DateTime)a).ToString("yyyy-MM-dd"); } });
     6             lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Sex", HeadText = "性别", Width = 100, WidthType = SizeType.Absolute, Format = (a) => { return ((int)a) == 0 ? "女" : "男"; } });
     7             this.ucComboxGrid1.GridColumns = lstCulumns;
     8             List<object> lstSourceGrid = new List<object>();
     9             for (int i = 0; i < 100; i++)
    10             {
    11                 TestModel model = new TestModel()
    12                 {
    13                     ID = i.ToString(),
    14                     Age = 3 * i,
    15                     Name = "姓名——" + i,
    16                     Birthday = DateTime.Now.AddYears(-10),
    17                     Sex = i % 2
    18                 };
    19                 lstSourceGrid.Add(model);
    20             }
    21             this.ucComboxGrid1.GridDataSource = lstSourceGrid;

    复制代码

     

    准备工作

    此控件继承自UCCombox,并且需要表格控件UCDataGridView,如果不了解请移步查看

    (三十五)c#Winform自定义控件-下拉框

    (三十二)c#Winform自定义控件-表格

    如果你想显示树形结构,请移步查看

    (四十七)c#Winform自定义控件-树表格(treeGrid)

    开始

    我们首先需要一个弹出的面板,那么我们先添加一个用户控件UCComboxGridPanel,在这个用户控件上添加一个文本框进行搜索,添加一个表格展示数据

    一些属性

    复制代码

     1  [Description("项点击事件"), Category("自定义")]
     2         public event DataGridViewEventHandler ItemClick;
     3         private Type m_rowType = typeof(UCDataGridViewRow);
     4 
     5         public Type RowType
     6         {
     7             get { return m_rowType; }
     8             set
     9             {
    10                 m_rowType = value;
    11                 this.ucDataGridView1.RowType = m_rowType;
    12             }
    13         }
    14 
    15         private List<DataGridViewColumnEntity> m_columns = null;
    16 
    17         public List<DataGridViewColumnEntity> Columns
    18         {
    19             get { return m_columns; }
    20             set
    21             {
    22                 m_columns = value;
    23                 this.ucDataGridView1.Columns = value;
    24             }
    25         }
    26         private List<object> m_dataSource = null;
    27 
    28         public List<object> DataSource
    29         {
    30             get { return m_dataSource; }
    31             set { m_dataSource = value; }
    32         }
    33 
    34         private string strLastSearchText = string.Empty;
    35         UCPagerControl m_page = new UCPagerControl();

    复制代码

    一些事件,处理数据绑定

    复制代码

     1  void ucDataGridView1_ItemClick(object sender, DataGridViewEventArgs e)
     2         {
     3             if (ItemClick != null)
     4             {
     5                 ItemClick((sender as IDataGridViewRow).DataSource, null);
     6             }
     7         }
     8 
     9         void txtInput_TextChanged(object sender, EventArgs e)
    10         {
    11             timer1.Enabled = false;
    12             timer1.Enabled = true;
    13         }
    14 
    15         private void UCComboxGridPanel_Load(object sender, EventArgs e)
    16         {
    17             m_page.DataSource = m_dataSource;
    18             this.ucDataGridView1.DataSource = m_page.GetCurrentSource();
    19         }
    20 
    21         private void timer1_Tick(object sender, EventArgs e)
    22         {
    23             if (strLastSearchText == txtSearch.InputText)
    24             {
    25                 timer1.Enabled = false;
    26             }
    27             else
    28             {
    29                 strLastSearchText = txtSearch.InputText;
    30                 Search(txtSearch.InputText);
    31             }
    32         }
    33 
    34         private void Search(string strText)
    35         {
    36             m_page.StartIndex = 0;
    37             if (!string.IsNullOrEmpty(strText))
    38             {
    39                 strText = strText.ToLower();
    40                 List<object> lst = m_dataSource.FindAll(p => m_columns.Any(c => (c.Format == null ? (p.GetType().GetProperty(c.DataField).GetValue(p, null).ToStringExt()) : c.Format(p.GetType().GetProperty(c.DataField).GetValue(p, null))).ToLower().Contains(strText)));
    41                 m_page.DataSource = lst;
    42             }
    43             else
    44             {
    45                 m_page.DataSource = m_dataSource;
    46             }
    47             m_page.Reload();
    48         }

    复制代码

    完整代码

     View Code

     View Code

    以上,弹出面板完成,下面就是下拉控件了

    添加一个用户控件UCComboxGrid,继承UCCombox

    一些属性

    复制代码

     1  private Type m_rowType = typeof(UCDataGridViewRow);
     2 
     3         [Description("表格行类型"), Category("自定义")]
     4         public Type GridRowType
     5         {
     6             get { return m_rowType; }
     7             set
     8             {
     9                 m_rowType = value;
    10             }
    11         }
    12         int intWidth = 0;
    13 
    14         private List<DataGridViewColumnEntity> m_columns = null;
    15 
    16         [Description("表格列"), Category("自定义")]
    17         public List<DataGridViewColumnEntity> GridColumns
    18         {
    19             get { return m_columns; }
    20             set
    21             {
    22                 m_columns = value;
    23                 if (value != null)
    24                     intWidth = value.Sum(p => p.WidthType == SizeType.Absolute ? p.Width : (p.Width < 80 ? 80 : p.Width));
    25             }
    26         }
    27         private List<object> m_dataSource = null;
    28 
    29         [Description("表格数据源"), Category("自定义")]
    30         public List<object> GridDataSource
    31         {
    32             get { return m_dataSource; }
    33             set { m_dataSource = value; }
    34         }
    35 
    36         private string m_textField;
    37 
    38         [Description("显示值字段名称"), Category("自定义")]
    39         public string TextField
    40         {
    41             get { return m_textField; }
    42             set
    43             {
    44                 m_textField = value;
    45                 SetText();
    46             }
    47         }
    48         [Obsolete("不再可用的属性")]
    49         [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    50         private new ComboBoxStyle BoxStyle
    51         {
    52             get;
    53             set;
    54         }
    55         private object selectSource = null;
    56 
    57         [Description("选中的数据源"), Category("自定义")]
    58         public object SelectSource
    59         {
    60             get { return selectSource; }
    61             set
    62             {
    63                 selectSource = value;
    64                 SetText();
    65                 if (SelectedChangedEvent != null)
    66                 {
    67                     SelectedChangedEvent(value, null);
    68                 }
    69             }
    70         }
    71 
    72  [Description("选中数据源改变事件"), Category("自定义")]
    73         public new event EventHandler SelectedChangedEvent;

    复制代码

    重写点击事件来处理弹出

    复制代码

     1  protected override void click_MouseDown(object sender, MouseEventArgs e)
     2         {
     3             if (m_columns == null || m_columns.Count <= 0)
     4                 return;
     5             if (m_ucPanel == null)
     6             {
     7                 var p = this.Parent.PointToScreen(this.Location);
     8                 int intScreenHeight = Screen.PrimaryScreen.Bounds.Height;
     9                 int intHeight = Math.Max(p.Y, intScreenHeight - p.Y - this.Height);
    10                 intHeight -= 100;
    11                 m_ucPanel = new UCComboxGridPanel();
    12                 m_ucPanel.ItemClick += m_ucPanel_ItemClick;
    13                 m_ucPanel.Height = intHeight;
    14                 m_ucPanel.Width = intWidth;
    15                 m_ucPanel.Columns = m_columns;
    16                 m_ucPanel.RowType = m_rowType;
    17                 if (m_dataSource != null && m_dataSource.Count > 0)
    18                 {
    19                     int _intHeight = Math.Min(110 + m_dataSource.Count * 36, m_ucPanel.Height);
    20                     m_ucPanel.Height = _intHeight;
    21                 }
    22             }
    23             m_ucPanel.DataSource = m_dataSource;
    24             if (_frmAnchor == null || _frmAnchor.IsDisposed || _frmAnchor.Visible == false)
    25             {
    26                 _frmAnchor = new Forms.FrmAnchor(this, m_ucPanel);
    27                 _frmAnchor.Show(this.FindForm());
    28             }
    29 
    30         }
    31 
    32         void m_ucPanel_ItemClick(object sender, DataGridViewEventArgs e)
    33         {
    34             _frmAnchor.Hide();
    35             SelectSource = sender;
    36         }
    37 
    38         private void SetText()
    39         {
    40             if (!string.IsNullOrEmpty(m_textField) && selectSource != null)
    41             {
    42                 var pro = selectSource.GetType().GetProperty(m_textField);
    43                 if (pro != null)
    44                 {
    45                     TextValue = pro.GetValue(selectSource, null).ToStringExt();
    46                 }
    47             }
    48         }

    复制代码

    完整代码

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using HZH_Controls.Controls;
    
    namespace HZH_Controls.Controls.ComboBox
    {
        public partial class UCComboxGrid : UCCombox
        {
    
            private Type m_rowType = typeof(UCDataGridViewRow);
    
            [Description("表格行类型"), Category("自定义")]
            public Type GridRowType
            {
                get { return m_rowType; }
                set
                {
                    m_rowType = value;
                }
            }
            int intWidth = 0;
    
            private List<DataGridViewColumnEntity> m_columns = null;
    
            [Description("表格列"), Category("自定义")]
            public List<DataGridViewColumnEntity> GridColumns
            {
                get { return m_columns; }
                set
                {
                    m_columns = value;
                    if (value != null)
                        intWidth = value.Sum(p => p.WidthType == SizeType.Absolute ? p.Width : (p.Width < 80 ? 80 : p.Width));
                }
            }
            private List<object> m_dataSource = null;
    
            [Description("表格数据源"), Category("自定义")]
            public List<object> GridDataSource
            {
                get { return m_dataSource; }
                set { m_dataSource = value; }
            }
    
            private string m_textField;
    
            [Description("显示值字段名称"), Category("自定义")]
            public string TextField
            {
                get { return m_textField; }
                set
                {
                    m_textField = value;
                    SetText();
                }
            }
            [Obsolete("不再可用的属性")]
            [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
            private new ComboBoxStyle BoxStyle
            {
                get;
                set;
            }
            private object selectSource = null;
    
            [Description("选中的数据源"), Category("自定义")]
            public object SelectSource
            {
                get { return selectSource; }
                set
                {
                    selectSource = value;
                    SetText();
                    if (SelectedChangedEvent != null)
                    {
                        SelectedChangedEvent(value, null);
                    }
                }
            }
    
          
    
            [Description("选中数据源改变事件"), Category("自定义")]
            public new event EventHandler SelectedChangedEvent;
            public UCComboxGrid()
            {
                InitializeComponent();
            }
            UCComboxGridPanel m_ucPanel = null;
            Forms.FrmAnchor _frmAnchor;
            protected override void click_MouseDown(object sender, MouseEventArgs e)
            {
                if (m_columns == null || m_columns.Count <= 0)
                    return;
                if (m_ucPanel == null)
                {
                    var p = this.Parent.PointToScreen(this.Location);
                    int intScreenHeight = Screen.PrimaryScreen.Bounds.Height;
                    int intHeight = Math.Max(p.Y, intScreenHeight - p.Y - this.Height);
                    intHeight -= 100;
                    m_ucPanel = new UCComboxGridPanel();
                    m_ucPanel.ItemClick += m_ucPanel_ItemClick;
                    m_ucPanel.Height = intHeight;
                    m_ucPanel.Width = intWidth;
                    m_ucPanel.Columns = m_columns;
                    m_ucPanel.RowType = m_rowType;
                    if (m_dataSource != null && m_dataSource.Count > 0)
                    {
                        int _intHeight = Math.Min(110 + m_dataSource.Count * 36, m_ucPanel.Height);
                        m_ucPanel.Height = _intHeight;
                    }
                }
                m_ucPanel.DataSource = m_dataSource;
                if (_frmAnchor == null || _frmAnchor.IsDisposed || _frmAnchor.Visible == false)
                {
                    _frmAnchor = new Forms.FrmAnchor(this, m_ucPanel);
                    _frmAnchor.Show(this.FindForm());
                }
    
            }
    
            void m_ucPanel_ItemClick(object sender, DataGridViewEventArgs e)
            {
                _frmAnchor.Hide();
                SelectSource = sender;
            }
    
            private void SetText()
            {
                if (!string.IsNullOrEmpty(m_textField) && selectSource != null)
                {
                    var pro = selectSource.GetType().GetProperty(m_textField);
                    if (pro != null)
                    {
                        TextValue = pro.GetValue(selectSource, null).ToStringExt();
                    }
                }
            }
        }
    }
    namespace HZH_Controls.Controls.ComboBox
    {
        partial class UCComboxGrid
        {
            /// <summary> 
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary> 
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region 组件设计器生成的代码
    
            /// <summary> 
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.SuspendLayout();
                // 
                // txtInput
                // 
                this.txtInput.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(240)))), ((int)(((byte)(240)))), ((int)(((byte)(240)))));
                // 
                // UCComboxGrid
                // 
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
                this.BackColor = System.Drawing.Color.Transparent;
                this.BoxStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
                this.FillColor = System.Drawing.Color.FromArgb(((int)(((byte)(240)))), ((int)(((byte)(240)))), ((int)(((byte)(240)))));
                this.Name = "UCComboxGrid";
                this.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(240)))), ((int)(((byte)(240)))), ((int)(((byte)(240)))));
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            #endregion
    
        }
    }

    以上就是全部东西了

    最后的话

    如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧

    展开全文
  • Winform分页控件之纯分页显示处理

    千次阅读 2018-08-15 16:39:50
    在之前介绍的Winform分页控件中,都以分页控件+显示表格控件作为一个整体性的控件,不可分开,这样做的目的是可以实现更多的操作,集成更多丰富的特性,减少我们开发的工作量,这种情况虽然适用于大多数的情
    原文地址为:Winform分页控件之纯分页显示处理

    在之前介绍的Winform分页控件中,都以分页控件+显示表格控件作为一个整体性的控件,不可分开,这样做的目的是可以实现更多的操作,集成更多丰富的特性,减少我们开发的工作量,这种情况虽然适用于大多数的情况,不过有时候需要更进一步对列表控件进行设置修改或者实现一些特殊的显示效果的时候,这种绑定列表控件的分页控件方式就有点捉襟见肘了。为了解决这个问题,我对分页控件进行稍微的调整,使其支持分页和列表控件分开的情况,具体实现以及效果是如何的呢,下面我们来分析了解下。

    1、单独的分页信息控件

    在我的开发设计过程总,实现一些关键的属性及操作后,它就可以作为一个独立的分页控件进行使用了。

    运行的时候效果如下所示:

    2、分页控件的使用

    在实际使用的时候,我们把分页控件的dll导入到工具栏里面,然后我们在VS的控件集合里面就可以看到它的图标了。拖动到界面上就实现了分页控件的布局显示了。

    由于这个纯分页没有列表控件,这个列表就需要自己放到你的界面上去,由于列表控件如DataGridView(传统控件)或者GridControl(DevExpress控件)是独立的,因此可以自由设计调整,列表的样式以及事件处理都合分页控件没有必然的联系。

    使用代码如下所示:

           private void FrmCustomerPager_Load(object sender, EventArgs e)
            {
                this.pager1.PageChanged += new WHC.Pager.WinControl.PageChangedEventHandler(pager1_PageChanged);
                this.pager1.ExportCurrent += new WHC.Pager.WinControl.ExportCurrentEventHandler(pager1_ExportCurrent);
                this.pager1.ExportAll += new WHC.Pager.WinControl.ExportAllEventHandler(pager1_ExportAll);
    
                BindData();
            }
    
            void pager1_PageChanged(object sender, EventArgs e)
            {
                BindData();
            }
    
            void pager1_ExportCurrent(object sender, EventArgs e)
            {
                MessageUtil.ShowTips("导出当前页");
            }
    
            void pager1_ExportAll(object sender, EventArgs e)
            {
                MessageUtil.ShowTips("导出所有");
            }        
            
            
            /// <summary>
            /// 标准的记录查询函数
            /// </summary>
            /// <param name="where"></param>
            /// <param name="pagerInfo"></param>
            /// <returns></returns>
            private DataTable FindToDataTable(string where, PagerInfo pagerInfo)
            {
                WHC.Pager.WinControl.PagerHelper helper = new WHC.Pager.WinControl.PagerHelper("All_Customer", "*", "LastUpdated", pagerInfo.PageSize, pagerInfo.CurrenetPageIndex, true, where);
                string countSql = helper.GetPagingSql(WHC.Pager.WinControl.DatabaseType.Access, true);
                string dataSql = helper.GetPagingSql(WHC.Pager.WinControl.DatabaseType.Access, false);
    
                string value = SqlValueList(countSql);
                pagerInfo.RecordCount = Convert.ToInt32(value);//为了显示具体的信息,需要设置总记录数
    
                DataTable dt = SqlTable(dataSql);
                return dt;
            }
    
            /// <summary>
            /// 根据查询条件构造查询语句
            /// </summary>
            /// <returns></returns>
            private string GetSearchSql()
            {
                SearchCondition condition = new SearchCondition();
                condition.AddCondition("Name", this.txtName.Text, SqlOperator.Like)
                    .AddCondition("Type", this.cmbType.Text, SqlOperator.Like)
                    .AddCondition("Area", this.cmbArea.Text, SqlOperator.Like);
                string where = condition.BuildConditionSql().Replace("Where", "");
                return where;
            }
            #endregion
    
            private void BindData()
            {
                string where = GetSearchSql();
                this.pager1.PageSize = 30;
                DataTable dt = FindToDataTable(where, this.pager1.PagerInfo);            
                this.gridControl1.DataSource = dt;
            }
    
            private void btnSearch_Click(object sender, EventArgs e)
            {
                BindData();
            }

    从上面的代码,我们可以看到,使用起来还是很方便简洁的,关键代码就是下面这几行:

           private void FrmCustomerPager_Load(object sender, EventArgs e)
            {
                this.pager1.PageChanged += new WHC.Pager.WinControl.PageChangedEventHandler(pager1_PageChanged);
                this.pager1.ExportCurrent += new WHC.Pager.WinControl.ExportCurrentEventHandler(pager1_ExportCurrent);
                this.pager1.ExportAll += new WHC.Pager.WinControl.ExportAllEventHandler(pager1_ExportAll);
    
                BindData();
            }
    
            private void BindData()
            {
                string where = GetSearchSql();
                this.pager1.PageSize = 30;
                DataTable dt = FindToDataTable(where, this.pager1.PagerInfo);            
                this.gridControl1.DataSource = dt;
            }

    这样独立的纯粹的分页,给了列表展示控件最大的弹性,可以在一些复合列、复杂列表中提供很好的整合效果,弥补一些特殊分页碰到的问题。

    除了DevExpress样式,还提供了传统界面样式、DotNetBar界面样式几种效果,如下所示。


    转载请注明本文地址:Winform分页控件之纯分页显示处理
    展开全文
  • 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git 如果...
  • C# 《WinForm程序设计与实践》 对此书中的章节 4.2 WinForm常用控件 进行笔记和编写代码实例,提供CSDN记录与分享。
  • 那么下面就介绍一些我们在表格控件里常用的方便的控件:  1、Grid表格查询条 Grid表格下拉  3、Grid表格弹框选择控件ButtonEdit。  4、Grid表格下拉搜索控件  5、Grid表格下拉多选控件 1.Grid表格查询条...
  • 微软自带winform图表控件样例大全开源免费,饼状图,雷达图,折线图,柱状图,实时曲线图等等,附件为微软的工具库自带的Chart图表的简单应用
  • winform控件ListView

    2017-04-29 15:43:00
    winform控件ListView 使用ListView构建表格展示数据 1、添加列数据:控件ListView--上方按钮--视图(Details)--编辑列--添加 2、添加行数据:--编辑项(行)--添加 添加数据:Text:默认添加每...
  • winform容器控件

    2017-04-27 16:06:00
    容器控件:  两个比较重要的布局属性: Anchor:填充布局 Dock:锁定布局(一般与容器控件配合使用)  Panel:类似于DIV,里面可以放任何控件,可以独立布局,还可以让其它控件及容器在它的内部再次布局  ...
  • winform 容器控件

    2017-05-03 15:24:00
    Anchor:填充布局 Dock:锁定布局(一般与容器控件配合使用)  Panel:类似于DIV,里面可以放任何控件,可以独立布局,还可以让其它控件及容器在它的内部再次布局  FlowLayoutPanel:相比Panel,此控件额外...
  • c#Winform自定义控件-目录

    千次阅读 多人点赞 2019-09-06 20:59:24
    入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git 如果...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 5,089
精华内容 2,035
关键字:

winform表格控件图片