-
March 26th, 2009, 08:27 PM
#1
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.
Thanks,
Ron
-
March 26th, 2009, 08:53 PM
#2
Re: Populating DataGridView Combobox column
Welcome to the forums!
We can't really do much to help, if we can't see what you've tried. Post the relevent section of code that you use to load the data, and also the click event
Code:
' Use Code Tags [] CODE |[/]
' so it lines up
-
March 27th, 2009, 07:05 AM
#3
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.
Code:
Private Sub BlotterAcctDataGridView_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles BlotterAcctDataGridView.EditingControlShowing
Try
Dim comboBoxColumn As DataGridViewComboBoxColumn = BlotterAcctDataGridView.Columns(0)
If (BlotterAcctDataGridView.CurrentCellAddress.X = comboBoxColumn.DisplayIndex) Then
Dim cb As ComboBox = e.Control
If (cb IsNot Nothing) Then
cb.DropDownStyle = ComboBoxStyle.DropDownList
End If
End If
' Fill datagridview row with cells and data,
' then add row to datagridview
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.
comboBoxColumn.Items.Clear()
For j As Integer = 0 To Me.AccountLkpBindingSource.Count - 1
comboBoxColumn.Items.Add(Me.AccountLkpBindingSource.Item(j)(1).ToString)
Next
Catch ex As Exception
End Try
End Sub
Thanks again.
Last edited by rcarran; March 27th, 2009 at 07:11 AM.
Reason: add more
-
March 29th, 2009, 11:56 AM
#4
Re: Populating DataGridView Combobox column
Check the row to see if it's valid, otherwise get out of the sub, or show a msg
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|