2016-10-10 6 views
0

私のDBの2つのテーブル値をコンボボックスのラインで区切って表示するには、データグリッドにマルチカラムコンボボックスが必要です。私はすでにこれを通常のコンボボックスを使ってEnterとDraw_Itemイベントで行いましたが、今度はCell_EnterとCell_PaintingイベントでDataGridでこれをやろうとしています。 DataViewrowを設定しようとすると問題が発生します - 「インデックスはDatagridViewのメンバーではありません...」というエラーが発生します。ここに私のコードは次のとおりです。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 

すべてのヘルプははるかに高く評価します!

答えて

0

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() 
+0

は、私は実際にここに投稿する前にこれを試してみました。それは機能していません - "DataGridViewRow型の値をDataRowViewに変換できません"というエラー。 – LuckyLuke82

+0

これを反映するように更新しました。ありがとうございます。 – N0Alias

+0

@別名、今私はエラーが発生します: "負でなく、コレクションのサイズより小さくなければなりません"。私は、コンボボックスが生成される前にDatagridViewCellPaintingEventArgsが発生することを知っています。私はこれを変更することができますか...私はForm_LoadにCell_Enterコードを変更しようとしましたが、それでも同じです。 – LuckyLuke82