有很多种方法。

1、可以在DataGridView控件中的RowStateChanged事件改变行标题单元格的值(Row.HeaderCell.Value)

         /// <summary>
/// 行状态更改时发生
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
    //e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);
e.Row.HeaderCell.Value = (e.Row.Index + ).ToString();//添加行号
}

2、可以在DataGridView控件中的RowPostPaint事件例进行设置,TextRenderer类的DrawText()方法使用指定的设备上下文、字体、颜色和格式说明在指定界限中绘制指定文本。

         /// <summary>
/// 所有单元格绘制后,执行 行绘制时发生
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
//
System.Drawing.Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Y, this.dataGridView1.RowHeadersWidth - , this.dataGridView1.ColumnHeadersHeight);
TextRenderer.DrawText(e.Graphics, (e.RowIndex + ).ToString(), this.dataGridView1.RowHeadersDefaultCellStyle.Font, rectangle, this.dataGridView1.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);//”TextFormatFlags.VerticalCenter | TextFormatFlags.Right“中“|”有增加的作用,此处添加了两种文本字符格式样式
}

dev 的DevExpress.XtraGrid.Views.Grid.GridView控件添加行号:

        /// <summary>
/// GridView 显示行号 设置行号列的宽度
/// </summary>
/// <param name="gv">GridView 控件名称</param>
/// <param name="width">行号列的宽度 如果为null或为0 默认为30</param>
public void DrawRowIndicator(DevExpress.XtraGrid.Views.Grid.GridView gv, int width)
{
gv.CustomDrawRowIndicator += new DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventHandler(gv_CustomDrawRowIndicator);
//if (width != null)
//{
if (width != )
{
gv.IndicatorWidth = width;
}
else
{
gv.IndicatorWidth = ;
}
//}
//else
//{
// gv.IndicatorWidth = 30;
//}
}
/// <summary>
/// 行号设置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void gv_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
{
if (e.Info.IsRowIndicator && e.RowHandle > -)
{
e.Info.DisplayText = (e.RowHandle + ).ToString();
}
}

Winform 中DataGridView控件添加行标题的更多相关文章

  1. Winform中 DataGridView控件中的 CheckBox 的值读出来 始终 为 False ,已解决

    private void DGV_DetailsViewer_CellContentClick(object sender, DataGridViewCellEventArgs e) { )) { D ...

  2. Winform中checklistbox控件的常用方法

    Winform中checklistbox控件的常用方法最近用到checklistbox控件,在使用其过程中,收集了其相关的代码段1.添加项checkedListBox1.Items.Add(" ...

  3. 为 DataGridView 控件添加行号

    虽然好像不经常用到,不过还是记下来防止以后用到 /// <summary> /// 为 DataGridView 控件添加行号 /// </summary> /// <p ...

  4. [C#]WinForm 中 comboBox控件之数据绑定

    [C#]WinForm 中 comboBox控件之数据绑定 一.IList 现在我们直接创建一个List集合,然后绑定 IList<string> list = new List<s ...

  5. Winform中Treeview控件失去焦点,将选择的节点设置为高亮显示 (2012-07-16 13:47:07)转载▼

    Winform中Treeview控件失去焦点,将选择的节点设置为高亮显示 (2012-07-16 13:47:07)转载▼标签: winform treeview drawnode Treeview控 ...

  6. Winform 中DataGridView、dev Gridview控件添加行标题

    有很多种方法. 1.可以在DataGridView控件中的RowStateChanged事件改变行标题单元格的值(Row.HeaderCell.Value) /// <summary> / ...

  7. Winform DataGridView控件添加行号

      有很多种方法,这里介绍三种: A: 控件的RowStateChanged事件中添加,RowStateChanged事件是在行的状态更改(例如,失去或获得输入焦点)时发生的事件: e.Row.Hea ...

  8. 基于Winform框架DataGridView控件的SqlServer数据库查询展示功能的实现

    关键词:Winform.DataGridView.SqlServer 一个基于winform框架的C/S软件,主要实现对SqlServer数据库数据表的实时查询. 一.为DataGridView添加数 ...

  9. C#中DataGridView控件使用大全

    DataGridView 动态添加新行: DataGridView控件在实际应用中非常实用,特别需要表格显示数据时.可以静态绑定数据源,这样就自动为DataGridView控件添加相应的行.假如需要动 ...

随机推荐

  1. Oracle补习班第四天

    Everything has its time and that time must be watched. 万物皆有时,时来不可失 1,管理参数文件 参数文件分两种spfile二进制文件和pfile ...

  2. Visual Studio最好用的快捷键

    当然每个人常用的一般都会有些不一样,欢迎大家评论说出自己常用或最常用的快捷键吧,比比看谁用的巧~~~ ctrl+-(shift+ctrl+-):移动光标到上次位置或相反,比如定位一个函数,转到函数定义 ...

  3. sql-分组排序

    我们有一张数据表,需要按照[类别]分组按照[时间]排序,并分组显示各自的序号. 表Archive ArchiveId varchar(30) 文章编号非数字 CategoryId int 文章分类Id ...

  4. Emergency(山东省第一届ACM省赛)

    Emergency Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Kudo’s real name is not Kudo. H ...

  5. css 画基本图形

    抄于http://dongtianee.sinaapp.com/demo9.html /******************************************************** ...

  6. mac-文本编辑器

    windows时代最喜欢的文本编辑器一直是ultraedit,但到了mac下,破解的ultraedit退出时会异常,于是琢磨着换编辑器,最终选择了sublime text2,百度下载,不注册也可以用. ...

  7. FileUpload 上传文件,并实现c#使用Renci.SshNet.dll实现SFTP文件传输

    fileupload上传文件和jquery的uplodify控件使用方法类似,对服务器控件不是很熟悉,记录一下. 主要是记录新接触的sftp文件上传.服务器环境下使用freesshd搭建好环境后,wi ...

  8. window.frame

    定义和用法 frames 属性返回窗口中所有命名的框架. 该集合是 Window 对象的数组,每个 Window 对象在窗口中含有一个框架或 <iframe>.属性 frames.leng ...

  9. linux故障判断

    系统问题: 带宽 netstat cpu io 磁盘 内存     free ------------------------------------------------------------- ...

  10. Spark Streaming资源动态申请和动态控制消费速率剖析

    本期内容 : Spark Streaming资源动态分配 Spark Streaming动态控制消费速率 为什么需要动态处理 : Spark 属于粗粒度资源分配,也就是在默认情况下是先分配好资源然后再 ...