2011-10-07 124 views
3

我在填充GridView中的下拉列表時遇到困難。 我創建了列與下面的代碼:需要在Gridview中填充下拉組合框(未綁定列)

If Not Me.GridViewIsConstructed Then 
      gv.Columns.Add(createComboBoxWithDDL(Me.ddlGPField.Items, "Bank_GP_Field_Name", "GPField")) 
      gv.Columns.Add(createComboBoxWithDDL(Me.ddlBankField.Items, "Bank_Bank_Field_Name", "BankField")) 
    End IF 


    Private Function createComboBoxWithDDL(ByVal obj As Object, ByVal nDataFieldName As String, ByVal nColName As String) As DataGridViewComboBoxColumn 
     Dim combo As New DataGridViewComboBoxColumn 
     combo.DataSource = obj 
     combo.DataPropertyName = nDataFieldName 
     combo.Name = nColName 
     Return combo 
    End Function 

的問題是,我不能讓格式化手柄來填充組合框,我需要的索引。這裏是我的代碼嘗試BankField DropDown List。

If e.ColumnIndex = gv.Columns("BankField").Index Then 
    e.FormattingApplied = True 
    Dim _row = gv.Rows(e.RowIndex) 
    Dim _cell As New DataGridViewComboBoxColumn 
    fillGPFieldList(_cell) 
    _cell.DisplayIndex = 1 
    _cell.DisplayMember = "Credit" 
    _cell.ValueMember = "Credit" 
    _cell.DataSource = _cell.Items 
    e.Value = _cell 

End If 
If e.ColumnIndex = gv.Columns("TrxType").Index Then 
    e.FormattingApplied = True 
    e.Value = "BAL" 
End If 

Gridview顯示下拉對象就好,它總是設置爲索引-1。

請幫

V * * ** * ** * *** 編編輯 ** * ** * **** * V

不幸的是,沒有人回答我的問題。所以我解決了整個問題。它的醜陋,我會非常感謝任何反饋。

我從未能夠將ComboBox綁定到數據源。我嘗試了一切,直到我變成藍色。所以我去了基礎知識,並編寫了所有自動化的東西。我很好奇爲什麼自動綁定不起作用。也許是因爲我的gridview數據源是LINQ。

這是我如何將它拉下來。我希望有人從我的delima最後48小時中獲益:

首先,我知道我的窗體上有兩個下拉列表,一個是GPField,另一個是BankField。這些已經是已填充的DDL,它們是靜態的。所以我用它們來欺騙值而不是使用枚舉。

,我不認爲它很重要,但這裏是我如何填寫GPField和BankField:

 Sub fillGPFieldListDDL(ByVal obj As Object) 
      Dim db As New CompanyDataDataContext 
      Dim myConn As New Connection With {.ConnCls = ConnCls} 
      myConn.dbConnect(db) 
      'Setup the GP Field list 
      obj.Items.Clear() 
      For Each fld In db.getGPFieldList(Me.ddlImportID.SelectedItem, ddlImportTypes.BNKREC_IMPORTS_WORK) 
       obj.Items.Add(fld.Trim) 
      Next 
      db.Connection.Close() 
      db.Connection.Dispose() 
      db.Dispose() 
     End Sub 
     Sub fillBankFieldListDDL(ByVal obj As Object) 
      If String.IsNullOrEmpty(ddlImportID.Text) Then 
       Return 
      End If 
      Dim db As New CompanyDataDataContext 
      Dim myConn As New Connection With {.ConnCls = ConnCls} 
      myConn.dbConnect(db) 
      'Setup the Bank Field list 
      obj.Items.Clear() 
      For Each fld In db.getImportIDVirtualFields(Me.ddlImportID.Text, ddlImportTypes.BNKREC_IMPORTS_WORK) 
       obj.Items.Add(fld.Trim) 
      Next 
      db.Connection.Close() 
      db.Connection.Dispose() 
      db.Dispose() 
     End Sub 

接下來,根據來自用戶的選擇,我填充我的網具有以下功能:

Function fillToleranceFieldsGridView(ByVal nTrxType As String) As Integer 
    Dim myConn As New Connection With {.ConnCls = ConnCls} 
    Try 
     Using db As New CompanyDataDataContext 
      myConn.dbConnect(db) 
      'Dim _query As IEnumerable(Of TWO_Tolerance_Field) = (From tf In db.getToleranceSetupRecordFields(nTrxType, BankToleranceRecordTypes.MasterRecord) _ 
      '    Select tf) 
      'Dim _query2 As IEnumerable(Of TWO_Tolerance_Field) = db.getToleranceSetupRecordFields(nTrxType, BankToleranceRecordTypes.MasterRecord) 
      Dim _query3 = (From t In db.TWO_Tolerance_Fields _ 
          Where t.TRXTYPESTRING = nTrxType And t.RCRDTYPE = BankToleranceRecordTypes.MasterRecord _ 
          Select t.Bank_Bank_Field_Number, t.Bank_GP_Field_Name, t.TRXTYPESTRING) 

      Dim gv = Me.DataGridViewX1 
      gv.AutoGenerateColumns = False 
      gv.AllowUserToAddRows = False 
      gv.AllowUserToDeleteRows = False 
      gv.AllowUserToResizeRows = False 
      gv.AutoSize = True 
      gv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells 

      'gv.DataSource = _query3 

      If Not Me.GridViewIsConstructed Then 

       'Add in the combo box for GPField Names 
       Dim _comboCol As DataGridViewComboBoxColumn 
       _comboCol = CreateComboBoxColumn(Me.ddlGPField.Items, _ 
               ColumnNameData.Bank_GP_Field_Name.ToString, _ 
               FieldNames.GPField.ToString) 

       gv.Columns.Add(_comboCol) 

       _comboCol = CreateComboBoxColumn(Me.ddlBankField.Items, _ 
                ColumnNameData.Bank_Bank_Field_Number.ToString, _ 
                FieldNames.BankField.ToString) 

       gv.Columns.Add(_comboCol) 

       Dim col As DataGridViewColumn = _ 
        New DataGridViewTextBoxColumn() 

       Dim _cell = New DataGridViewTextBoxCell 
       Dim _coll = New DataGridViewColumn(_cell) 
       Dim _colIndex As Integer = 0 

       ''Bind to an existing column Left in for easy access for a simple text box 
       '_coll = New DataGridViewColumn(_cell) 
       '_coll.Name = ColumnNameData.Bank_GP_Field_Name.ToString 
       '_coll.ReadOnly = True 
       '_coll.HeaderText = ColumnNameData.Bank_GP_Field_Name.ToString 
       '_colIndex = gv.Columns.Add(_coll) 
       'gv.Columns(_colIndex).DataPropertyName = ColumnNameData.Bank_GP_Field_Name.ToString 

       Me.GridViewIsConstructed = True 
      End If 

      gv.Rows.Clear() 
      Dim ri As Integer = 0 
      For Each r In _query3 
       Dim _row As New DataGridViewRow 
       _row.CreateCells(gv) 
       _row.Cells(FieldNames.GPField).Value = r.Bank_GP_Field_Name.ToString.Trim 
       _row.Cells(FieldNames.BankField).Value = ddlBankField.Items(r.Bank_Bank_Field_Number - 1).ToString.Trim 
       gv.Rows.Add(_row) 
       ri += 1 
      Next 
      db.Connection.Close() 
      db.Connection.Dispose() 
     End Using 
     Return 0 
    Catch ex As Exception 
     Throw ex 
    Finally 
     myConn.Dispose() 
    End Try 
End Function 

所以,未解答的問題是: 1)。我無法使用_query或_query2作爲gridview的數據源,但_query3確實適用於簡單的文本框。 2)。如果使用_query3作爲gv.datasource,當執行gv.Columns.Add(_comboCol)時,爲什麼我的組合框會拋出「bank_gp_field_name」錯誤 3)。我明白我無法對_query3執行gv綁定的原因,因爲Bank_Bank_field_number中的數據是整數,並且DDL值在整數和字符串值之間沒有翻譯。但是我注意到該領域希望GPField能夠在標準綁定上運行。我仍然得到了「字段名爲」Bank_GP_Field_Name「不存在於gv.Columns.Add(_comboCol)

因此,總結,爲什麼下面的代碼不工作,而上面的代碼呢?

Function fillToleranceFieldsGridView(ByVal nTrxType As String) As Integer 
    Dim myConn As New Connection With {.ConnCls = ConnCls} 
    Try 
     Using db As New CompanyDataDataContext 
      myConn.dbConnect(db) 
      'Dim _query As IEnumerable(Of TWO_Tolerance_Field) = (From tf In db.getToleranceSetupRecordFields(nTrxType, BankToleranceRecordTypes.MasterRecord) _ 
      '    Select tf) 
      'Dim _query2 As IEnumerable(Of TWO_Tolerance_Field) = db.getToleranceSetupRecordFields(nTrxType, BankToleranceRecordTypes.MasterRecord) 
      Dim _query3 = (From t In db.TWO_Tolerance_Fields _ 
          Where t.TRXTYPESTRING = nTrxType And t.RCRDTYPE = BankToleranceRecordTypes.MasterRecord _ 
          Select t.Bank_Bank_Field_Number, t.Bank_GP_Field_Name, t.TRXTYPESTRING) 

      Dim gv = Me.DataGridViewX1 
      gv.AutoGenerateColumns = False 
      gv.AllowUserToAddRows = False 
      gv.AllowUserToDeleteRows = False 
      gv.AllowUserToResizeRows = False 
      gv.AutoSize = True 
      gv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells 

      gv.DataSource = _query3 

      If Not Me.GridViewIsConstructed Then 

       'Add in the combo box for GPField Names 
       Dim _comboCol As DataGridViewComboBoxColumn 
       _comboCol = CreateComboBoxColumn(Me.ddlGPField.Items, _ 
               ColumnNameData.Bank_GP_Field_Name.ToString, _ 
               FieldNames.GPField.ToString) 

       gv.Columns.Add(_comboCol) 

       '_comboCol = CreateComboBoxColumn(Me.ddlBankField.Items, _ 
       '         ColumnNameData.Bank_Bank_Field_Number.ToString, _ 
       '         FieldNames.BankField.ToString) 

       'gv.Columns.Add(_comboCol) 

       Dim col As DataGridViewColumn = _ 
        New DataGridViewTextBoxColumn() 

       Dim _cell = New DataGridViewTextBoxCell 
       Dim _coll = New DataGridViewColumn(_cell) 
       Dim _colIndex As Integer = 0 

       ''Bind to an existing column Left in for easy access for a simple text box 
       '_coll = New DataGridViewColumn(_cell) 
       '_coll.Name = ColumnNameData.Bank_GP_Field_Name.ToString 
       '_coll.ReadOnly = True 
       '_coll.HeaderText = ColumnNameData.Bank_GP_Field_Name.ToString 
       '_colIndex = gv.Columns.Add(_coll) 
       'gv.Columns(_colIndex).DataPropertyName = ColumnNameData.Bank_GP_Field_Name.ToString 

       Me.GridViewIsConstructed = True 
      End If 

      'gv.Rows.Clear() 
      'Dim ri As Integer = 0 
      'For Each r In _query3 
      ' Dim _row As New DataGridViewRow 
      ' _row.CreateCells(gv) 
      ' _row.Cells(FieldNames.GPField).Value = r.Bank_GP_Field_Name.ToString.Trim 
      ' _row.Cells(FieldNames.BankField).Value = ddlBankField.Items(r.Bank_Bank_Field_Number - 1).ToString.Trim 
      ' gv.Rows.Add(_row) 
      ' ri += 1 
      'Next 
      db.Connection.Close() 
      db.Connection.Dispose() 
     End Using 
     Return 0 
    Catch ex As Exception 
     Throw ex 
    Finally 
     myConn.Dispose() 
    End Try 
End Function 

Private Function CreateComboBoxColumn(ByVal obj As ComboBox.ObjectCollection, ByVal nDataFieldName As String, ByVal nColName As String) _ 
As DataGridViewComboBoxColumn 

    Dim column As New DataGridViewComboBoxColumn() 
    With (column) 
     .HeaderText = nColName 
     .DropDownWidth = 160 
     .Width = 90 
     .MaxDropDownItems = 3 
     .FlatStyle = FlatStyle.Flat 
     .DataSource = obj 
     .DataPropertyName = nDataFieldName 
     .Name = nColName 
     .ValueMember = nDataFieldName 
     .DisplayMember = .ValueMember 
    End With 
    Return column 
End Function 

回答

0

很抱歉,如果我誤解或過度簡化了這個,但是從我收集的COMBOX是有約束力的罰款,你只是不能設置索引爲1?這是因爲該單元沒有底層數據?

您是否嘗試過設置列的默認值?從我讀到的DataGridViewComboBoxColumn沒有一個單元DefaultValue,但與列默認工作。

Columns("ColumName").DefaultValue = 1

http://msdn.microsoft.com/en-us/library/system.data.datacolumn.defaultvalue.aspx

您也可以嘗試處理DefaultValuesNeeded

Private Sub gv_DefaultValuesNeeded(ByVal sender As Object, _ 
    ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) _ 
    Handles gv.DefaultValuesNeeded 

    With e.Row 
     .Cells("GPField") = 1 
     .Cells("BankField") = 1 
    End With 

End Sub 

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.defaultvaluesneeded(v=vs.110).aspx