如图。给单元格的添加下划线。
主要思路是在cellformating这个事件中设定。代码如下:
View Code
1
private
void
dataGridView1_CellFormatting(
object
sender, DataGridViewCellFormattingEventArgs e)
2 {
3 if (isFirstCellFormatting)
4 {
5 int r = 0 ;
6 if (e.RowIndex < 0 )
7 {
8 r = 0 ;
9 }
10 else
11 {
12 r = e.RowIndex;
13 }
14 dataGridView1.Rows[r].Cells[e.ColumnIndex].Style.Font = new Font( " 宋体 " , 12 , FontStyle.Underline);
15 dataGridView1.Rows[r].Cells[e.ColumnIndex].Style.ForeColor = Color.Blue;
16 dataGridView1.Rows[r].Cells[e.ColumnIndex].ToolTipText = string .Format( " 我是第{0}行,第{1}列的单元格 " , r, e.ColumnIndex);
17
18 }
19 }
2 {
3 if (isFirstCellFormatting)
4 {
5 int r = 0 ;
6 if (e.RowIndex < 0 )
7 {
8 r = 0 ;
9 }
10 else
11 {
12 r = e.RowIndex;
13 }
14 dataGridView1.Rows[r].Cells[e.ColumnIndex].Style.Font = new Font( " 宋体 " , 12 , FontStyle.Underline);
15 dataGridView1.Rows[r].Cells[e.ColumnIndex].Style.ForeColor = Color.Blue;
16 dataGridView1.Rows[r].Cells[e.ColumnIndex].ToolTipText = string .Format( " 我是第{0}行,第{1}列的单元格 " , r, e.ColumnIndex);
17
18 }
19 }
同时,也可以手动设置字体的样式。
如我单击”更改下划线“按钮,下划线将删除
代码如下
View Code
1
//
设置没有字体的下划线
2 private Font FontNoUnderLine { get ; set ; }
3 FontNoUnderLine = new System.Drawing.Font( " 宋体 " , 12 );
4
5 dataGridView1[ 3 , 3 ].Style.Font = FontNoUnderLine;
6 dataGridView1[ 3 , 3 ].Style.ForeColor = Color.Red;
2 private Font FontNoUnderLine { get ; set ; }
3 FontNoUnderLine = new System.Drawing.Font( " 宋体 " , 12 );
4
5 dataGridView1[ 3 , 3 ].Style.Font = FontNoUnderLine;
6 dataGridView1[ 3 , 3 ].Style.ForeColor = Color.Red;