2012-12-23 36 views
3

当鼠标悬停在行上时,无论选择哪一行,我都希望在datagridview行上获得下划线字体。在鼠标悬停时更改datagridview上的行字体vb.net

我得到的 - 半:)

Private Sub aDgv_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles aDgv.MouseMove 

    Dim hit As DataGridView.HitTestInfo = aDgv.HitTest(e.X, e.Y) 
    If hit.Type = DataGridViewHitTestType.Cell Then 
     aDgv.Rows(hit.RowIndex).DefaultCellStyle.Font = New Font(aDgv.DefaultCellStyle.Font, FontStyle.Underline) 
    End If 
End Sub 

所以,当我过来该行行文本变成下划线(如预期),当我移动到下一行则那些下一行变成下划线,但以前不回正常的字体。

该怎么办,只有鼠标悬停行上的文本才会被加下划线。
如何将鼠标移动到其他行时将字体重置为正常?

回答

3

要回正常Font只使用CellMouseLeave事件

Private Sub DataGridView1_MouseMove(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseMove 
    Dim normalFont = New Font(DataGridView1.DefaultCellStyle.Font, FontStyle.Regular) 
    Dim hit As DataGridView.HitTestInfo = DataGridView1.HitTest(e.X, e.Y) 
    If hit.Type = DataGridViewHitTestType.Cell Then 
     If DataGridView1.Rows(hit.RowIndex).Cells(hit.ColumnIndex).FormattedValue.ToString().Trim().Length > 0 Then 
      DataGridView1.Rows(hit.RowIndex).DefaultCellStyle.Font = New Font(DataGridView1.DefaultCellStyle.Font, FontStyle.Underline) 
     Else 
      DataGridView1.Rows(hit.RowIndex).DefaultCellStyle.Font = normalFont 
     End If 
    End If 
End Sub 

Private Sub DataGridView1_CellMouseLeave(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellMouseLeave 
    Dim normalFont = New Font(DataGridView1.DefaultCellStyle.Font, FontStyle.Regular) 
    If (e.ColumnIndex > -1) Then 
     If e.RowIndex > -1 Then 
      If DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).FormattedValue.ToString().Trim().Length > 0 Then 
       DataGridView1.Rows(e.RowIndex).DefaultCellStyle.Font = normalFont 
      Else 
       DataGridView1.Rows(e.RowIndex).DefaultCellStyle.Font = normalFont 
      End If 
     End If 
    End If 
End Sub 
+0

这个工作,非常感谢你!唯一的问题是,如果我通过行左右下划线闪烁(其中没有文字或什么?)。你有想法如何避免这种情况? –

+0

我修改了我的答案:) – spajce

+0

对不起,但我不能得到它应该的工作。必须将String.IsNullOrWhiteSpace更改为IsNullOrEmpty,因为我的IDE说IsNullOrWhiteSpace不是字符串的成员,所以我选择最接近的一个:IsNullOrEmpty。现在比以前更闪烁。该怎么办? –

相关问题