zoukankan      html  css  js  c++  java
  • 在DataGridView中添加行号

    转载:http://blog.csdn.net/webwx/archive/2007/03/21/1536090.aspx
    今天用C#2.0开发一个WinForm项目时,碰到一个在DataGridView中加行号的问题,找了一些资料,终于搞定。现把它贴出来供大家参考。
      参考:http://community.csdn.net/Expert/topic/4671/4671416.xml?temp=.1845667
            这里提到了两种方法:
            一、在数据加载后,用下面的代码:
    1                for (int i = 0; i < DataGridView1.Rows.Count; i++)
    2                {
    3                    int j = i + 1;
    4                    DataGridView1.Rows[i].HeaderCell.Value = j.ToString();
    5                }

    6                DataGridView1.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);

    这样当窗体第一次加载显示时,行号不会出现,于是试了第二种方法,加上自己改了一点。
      二、在DataGridView的RowPostPaint事件中加入下面的代码:

     1        private void DataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
     2        {
     3            Color color = DataGridView1.RowHeadersDefaultCellStyle.ForeColor;
     4            if (DataGridView1.Rows[e.RowIndex].Selected)
     5                color = DataGridView1.RowHeadersDefaultCellStyle.SelectionForeColor;
     6            else
     7                color = DataGridView1.RowHeadersDefaultCellStyle.ForeColor;
     8
     9            using (SolidBrush b = new SolidBrush(color))
    10            {
    11                e.Graphics.DrawString((e.RowIndex+1).ToString(), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X+20, e.RowBounds.Location.Y+6);
    12            }

    13        }

    网上还有:

     void dgrid_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
            {
                SolidBrush B 
    = new SolidBrush(Color.Red);
                
    //or:SolidBrush b = new SolidBrush(this.dataGridView1.RowHeadersDefaultCellStyle.ForeColor)
                e.Graphics.DrawString(Convert.ToString(e.RowIndex + 1, System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, B, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
            }

  • 相关阅读:
    【洛谷P1005】矩阵取数游戏
    【洛谷P1966】火柴排队
    【题解】洛谷P1731 [NOI1999] 生日蛋糕(搜索+剪枝)
    【题解】洛谷P3627 [APIO2009]抢掠计划(缩点+SPFA)
    【题解】洛谷P1262 间谍网络 (强连通分量缩点)
    【题解】洛谷P3200 [HNOI2009] 有趣的数列(卡特兰数+质因数分解)
    点双连通分量 [HNOI2012]矿场搭建
    分块 公主的朋友
    [置顶] 数学的坑,一点点来填
    大暴搜 [NOIP2009]靶形数独
  • 原文地址:https://www.cnblogs.com/myjece/p/1272717.html
Copyright © 2011-2022 走看看