【发布时间】:2016-10-10 11:27:43
【问题描述】:
我需要数据网格中的多列组合框,以显示我的数据库中的 2 个表值,在组合框中以行分隔。我已经使用带有 Enter 和 Draw_Item 事件的普通组合框完成了此操作,现在我也尝试在数据网格中执行此操作 - 在 Cell_Enter 和 Cell_Painting 事件中。当我尝试设置 DataViewrow 时出现问题 - 我收到错误“索引不是 DatagridView 的成员...”。这是我的代码:
Private Sub MyDGV_CellEnter(sender As Object, e As DataGridViewCellEventArgs) Handles MyDGV.CellEnter
If e.ColumnIndex = 0 Then
Dim SQL As String = "SELECT Field1,Field2 from MyTable"
Dim dtb As New DataTable()
dtb.Columns.Add("Field1", System.Type.GetType("System.String"))
dtb.Columns.Add("Field2", System.Type.GetType("System.String"))
Try
Myconn() 'My connection to Oracle
Using dad As New OracleDataAdapter(SQL, Myconn)
dad.Fill(dtb)
End Using
Column1.DisplayMember = "Field1"
Column1.DataSource = dtb
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
Myconn.Close()
End Try
End If
End Sub
Private Sub MyDGV_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles MyDGV.CellPainting
If e.ColumnIndex = 0 Then
Dim drv As DataRowView = CType(Column1.Items(e.Index), DataRowView) 'I have error here - this line should get value of each row
Dim id As String = drv("Field1").ToString()
Dim name As String = drv("Field2").ToString()
Dim r1 As Rectangle = e.CellBounds
r1.Width = r1.Width / 2
Using sb As SolidBrush = New SolidBrush(MyDGV.BackColor)
e.Graphics.DrawString(id, e.CellStyle.Font, sb, r1)
End Using
Using p As Pen = New Pen(Color.AliceBlue)
e.Graphics.DrawLine(p, r1.Right, 0, r1.Right, r1.Bottom)
End Using
Dim r2 As Rectangle = e.CellBounds
r2.X = e.CellBounds.Width / 2
r2.Width = r2.Width / 2
Using sb As SolidBrush = New SolidBrush(MyDGV.BackColor)
e.Graphics.DrawString(name, e.CellStyle.Font, sb, r2)
End Using
End If
End Sub
非常感谢任何帮助!
【问题讨论】:
标签: vb.net datagridview