2017-08-06 63 views
0

我是VB.net的新手,我正在开发一个提供服务的项目。我目前使用连接到数据集,数据源,表格适配器的标准数据网格视图来显示收到的未决项目。不过,我想知道,如果有人可以帮助我有相同的待批项目显示像下面的第二图像:如何使用datagridview创建自定义显示

enter image description here

+1

你有一个“东西”多个数据项:一个或两个日期,一个名字,一个问题/问题,状态和地址等。那真的不是DGV的意思。在滚动面板上的用户控件可能工作 – Plutonix

+0

您好Putonix,感谢您的快速响应。调用什么方法来执行此操作? – Facekianda

+2

您可能会考虑使用VB PowerPacks库中的'DataRepeater'控件。它基本上是Plutonix描述的,但是却为你做了很多重要的工作。 https://docs.microsoft.com/en-us/dotnet/visual-basic/developing-apps/windows-forms/power-packs-controls – jmcilhinney

回答

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