在DataGridView中添加行号
生活随笔
收集整理的這篇文章主要介紹了
在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);
?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);
????????}
今天用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事件中加入下面的代碼:
?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);
????????}
轉載于:https://www.cnblogs.com/myjece/archive/2008/08/21/1272717.html
總結
以上是生活随笔為你收集整理的在DataGridView中添加行号的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 企消互动广告:网络时代广告活动的创新形式
- 下一篇: 文档知多少---走出软件作坊:三五个人十