【问题标题】:Datagridview - multi column comboboxDatagridview - 多列组合框
【发布时间】: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


    【解决方案1】:

    查看DataGridViewCellPaintingEventArgs Class 的成员。我会尝试利用 RowIndex 属性:

    Dim drv As DataGridViewRow = CType(MyDGV.Rows(e.RowIndex), DataGridViewRow)
    

    那么你可能需要通过引用单元格属性来获取你需要的值:

    Dim id As String = drv.Cells("Field1").Value.ToString()
    Dim name As String = drv.Cells("Field2").Value.ToString()
    

    【讨论】:

    • 在发帖之前我实际上已经尝试过了。它不起作用 - “DataGridViewRow 类型的值无法转换为 DataRowView”错误。
    • @No alias,现在我得到错误:“必须是非负数并且小于集合的大小”。我在填充组合框之前发生了 DatagridViewCellPaintingEventArgs。我可以更改吗?...我尝试将 Cell_Enter 代码更改为 Form_Load,但还是一样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-08
    • 2017-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多