none
visual basic 2010 DataGridView中 莫行莫列的数据是"1234",其中的"3"要用红色表示,用什么属性。 RRS feed

  • 问题

  • visual basic 2010  DataGridView中 莫行莫列的数据是"1234",其中的"3"要用红色表示,用什么属性。

    要代码。

    谢谢。

    2011年2月21日 1:55

答案

  • 你好

    这是上面那个例子VB.NET的代码,你可以运行下,应该是你想要的效果。

    Imports System.Collections.Generic
    Imports System.ComponentModel
    Imports System.Data
    Imports System.Drawing
    Imports System.Linq
    Imports System.Text
    Imports System.Windows.Forms
    Imports System.Text.RegularExpressions
    
    Public Class Form1
     'DataSet 
     Private dataSet As New DataSet()
     Private dStable As New DataTable()
     Private dSCol1 As New DataColumn("name", GetType(String))
     Private dSCol2 As New DataColumn("value", GetType(String))
     Private rTemp As DataRow
     'DataGridView 
     Private dGVColumnCollection As DataGridViewColumnCollection
     Private dGVColumn1 As New DataGridViewColumn()
     Private dGVColumn2 As New DataGridViewColumn()
     
     Public Sub New()
    
     ' This call is required by the designer.
     InitializeComponent()
     ' Add any initialization after the InitializeComponent() call.
     'initialise dataset 
     dStable.TableName = "sample"
     dStable.Columns.Add(dSCol1)
     dStable.Columns.Add(dSCol2)
     rTemp = dStable.NewRow()
     rTemp.ItemArray = New Object() {"bill", " 000 0000 0000000 0000000 0000000000"}
     dStable.Rows.Add(rTemp)
     rTemp = dStable.NewRow()
     rTemp.ItemArray = New Object() {"ted", " 111 1111 1111111 1111111 1111111111"}
     dStable.Rows.Add(rTemp)
     rTemp = dStable.NewRow()
     rTemp.ItemArray = New Object() {"john", " 222 2222 2222222 2222222 2222222222"}
     dStable.Rows.Add(rTemp)
     dataSet.Tables.Add(dStable)
     'add coloumns to datagridView 
     dGVColumnCollection = DataGridView1.Columns
     dGVColumnCollection.Add("col1", "Name")
     dGVColumnCollection.Add("col2", "Value")
     dGVColumn1 = dGVColumnCollection(0)
     dGVColumn2 = dGVColumnCollection(1)
     'pupulate DataGridView 
     For Each row As DataRow In dataSet.Tables(0).Rows
      Dim dGVRow As New DataGridViewRow()
      Dim textCell As New DataGridViewTextBoxCell()
      Dim textCel2 As New MultiColourDataGridViewTextBoxCell()
      textCell.Value = row.ItemArray(0)
      textCel2.Value = row.ItemArray(1)
      dGVRow.Cells.Add(textCell)
      dGVRow.Cells.Add(textCel2)
      DataGridView1.Rows.Add(dGVRow)
     Next
    
     End Sub
    End Class
    Public Class MultiColourDataGridViewTextBoxCell
     Inherits DataGridViewTextBoxCell
     Public Sub New()
     MyBase.New()
     End Sub
     ''' <summary> 
     ''' overide paint method to colour text dependant on number of consecutive numbers 
     ''' </summary> 
     ''' <param name="graphics"></param>
     ''' <param name="clipBounds"></param>
     ''' <param name="cellBounds"></param> 
     ''' <param name="rowIndex"></param> 
     ''' <param name="cellState"></param> 
     ''' <param name="value"></param> 
     ''' <param name="formattedValue"></param> 
     ''' <param name="errorText"></param> 
     ''' <param name="cellStyle"></param>
     ''' <param name="advancedBorderStyle"></param> 
     ''' <param name="paintParts"></param> 
     Protected Overrides Sub Paint(ByVal graphics As Graphics, ByVal clipBounds As Rectangle, ByVal cellBounds As Rectangle, ByVal rowIndex As Integer, ByVal cellState As DataGridViewElementStates, ByVal value As Object, _
     ByVal formattedValue As Object, ByVal errorText As String, ByVal cellStyle As DataGridViewCellStyle, ByVal advancedBorderStyle As DataGridViewAdvancedBorderStyle, ByVal paintParts As DataGridViewPaintParts)
     Dim fontSize As Integer = 9
     Dim textBrush As Brush = Brushes.Green
     Dim font As New Font(FontFamily.GenericSansSerif, fontSize, FontStyle.Regular)
     Dim stringPrint As [String] = DirectCast(formattedValue, [String])
     graphics.FillRectangles(Brushes.White, New Rectangle() {cellBounds})
     Dim stringPrintInfo As StringPrintInfo() = GetConsectiveNumbers(4, stringPrint, Brushes.Black, Brushes.Red)
     PrintConsectiveNumbers(stringPrintInfo, graphics, font, textBrush, cellBounds, StringFormat.GenericDefault)
     Me.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle)
     End Sub
     ''' <summary> 
     ''' from input string finds consective numbers marjs them in specified colour. 
     ''' </summary> 
     ''' <param name="numberConsecutive">number of consecutive numbers to look for</param> 
     ''' <param name="input">input string</param> 
     ''' <param name="bOriginal">default brush colour</param> 
     ''' <param name="bConsecutive">brush for consecutive numbers</param> 
     ''' <returns> an ordered array of text and colour pairs</returns> 
     Public Function GetConsectiveNumbers(ByVal numberConsecutive As Integer, ByVal input As [String], ByVal bOriginal As Brush, ByVal bConsecutive As Brush) As StringPrintInfo()
     Dim formatInformation As New List(Of StringPrintInfo)()
     Dim inputCount As Integer = 0
     'how much of input string is used  
     Dim tmpStrPrntInfo As StringPrintInfo
     Dim regExp As [String] = "\b[0-9]{4}\b"
     Dim exp As New Regex(regExp)
     For Each match As Match In exp.Matches(input)
      If inputCount < match.Index Then
      tmpStrPrntInfo = New StringPrintInfo()
      Dim strTmp As [String] = input.Substring(0, match.Index)
      tmpStrPrntInfo.str = strTmp
      tmpStrPrntInfo.brush = bOriginal
      inputCount = match.Index
      formatInformation.Add(tmpStrPrntInfo)
      End If
      tmpStrPrntInfo = New StringPrintInfo()
      tmpStrPrntInfo.str = match.Value
      tmpStrPrntInfo.brush = bConsecutive
      inputCount = match.Index + match.Length
      formatInformation.Add(tmpStrPrntInfo)
     Next
     If inputCount < input.Length Then
      tmpStrPrntInfo = New StringPrintInfo()
      Dim strTmp As [String] = input.Substring(inputCount)
      tmpStrPrntInfo.str = strTmp
      tmpStrPrntInfo.brush = bOriginal
      inputCount = input.Length
      formatInformation.Add(tmpStrPrntInfo)
     End If
     Return formatInformation.ToArray()
     End Function
     ''' <summary> 
     ''' Prints the cell with multicolour text. should be called as part of paint method 
     ''' </summary> 
     ''' <param name="toPrint"></param> 
     ''' <param name="graphics"></param> 
     ''' <param name="font"></param> 
     ''' <param name="textBrush"></param> 
     ''' <param name="cellBounds"></param> 
     ''' <param name="stringFormat"></param> 
     Public Sub PrintConsectiveNumbers(ByVal toPrint As StringPrintInfo(), ByVal graphics As Graphics, ByVal font As Font, ByVal textBrush As Brush, ByVal cellBounds As Rectangle, ByVal stringFormat As StringFormat)
     Dim xOffset As Single = 0
     For i As Integer = 0 To toPrint.Length - 1
      If i > 0 Then
      Dim stringSize As SizeF = graphics.MeasureString(toPrint(i - 1).str, font)
      xOffset += stringSize.Width
      End If
      'dont print of edges of cell
    
      If cellBounds.X + xOffset + graphics.MeasureString(toPrint(i).str, font).Width > cellBounds.X + cellBounds.Width Then
      Dim cutDown As String = toPrint(i).str
    
      For j As Integer = cutDown.Length To 1 Step -1
       cutDown = cutDown.Substring(0, j)
       If cellBounds.X + xOffset + graphics.MeasureString(cutDown, font).Width <= cellBounds.X + cellBounds.Width Then
       graphics.DrawString(cutDown, font, toPrint(i).brush, cellBounds.X + xOffset, cellBounds.Y, stringFormat)
    
       Exit For
       End If
      Next
      Else
      graphics.DrawString(toPrint(i).str, font, toPrint(i).brush, cellBounds.X + xOffset, cellBounds.Y, stringFormat)
      End If
     Next
     End Sub
     ''' <summary> 
     ''' represent string to print and colour to print it
     ''' </summary> 
     Public Structure StringPrintInfo
     Public str As [String]
     Public brush As Brush
     End Structure
    End Class
    
    

    Best regards


    如果您对我们的论坛在线支持服务有任何的意见或建议,请通过邮件告诉我们。
    MSDN 论坛好帮手 立刻免费下载  MSDN 论坛好帮手

    2011年3月1日 8:41

全部回复

  • 难道都不会吗?
    2011年2月22日 1:28
  • 你好,

    你需要重写DataGridViewTextBoxCell里边的paint()方法.

    http://msdn.microsoft.com/zh-cn/library/system.windows.forms.datagridviewcell.paint.aspx (DataGridViewCell.Paint 方法)

    这里有个例子你可以参考下,希望对你有所帮助。

    http://www.codeproject.com/Messages/3322096/Re-Multi-color-text-in-single-cell-of-datagridview.aspx

    Best regards 


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年2月23日 8:13
  •  看不懂哦
    2011年2月25日 8:13
  • 你好

    这是上面那个例子VB.NET的代码,你可以运行下,应该是你想要的效果。

    Imports System.Collections.Generic
    Imports System.ComponentModel
    Imports System.Data
    Imports System.Drawing
    Imports System.Linq
    Imports System.Text
    Imports System.Windows.Forms
    Imports System.Text.RegularExpressions
    
    Public Class Form1
     'DataSet 
     Private dataSet As New DataSet()
     Private dStable As New DataTable()
     Private dSCol1 As New DataColumn("name", GetType(String))
     Private dSCol2 As New DataColumn("value", GetType(String))
     Private rTemp As DataRow
     'DataGridView 
     Private dGVColumnCollection As DataGridViewColumnCollection
     Private dGVColumn1 As New DataGridViewColumn()
     Private dGVColumn2 As New DataGridViewColumn()
     
     Public Sub New()
    
     ' This call is required by the designer.
     InitializeComponent()
     ' Add any initialization after the InitializeComponent() call.
     'initialise dataset 
     dStable.TableName = "sample"
     dStable.Columns.Add(dSCol1)
     dStable.Columns.Add(dSCol2)
     rTemp = dStable.NewRow()
     rTemp.ItemArray = New Object() {"bill", " 000 0000 0000000 0000000 0000000000"}
     dStable.Rows.Add(rTemp)
     rTemp = dStable.NewRow()
     rTemp.ItemArray = New Object() {"ted", " 111 1111 1111111 1111111 1111111111"}
     dStable.Rows.Add(rTemp)
     rTemp = dStable.NewRow()
     rTemp.ItemArray = New Object() {"john", " 222 2222 2222222 2222222 2222222222"}
     dStable.Rows.Add(rTemp)
     dataSet.Tables.Add(dStable)
     'add coloumns to datagridView 
     dGVColumnCollection = DataGridView1.Columns
     dGVColumnCollection.Add("col1", "Name")
     dGVColumnCollection.Add("col2", "Value")
     dGVColumn1 = dGVColumnCollection(0)
     dGVColumn2 = dGVColumnCollection(1)
     'pupulate DataGridView 
     For Each row As DataRow In dataSet.Tables(0).Rows
      Dim dGVRow As New DataGridViewRow()
      Dim textCell As New DataGridViewTextBoxCell()
      Dim textCel2 As New MultiColourDataGridViewTextBoxCell()
      textCell.Value = row.ItemArray(0)
      textCel2.Value = row.ItemArray(1)
      dGVRow.Cells.Add(textCell)
      dGVRow.Cells.Add(textCel2)
      DataGridView1.Rows.Add(dGVRow)
     Next
    
     End Sub
    End Class
    Public Class MultiColourDataGridViewTextBoxCell
     Inherits DataGridViewTextBoxCell
     Public Sub New()
     MyBase.New()
     End Sub
     ''' <summary> 
     ''' overide paint method to colour text dependant on number of consecutive numbers 
     ''' </summary> 
     ''' <param name="graphics"></param>
     ''' <param name="clipBounds"></param>
     ''' <param name="cellBounds"></param> 
     ''' <param name="rowIndex"></param> 
     ''' <param name="cellState"></param> 
     ''' <param name="value"></param> 
     ''' <param name="formattedValue"></param> 
     ''' <param name="errorText"></param> 
     ''' <param name="cellStyle"></param>
     ''' <param name="advancedBorderStyle"></param> 
     ''' <param name="paintParts"></param> 
     Protected Overrides Sub Paint(ByVal graphics As Graphics, ByVal clipBounds As Rectangle, ByVal cellBounds As Rectangle, ByVal rowIndex As Integer, ByVal cellState As DataGridViewElementStates, ByVal value As Object, _
     ByVal formattedValue As Object, ByVal errorText As String, ByVal cellStyle As DataGridViewCellStyle, ByVal advancedBorderStyle As DataGridViewAdvancedBorderStyle, ByVal paintParts As DataGridViewPaintParts)
     Dim fontSize As Integer = 9
     Dim textBrush As Brush = Brushes.Green
     Dim font As New Font(FontFamily.GenericSansSerif, fontSize, FontStyle.Regular)
     Dim stringPrint As [String] = DirectCast(formattedValue, [String])
     graphics.FillRectangles(Brushes.White, New Rectangle() {cellBounds})
     Dim stringPrintInfo As StringPrintInfo() = GetConsectiveNumbers(4, stringPrint, Brushes.Black, Brushes.Red)
     PrintConsectiveNumbers(stringPrintInfo, graphics, font, textBrush, cellBounds, StringFormat.GenericDefault)
     Me.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle)
     End Sub
     ''' <summary> 
     ''' from input string finds consective numbers marjs them in specified colour. 
     ''' </summary> 
     ''' <param name="numberConsecutive">number of consecutive numbers to look for</param> 
     ''' <param name="input">input string</param> 
     ''' <param name="bOriginal">default brush colour</param> 
     ''' <param name="bConsecutive">brush for consecutive numbers</param> 
     ''' <returns> an ordered array of text and colour pairs</returns> 
     Public Function GetConsectiveNumbers(ByVal numberConsecutive As Integer, ByVal input As [String], ByVal bOriginal As Brush, ByVal bConsecutive As Brush) As StringPrintInfo()
     Dim formatInformation As New List(Of StringPrintInfo)()
     Dim inputCount As Integer = 0
     'how much of input string is used  
     Dim tmpStrPrntInfo As StringPrintInfo
     Dim regExp As [String] = "\b[0-9]{4}\b"
     Dim exp As New Regex(regExp)
     For Each match As Match In exp.Matches(input)
      If inputCount < match.Index Then
      tmpStrPrntInfo = New StringPrintInfo()
      Dim strTmp As [String] = input.Substring(0, match.Index)
      tmpStrPrntInfo.str = strTmp
      tmpStrPrntInfo.brush = bOriginal
      inputCount = match.Index
      formatInformation.Add(tmpStrPrntInfo)
      End If
      tmpStrPrntInfo = New StringPrintInfo()
      tmpStrPrntInfo.str = match.Value
      tmpStrPrntInfo.brush = bConsecutive
      inputCount = match.Index + match.Length
      formatInformation.Add(tmpStrPrntInfo)
     Next
     If inputCount < input.Length Then
      tmpStrPrntInfo = New StringPrintInfo()
      Dim strTmp As [String] = input.Substring(inputCount)
      tmpStrPrntInfo.str = strTmp
      tmpStrPrntInfo.brush = bOriginal
      inputCount = input.Length
      formatInformation.Add(tmpStrPrntInfo)
     End If
     Return formatInformation.ToArray()
     End Function
     ''' <summary> 
     ''' Prints the cell with multicolour text. should be called as part of paint method 
     ''' </summary> 
     ''' <param name="toPrint"></param> 
     ''' <param name="graphics"></param> 
     ''' <param name="font"></param> 
     ''' <param name="textBrush"></param> 
     ''' <param name="cellBounds"></param> 
     ''' <param name="stringFormat"></param> 
     Public Sub PrintConsectiveNumbers(ByVal toPrint As StringPrintInfo(), ByVal graphics As Graphics, ByVal font As Font, ByVal textBrush As Brush, ByVal cellBounds As Rectangle, ByVal stringFormat As StringFormat)
     Dim xOffset As Single = 0
     For i As Integer = 0 To toPrint.Length - 1
      If i > 0 Then
      Dim stringSize As SizeF = graphics.MeasureString(toPrint(i - 1).str, font)
      xOffset += stringSize.Width
      End If
      'dont print of edges of cell
    
      If cellBounds.X + xOffset + graphics.MeasureString(toPrint(i).str, font).Width > cellBounds.X + cellBounds.Width Then
      Dim cutDown As String = toPrint(i).str
    
      For j As Integer = cutDown.Length To 1 Step -1
       cutDown = cutDown.Substring(0, j)
       If cellBounds.X + xOffset + graphics.MeasureString(cutDown, font).Width <= cellBounds.X + cellBounds.Width Then
       graphics.DrawString(cutDown, font, toPrint(i).brush, cellBounds.X + xOffset, cellBounds.Y, stringFormat)
    
       Exit For
       End If
      Next
      Else
      graphics.DrawString(toPrint(i).str, font, toPrint(i).brush, cellBounds.X + xOffset, cellBounds.Y, stringFormat)
      End If
     Next
     End Sub
     ''' <summary> 
     ''' represent string to print and colour to print it
     ''' </summary> 
     Public Structure StringPrintInfo
     Public str As [String]
     Public brush As Brush
     End Structure
    End Class
    
    

    Best regards


    如果您对我们的论坛在线支持服务有任何的意见或建议,请通过邮件告诉我们。
    MSDN 论坛好帮手 立刻免费下载  MSDN 论坛好帮手

    2011年3月1日 8:41