0
我是VB.net的新手,我正在開發一個提供服務的項目。我目前使用連接到數據集,數據源,表格適配器的標準數據網格視圖來顯示收到的未決項目。不過,我想知道,如果有人可以幫助我有相同的待批項目顯示像下面的第二圖像:如何使用datagridview創建自定義顯示
我是VB.net的新手,我正在開發一個提供服務的項目。我目前使用連接到數據集,數據源,表格適配器的標準數據網格視圖來顯示收到的未決項目。不過,我想知道,如果有人可以幫助我有相同的待批項目顯示像下面的第二圖像:如何使用datagridview創建自定義顯示
使用VirtualMode
和DataGridViewImageColumn
。畫出你想要的形象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
你有一個「東西」多個數據項:一個或兩個日期,一個名字,一個問題/問題,狀態和地址等。那真的不是DGV的意思。在滾動面板上的用戶控件可能工作 – Plutonix
您好Putonix,感謝您的快速響應。調用什麼方法來執行此操作? – Facekianda
您可能會考慮使用VB PowerPacks庫中的'DataRepeater'控件。它基本上是Plutonix描述的,但是卻爲你做了很多重要的工作。 https://docs.microsoft.com/en-us/dotnet/visual-basic/developing-apps/windows-forms/power-packs-controls – jmcilhinney