If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Populating DataGridView Combobox column

Hi,
I have an unbound DataGridView control that displays a few columns of account data. The first column is a combobox that I fill with data from a dataset of names filtered by what's in a customer field on the form. When you click on a combo on one of the populated rows, the available names come up in the combo. But if the first click in the datagridview is on a new row's combo, various (incorrect) names appear. Only after you click on one of the populated row's comboboxes, does the correct data appear in the new row's combobox control.

I don't really understand why this is happening. I have been using the EditingControlShowing event to populate and add the combo to the new row, but it doesn't seem to be working very well. I hope someone out there has some ideas about the correct way to do this.

Re: Populating DataGridView Combobox column

Sorry about that. The original rows are added when the record of the main dataset changes.

Code:

Private Sub doAcctLines()
Try
Me.BlotterAcctDataGridView.Rows.Clear() 'Clear old rows first
Dim dt As New DataTable
dt = oUtils.loadBlotterAccounts(Me.TicketNoTextBox.Text) 'Get accounts for this customer
If Not IsDBNull(dt) Then
If dt.Rows.Count > 0 Then
For i As Integer = 0 To dt.Rows.Count - 1 'Add columns and rows for this account record
Dim dgvRow As New DataGridViewRow
Dim dgvCell As DataGridViewCell
Dim dgvCbo As DataGridViewComboBoxCell
' Fill datagridview row with cells and data,
' then add row to datagridview
dgvCbo = New DataGridViewComboBoxCell
Me.AccountLkpBindingSource.Filter = "" 'Clear filter
Me.AccountLkpBindingSource.Filter = "CustomerName = '" & Me.CustomerIdComboBox.Text & "' AND Active = 1" 'Filter accounts. Only show accounts for this customer that are active.
For j As Integer = 0 To Me.AccountLkpBindingSource.Count - 1
dgvCbo.Items.Add(Me.AccountLkpBindingSource.Item(j)(1).ToString)
Next
If Not IsDBNull(dt.Rows(i)("AccountId")) Then
For j = 0 To Me.AccountLkpBindingSource.Count - 1
If dgvCbo.Items(j).ToString = dt.Rows(i)("AccountName").ToString Then
dgvCbo.Value = dgvCbo.Items(j).ToString
Exit For
End If
Next
Else
dgvCbo.Selected = -1
End If
dgvRow.Cells.Add(dgvCbo)
dgvCell = New DataGridViewTextBoxCell
If Not IsDBNull(dt.Rows(i)("Quantity")) Then
dgvCell.Value = dt.Rows(i)("Quantity")
Else
dgvCell.Value = ""
End If
dgvRow.Cells.Add(dgvCell)
dgvCell = New DataGridViewTextBoxCell
If Not IsDBNull(dt.Rows(i)("Commission")) Then
dgvCell.Value = dt.Rows(i)("Commission")
Else
dgvCell.Value = ""
End If
dgvRow.Cells.Add(dgvCell)
dgvCell = New DataGridViewTextBoxCell
If Not IsDBNull(dt.Rows(i)("SettleDate")) Then
dgvCell.Value = dt.Rows(i)("SettleDate")
Else
dgvCell.Value = ""
End If
dgvRow.Cells.Add(dgvCell)
dgvCell = New DataGridViewTextBoxCell
If Not IsDBNull(dt.Rows(i)("ClearingCharge")) Then
dgvCell.Value = dt.Rows(i)("ClearingCharge")
Else
dgvCell.Value = ""
End If
dgvRow.Cells.Add(dgvCell)
dgvCell = New DataGridViewTextBoxCell
If Not IsDBNull(dt.Rows(i)("SettleInfo")) Then
dgvCell.Value = dt.Rows(i)("SettleInfo")
Else
dgvCell.Value = ""
End If
dgvRow.Cells.Add(dgvCell)
dgvCell = New DataGridViewCheckBoxCell
If Not IsDBNull(dt.Rows(i)("Confirmed")) Then
dgvCell.Value = dt.Rows(i)("Confirmed")
Else
dgvCell.Value = 0
End If
dgvRow.Cells.Add(dgvCell)
dgvCell = New DataGridViewTextBoxCell
If Not IsDBNull(dt.Rows(i)("LineNum")) Then
dgvCell.Value = dt.Rows(i)("LineNum")
Else
dgvCell.Value = ""
End If
dgvRow.Cells.Add(dgvCell)
Me.BlotterAcctDataGridView.Rows.Add(dgvRow) 'Add completed row to DataGridView
Next
End If
End If
Catch ex As Exception
Throw ex
End Try
End Sub

This is the code that supposedly fills the combobox column in the new row.