zoukankan      html  css  js  c++  java
  • DataGridView显示行号

    可以做成扩展控件,这里是主要代码:

    方法一:

    复制代码
    private void dataGridView2_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
            {
                
    using (var brush = new
     SolidBrush(dataGridView2.RowHeadersDefaultCellStyle.ForeColor))
                {
                    e.Graphics.DrawString((e.RowIndex 
    + 1).ToString(), dataGridView2.DefaultCellStyle.Font, brush, e.RowBounds.Location.X + 12, e.RowBounds.Y + 5
    );
                }
            }
    复制代码

    方法二:

    复制代码
    private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
            {
                
    if (e.ColumnIndex == -1 && e.RowIndex >= 0 && e.RowIndex <
     dataGridView1.Rows.Count)
                {
                    
    //dataGridView1.Rows[e.RowIndex].HeaderCell.Value = (e.RowIndex).ToString();

                    e.PaintBackground(e.ClipBounds, true);
                    e.Graphics.DrawString((e.RowIndex 
    + 1).ToString(), Font, Brushes.Black, e.CellBounds.Left + 6
    ,
                                          e.CellBounds.Top 
    + 5
    );
                    e.Handled 
    = true
    ;
                }
            }
    复制代码

    方法三:继承DataGridView扩展为自定义控件

    复制代码
    public partial class DataGridViewEx : DataGridView
        {
            
    bool showRowHeaderNumbers;

            
    /// <summary>
            
    /// 是否显示行号
            
    /// </summary>
            [Category("扩展属性"), Description("是否显示行号"), DefaultValue(false)]
            
    public bool ShowRowHeaderNumbers
            {
                
    get { return showRowHeaderNumbers; }
                
    set
                {
                    
    if (showRowHeaderNumbers != value)
                        Invalidate();
                    showRowHeaderNumbers 
    = value;
                }
                
    //get;
                
    //set;
            }

            
    public DataGridViewEx()
            {
                InitializeComponent();
            }

            
    protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
            {
                
    if (ShowRowHeaderNumbers)
                {
                    
    string title = (e.RowIndex + 1).ToString();
                    Brush brush 
    = Brushes.Black;
                    e.Graphics.DrawString(title, DefaultCellStyle.Font, brush, e.RowBounds.Location.X 
    + RowHeadersWidth / 2 - 4, e.RowBounds.Location.Y + 4);
                }

                
    base.OnRowPostPaint(e);
            }
        }
  • 相关阅读:
    MS CRM 2011 RC中的新特性(4)——活动方面之批量编辑、自定义活动
    最近的一些有关MS CRM 2011的更新
    MS CRM 2011 RC中的新特性(6)——连接
    MS CRM 2011 RC中的新特性(7)—仪表板
    参加MS CRM2011深度培训课程——第一天
    MS CRM 2011插件调试工具
    MS CRM2011实体介绍(四)——目标管理方面的实体
    MS CRM 2011 RC中的新特性(3)——客户服务管理方面
    MS CRM 2011 RC中的新特性(8)—数据管理
    ExtAspNet 登陆
  • 原文地址:https://www.cnblogs.com/ewyb/p/2590006.html
Copyright © 2011-2022 走看看