Windows Develop Bookmark and Share   
 index > Windows Forms General > Cascade ComboBox in DatagridView
 

Cascade ComboBox in DatagridView

i read the thread about Cascade ComboBox in DatagridView (VB.NET) in here.
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/e7746ddf-3294-478c-a072-8a2eb83ba20c

I Configure the code in VB.net according to My Project...
I Give You Exactly My Code As it is..
DATAGRIDVIE = Flights_PrsonnelDatagridView
Two ComboBoxes (First In Column2 and Second In Column 3)
The Code Works Properly and i have a button Save that update the Flights_PersonnelTableAdapter..
The Data Saved Succesfully...
The Problem is that After Update Some Values Of the Filtered COmbobox dissapears although they were Stored in my SQL Table..
When I close the form and open it again the data of the filtered combobox are dislplayed Correctly..What is going on?




 
Private Sub Flights_PersonnelDataGridView_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Flights_PersonnelDataGridView.CellValueChanged
        If e.ColumnIndex = FLPComboBoxColumn.Index Then
            ' clear the cell value of the territory column in the same row
            If Me.Flights_PersonnelDataGridView.DataSource.[GetType]() Is GetType(DataTable) Then
                Me.Flights_PersonnelDataGridView.Rows(e.RowIndex).Cells(CrewComboBoxColumn.Index).Value = DBNull.Value
                '   Else
                '      Me.Flights_PersonnelDataGridView.Rows(e.RowIndex).Cells(3).Value = Nothing
            End If
        End If
    End Sub

 Private Sub Flights_PersonnelDataGridView_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles Flights_PersonnelDataGridView.EditingControlShowing
        If Me.Flights_PersonnelDataGridView.CurrentCell.ColumnIndex = CrewComboBoxColumn.Index Then
            Dim control As DataGridViewComboBoxEditingControl = TryCast(e.Control, DataGridViewComboBoxEditingControl)
            Dim bs As BindingSource = TryCast(control.DataSource, BindingSource)
            If bs IsNot Nothing Then
                ' set the filteredChildBS as the DataSource of the editing control
                TryCast(e.Control, ComboBox).DataSource = SelectAllPersonnel4CrewBindingSource

                Dim Positionnvalue As Object = Me.Flights_PersonnelDataGridView.Rows(Me.Flights_PersonnelDataGridView.CurrentCell.RowIndex).Cells(FLPComboBoxColumn.Index).Value
                If IsDBNull(Positionnvalue) OrElse Positionnvalue Is Nothing Then
                    SelectAllPersonnel4CrewBindingSource.Filter = "Key_positions=-1"
                Else
                    SelectAllPersonnel4CrewBindingSource.Filter = "Key_positions=" & Positionnvalue.ToString()
                End If
                ' set the SelectedValue property of the editing control
                If Not IsDBNull(Me.Flights_PersonnelDataGridView.CurrentCell.Value) AndAlso Me.Flights_PersonnelDataGridView.CurrentCell.Value IsNot Nothing Then
                    control.SelectedValue = Me.Flights_PersonnelDataGridView.CurrentCell.Value
                End If
            End If
        End If
    End Sub

  Private Sub Flights_PersonnelDataGridView_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles Flights_PersonnelDataGridView.DataError
        If e.ColumnIndex <> CrewComboBoxColumn.Index Then
            MessageBox.Show("Data error occurs:" & e.Exception.Message)
        End If
    End Sub

  • Edited byToutou Monday, August 31, 2009 12:30 PM
  • Edited byToutou Monday, August 31, 2009 12:17 PM
  •  
Toutou  Monday, August 31, 2009 11:58 AM

Hi,

If IsDBNull(Positionnvalue) OrElse Positionnvalue Is Nothing Then

SelectAllPersonnel4CrewBindingSource.Filter = "Key_positions=-1"

Else

SelectAllPersonnel4CrewBindingSource.Filter = "Key_positions=" & Positionnvalue.ToString()

End If

I saw you use bindingsource.Filter. I guess the value may hide by bindingsource.fiter property. Try to use bindingsource.RemoveFilter() method to check whether the value are all in the combobox dropdownlist. You will also need to change the bindingsource.filter to show the other “disappeared�items.

More information:

BindingSource.Filter Property

http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.filter.aspx

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
  • Marked As Answer byToutou Monday, September 07, 2009 5:26 PM
  •  
Ling Wang  Monday, September 07, 2009 8:29 AM
Some notes for my Code

SelectAllPersonnel4CrewBindingSource
is the Bindingsource of the Second (filtered) Combobox

FLPComboBoxColumn
is the name of the first ComboBox wich is bounded to a dataset and has DisplayMember =Position and ValueMember=Key_positions

The Second ComboBoxName is CrewComboBoxColumn
Toutou  Monday, August 31, 2009 12:17 PM
I find a solution that seems to solve the problem

in my update function

i put an Flights_personnelDatagridview.EndEdit
Toutou  Monday, August 31, 2009 1:12 PM
for a while i thought that i found a solution but doesn't work finally...could somebody give me a guideline?
Toutou  Monday, August 31, 2009 6:19 PM
somebody?
Toutou  Monday, August 31, 2009 8:35 PM

Hi,

If IsDBNull(Positionnvalue) OrElse Positionnvalue Is Nothing Then

SelectAllPersonnel4CrewBindingSource.Filter = "Key_positions=-1"

Else

SelectAllPersonnel4CrewBindingSource.Filter = "Key_positions=" & Positionnvalue.ToString()

End If

I saw you use bindingsource.Filter. I guess the value may hide by bindingsource.fiter property. Try to use bindingsource.RemoveFilter() method to check whether the value are all in the combobox dropdownlist. You will also need to change the bindingsource.filter to show the other “disappeared�items.

More information:

BindingSource.Filter Property

http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.filter.aspx

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
  • Marked As Answer byToutou Monday, September 07, 2009 5:26 PM
  •  
Ling Wang  Monday, September 07, 2009 8:29 AM
Ling Wang


Thank you for your answer...
"
I guess the value may hide by bindingsource.fiter property"
this is true because the filter reflects all the datagridview and not the current row...
so ...how can i change this and filter only the current cell that is the focused ....
Toutou  Monday, September 07, 2009 5:45 PM
and as for the bindingsourcre.Reomvefilter when i use it all the values are all in the combobox
Toutou  Monday, September 07, 2009 5:48 PM

->how can i change this and filter only the current cell that is the focused ....

You can handle EditingControlShowing event or CellBeginEdit event. This two method fire when a cell gets focus.

->and as for the bindingsourcre.Reomvefilter when i use it all the values are all in the combobox

You can use this method at CellEndEdit event. (See the following FAQ link)

Filter property depends on your datasource. It just like the select statement in SQL Server. Please refer to BindingSource.Filter Property link in my last post if you have any questions about filter property.

@Linda’s solution shows handling EditingControlShowing event. Here is a FAQ using CellBeginEdit event.

27. How do I have a combo box column display a sub set of data based upon the value of a different combo box column?

http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/a44622c0-74e1-463b-97b9-27b87513747e#faq27

Best regards,
Ling Wang

Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Wednesday, September 09, 2009 3:07 AM

You can use google to search for other answers

Custom Search

More Threads

• Handle the addition and removal of elements (CollectionEditor)
• application "freeze"
• TreeView and Binary File
• The new .NET 2.0 ConfigurationManager and Associated Classes
• Unresponsive UI in WinForms application
• Assigning Image to PictureBox from Stored OLE Image in Microsoft Access Database
• can't understand error...
• Pagination feature for Windows forms grid.
• Constants for Message.msg working with prefilterMessage();
• ToolStrip scrolling with keyboard displays non available items.