日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

在DataGridView中添加行号

發(fā)布時(shí)間:2025/7/14 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在DataGridView中添加行号 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
轉(zhuǎn)載:http://blog.csdn.net/webwx/archive/2007/03/21/1536090.aspx
今天用C#2.0開(kāi)發(fā)一個(gè)WinForm項(xiàng)目時(shí),碰到一個(gè)在DataGridView中加行號(hào)的問(wèn)題,找了一些資料,終于搞定?,F(xiàn)把它貼出來(lái)供大家參考。
  參考:http://community.csdn.net/Expert/topic/4671/4671416.xml?temp=.1845667
??????? 這里提到了兩種方法:
??????? 一、在數(shù)據(jù)加載后,用下面的代碼:
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);

這樣當(dāng)窗體第一次加載顯示時(shí),行號(hào)不會(huì)出現(xiàn),于是試了第二種方法,加上自己改了一點(diǎn)。
  二、在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????????}

網(wǎng)上還有:

?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);
????????}

轉(zhuǎn)載于:https://www.cnblogs.com/myjece/archive/2008/08/21/1272717.html

總結(jié)

以上是生活随笔為你收集整理的在DataGridView中添加行号的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。