grid 控件的重要性无需多说了,但要找一个好用的却是难上加难

虽然市面上有很多成熟公司的成熟产品, 但那些东西毕竟太贵了,所以还是自己写比较实在(什么, 破解版?还是算了吧)

自vs2005 以后,vs 系统自带了一个 DataGridView 控件, 这个控件的功能是很强大的,扩展性也不错, 所以我们就以它为基础吧

开始统计需要扩展的功能先:

1.显示行号

2.加入可以输入文字的 DataGridViewComboBoxCell

2.可分组折叠

3.合并单元格

4.Undo/Redo的支持

5.其他。。。。。

定义类

    /// <summary>
    
/// 扩展的 DataGridView
    
/// </summary>
    public class DataGridViewEx : DataGridView
    {
    }

 

先来一个最简单的:显示行号

这里我们用到了一个事件 RowPostPaint, 查看MSDN后可知该事件是在“绘制 DataGridViewRow 后发生” 

DataGridView 在绘制 DataGridViewRow 时没有处理行号, 那就由 DataGridViewEx 来处理吧

知道了原理,添加行号就很简单了, DrawString 就OK。

给出主要的代码实现 :

        void DataGridViewEx_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            
if (showRowHeaderNumbers)
            {
                
string title = (e.RowIndex + 1).ToString();
                Brush bru 
= Brushes.Black;
                e.Graphics.DrawString(title, DefaultCellStyle.Font,
                    bru, e.RowBounds.Location.X 
+ RowHeadersWidth / 2 - 4, e.RowBounds.Location.Y + 4);
            }
        }

 

完成后的效果:

扩展 DataGridView 的功能(一)

代码下载


相关文章:

  • 2022-12-23
  • 2021-12-08
  • 2021-12-04
  • 2021-11-09
  • 2021-12-04
  • 2021-07-08
  • 2021-06-13
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-07-09
  • 2021-06-01
  • 2021-09-25
  • 2021-09-05
  • 2021-08-08
相关资源
相似解决方案