如何使用datagridview创建自定义显示

时间:2017-08-06 21:26:11

标签: vb.net datagridview

我是VB.net的新手,我正在开发一个提供服务的项目。我目前正在使用连接到数据集,数据源,表适配器的标准数据网格视图来显示收到的待处理项目。但是,我想知道是否有人可以帮助我显示相同的待处理项目,如下面的第二张图片:

enter image description here

1 个答案:

答案 0 :(得分:0)

使用VirtualModeDataGridViewImageColumn。在CellValueNeeded

中绘制所需的图像
Public Class Form1
  'Add a DataGridView called DataGridView1 to the form
  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim dtb As New DataTable()
    dtb.Columns.Add("Col1", GetType(String))
    dtb.Columns.Add("Col2", GetType(Integer))
    dtb.Rows.Add("AA", 123)
    dtb.Rows.Add("CC", 234)
    DataGridView1.VirtualMode = True
    DataGridView1.AutoGenerateColumns = False
    DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
    DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
    DataGridView1.Columns.Add(New DataGridViewImageColumn() With {.Name = "img"})
    DataGridView1.DataSource = dtb
  End Sub

  Private Sub DataGridView1_CellValueNeeded(sender As Object, e As DataGridViewCellValueEventArgs) Handles DataGridView1.CellValueNeeded
    If DataGridView1.Columns(e.ColumnIndex).Name = "img" AndAlso e.ColumnIndex >= 0 AndAlso e.ColumnIndex < DataGridView1.ColumnCount Then
      Select Case e.RowIndex
        Case 0 To DataGridView1.RowCount - 2
          Dim drw As DataRow = DirectCast(DataGridView1.Rows(e.RowIndex).DataBoundItem, DataRowView).Row
          Dim strCol1Value As String = CStr(drw("Col1"))
          Dim intCol2Value As Integer = CInt(drw("Col2"))
          Dim bmp As New Bitmap(100, 50)
          Using gfx As Graphics = Graphics.FromImage(bmp)
            Dim brs As Brush = New System.Drawing.Drawing2D.LinearGradientBrush(New Point(0, 0), New Point(100, 50), Color.Black, Color.LightGray)
            gfx.FillRectangle(brs, New RectangleF(0, 0, bmp.Width, bmp.Height))
            gfx.DrawString(strCol1Value, New Font("Arial", 12), Brushes.Red, New PointF(0, 0))
            gfx.DrawString(intCol2Value.ToString, New Font("Arial", 12), Brushes.Yellow, New PointF(0, 25))
          End Using
          e.Value = bmp
        Case DataGridView1.RowCount - 1
          Dim bmp As New Bitmap(100, 50)
          Using gfx As Graphics = Graphics.FromImage(bmp)
            gfx.DrawString("NEW", New Font("Arial", 12), Brushes.Red, New PointF(0, 0))
          End Using
          e.Value = bmp
        Case Else
      End Select
    End If
  End Sub       
End Class