Results 1 to 11 of 11

Thread: [RESOLVED] DataGridView ComboBox Columns

  1. #1

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Resolved [RESOLVED] DataGridView ComboBox Columns

    Hello

    Once again i'm stuck here with the DGV, this time with DataGridViewComboBox Columns.

    So i have a datagridview with two combos, when the form it's created i fill the first one with all the MainValues, and the second one with all possible values for the MainValues (called SecondValues) (1..N).
    Now my issue, when the user selects one value from the first combo i want to fill the second combo with all values related to the selected MainValue, i can do this for example by filling the datatable again with a parameter that filters the data, but if the previous rows have different main values, the dgv throws errors because it doesn't find the binded ones in the new filtered datatable.

    How can i handle this? One datatable for each line?!

    Other thing, i'm using the SelectionChangeCommited event for handling the values change (in the EditingControlShowing), but if the user uses the keyboard to select the values the event doesn't behave in the same way or the way that i need.
    The values in the combo are numeric values (1001,1020,2010,3010), when the user select one of the values, i need to fill another cell in the same row with the number selected plus some string, example combo:1001 cell:1001XPTO. So if the user presses 1 in the keyboard the combobox selects the 1001 and fills the cell with 1001XPTO, but if i keep pressing the keyboard to select the 1020, by pressing 0 and 2, i get the 1020 selected in the combo, but the cell remains with the 1001XPTO...
    I already tried the IndexChange, ValueChange, but these events are called several times in the same keyPress, and some of the calls doesn't have any values and i don't want to check every possible option.

    Thanks

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: DataGridView ComboBox Columns

    You don;t need to do anything on any event of the first editing control. You might handle the CellValueChanged event and then clear the second combo box cell if its current value is not valid. As for the second combo box, it doesn't exist until the user starts editing a cell in the second column, so you don't need to do anything until then, i.e. when the EditingControlShowing event is raised for that column. At that point AND NOT BEFORE you bind only the appropriate data to only that ComboBox.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: DataGridView ComboBox Columns

    Thanks once again jmcilhinney

    I don't understand well what i have to do in the filtered combo box...
    You say that i need to handle the EditingControlShowing and inside the event i set the datasource/options? I though that the EditingControlShowing event was raised when the cell entered in edit mode, and not when i set the datasource for the dgv...

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: DataGridView ComboBox Columns

    Quote Originally Posted by mickey_pt View Post
    Thanks once again jmcilhinney

    I don't understand well what i have to do in the filtered combo box...
    You say that i need to handle the EditingControlShowing and inside the event i set the datasource/options? I though that the EditingControlShowing event was raised when the cell entered in edit mode, and not when i set the datasource for the dgv...
    That's correct. What's the problem here? You need a different data source for each row, right? If you need a different data source for each row then what's the point of setting the DataSource for the column? There's isn't one. You should either set the DataSource of each cell individually or just set the DataSource of the editing control as and when required.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: DataGridView ComboBox Columns

    Thanks

    I'll try this tomorrow.

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  6. #6

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: DataGridView ComboBox Columns

    Or i'm missing something or this doesn't work for me...

    I forget to tell, but the complete DGV has a datasource. So when i create the form, i fill the datasource and bind it to the DGV, the ComboBox Columns will replace two of the original datasource columns.
    And like i though the EditingControlShowing only works when the user enters the cell, so when the dgv it's painted/displayed with all the saved values, the event doesn't trigger, if i only have to add new rows i think this could work...

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  7. #7

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: DataGridView ComboBox Columns

    I'm struggling here with the datasource changed event to set the datasource for each line that already exists in the database, but i don't know why the items doesn't show up in the combo, i check in debug and i see that i have items in the datasource, but once the dgv it's displayed the combo it's empty.

    I have created a small application just to test this.
    vb.net Code:
    1. '... The vars are all declared
    2.  Public Sub New()
    3.  
    4.         ' This call is required by the Windows Form Designer.
    5.         InitializeComponent()
    6.  
    7.         ' Add any initialization after the InitializeComponent() call.
    8.         adRegistos = New SqlClient.SqlDataAdapter(sqlRegistos)
    9.         adRegistos.Fill(dt)
    10.  
    11.         'Column
    12.         Dim colPF As New DataGridViewComboBoxColumn
    13.         With colPF
    14.             .Name = "PF2"
    15.             '.DataSource = DataTablePFs Removed
    16.             '.DataPropertyName = "PF"
    17.         End With
    18.        
    19.         DataGridView1.Columns.Add(colPF)
    20.         DataGridView1.DataSource = dt    
    21.     End Sub

    Then the rest of code it's in the DataSourceChangeEvent:
    vb.net Code:
    1. Private Sub DataGridView1_DataSourceChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.DataSourceChanged
    2.       For Each Row As DataGridViewRow In DataGridView1.Rows
    3.             If Row.IsNewRow Then Exit For
    4.  
    5.             sql = New SqlClient.SqlCommand("dbo.PontoPF_Test", sqlCon)
    6.             sql.CommandType = CommandType.StoredProcedure
    7.             sql.Parameters.Clear()
    8.             sql.Parameters.AddWithValue("@Var1", 9)
    9.             sql.Parameters.AddWithValue("@Var2", Row.Cells("ReferenceCol").Value)
    10.             ad = New SqlClient.SqlDataAdapter(sql)
    11.             ad.Fill(DataTablePFs)
    12.             Dim dgvCB As DataGridViewComboBoxCell = CType(Row.Cells("PF2"), DataGridViewComboBoxCell)
    13.             dgvCB.DisplayMember = "NomePF"
    14.             dgvCB.ValueMember = "idPF"
    15.             dgvCB.DataSource = DataTablePFs.Copy()
    16.            
    17.  
    18.         Next
    19.  
    20.     End Sub
    Last edited by mickey_pt; Nov 11th, 2010 at 03:11 AM. Reason: English :)

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  8. #8

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: DataGridView ComboBox Columns

    No one?

    I have tried a new way.

    Fill the two datatables use two bindingsources and set the datasource of each of them to the datatables, then set them as datasource of the columns and declare an auxiliar bindingSource to act as the filter (filterBindingSource), pointing to the table that fills the second combo.
    After this in the editingControlShowing event i check if the user is editing the second combo if that's it's true i set the filter property of the filterBindingSource and use it for the current combo datasource, using the value selected in the first comobo as filter.
    This almost works, but sometimes when adding a new line, editing some value in the same line, selecting some value in the first combo, the text displayed in the second combo disappears.

    vb.net Code:
    1. Private Sub DataGridViewRegistos_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridViewRegistos.EditingControlShowing
    2.         If DataGridViewRegistos.CurrentCell.ColumnIndex = DataGridViewRegistos.Rows(DataGridViewRegistos.CurrentRow.Index).Cells("PF2").ColumnIndex Then
    3.             Dim control As DataGridViewComboBoxEditingControl = e.Control
    4.             Dim bs As BindingSource = CType(control.DataSource, BindingSource)
    5.  
    6.             If bs IsNot Nothing Then
    7.                 CType(e.Control, ComboBox).DataSource = filterBindingSource
    8.                 Dim obra As Object = DataGridViewRegistos.Rows(DataGridViewRegistos.CurrentCell.RowIndex).Cells("OBRA2").Value
    9.  
    10.                 If obra Is Nothing OrElse obra Is DBNull.Value Then
    11.                     filterBindingSource.Filter = "obra=-1"
    12.                 Else
    13.                     filterBindingSource.Filter = String.Format("obra={0}", obra)
    14.                 End If
    15.  
    16.                 If DataGridView1.CurrentCell IsNot Nothing AndAlso DataGridView1.CurrentCell.Value IsNot DBNull.Value Then
    17.                     control.SelectedValue = DataGridView1.CurrentCell.Value
    18.                 End If
    19.  
    20.             End If
    21.         End If
    22.     End Sub

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  9. #9

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: DataGridView ComboBox Columns

    @JMC

    Sample added.
    This is one of the ways that i implemented trying to solve this.
    Attached Files Attached Files

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  10. #10

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: DataGridView ComboBox Columns

    No one?

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  11. #11

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: DataGridView ComboBox Columns

    Just to close this thread and post the solution.

    I used two dataviews (dvTotal,dvFilter), set the datasource for the combo box column the complete datatable.

    When filling the DataTable, i instantiate the dataviews:

    vb.net Code:
    1. dvTotal = New DataView(DataTableName)
    2. dvFilter = New DataView(DataTableName)

    Then in the CellBeginEdit event, check if it's the combobox column, if it's the combo then just set the datasource for the current cell:
    vb.net Code:
    1. If YourDataGridView.Columns(e.ColumnIndex).Name = "YourComboColumnName" Then
    2.             Dim valueToFilter As Object = YourDataGridView.Rows(e.RowIndex).Cells("FilterColumn").Value
    3.             Dim cbCell As DataGridViewComboBoxCell = CType(YourDataGridView(e.ColumnIndex, e.RowIndex), DataGridViewComboBoxCell)
    4.             cbCell.DataSource = dvFilter
    5.             If valueToFilter Is Nothing OrElse IsDBNull(valueToFilter ) Then
    6.                 dvFilter.RowFilter = "FilterColumn=-1"
    7.             Else
    8.                 dvFilter.RowFilter = String.Format("FilterColumn={0}", valueToFilter )
    9.             End If
    10.         End If

    Then in the CellEndEdit you need to remove the filter:

    vb.net Code:
    1. If YourDataGridView.Columns(e.ColumnIndex).Name = "CentroCusto" Then
    2.             Dim cbCell As DataGridViewComboBoxCell = CType(YourDataGridView(e.ColumnIndex, e.RowIndex), DataGridViewComboBoxCell)
    3.             cbCell.DataSource = dvTotal
    4.             dvFilter.RowFilter = String.Empty
    5.         End If


    Rate People That Helped You
    Mark Thread Resolved When Resolved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width