Click here to Skip to main content
15,912,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All, I think my question is discriptive, or as Microsoft in the Documentation for Data Grid the question is, How do I have a combo box column display a sub set of data based upon the value of a different combo box column?
I have a DS with 3 tables filled, Customers, Orders, OrderDetails. The order details is in a DataGridView with two combo boxes columns, 1 is the Location, and 1 the products. Both come from seperate Lookup Datasets.
What I want is that when the user selects the Location combo , the Products combo should be filtered to the locations its availiable. The Products is related to location by the LocationID

This is the solution from the documentation but it dosent work for me.

private void Form1_Load(object sender, EventArgs e) 
{ this.territoriesTableAdapter.Fill(this.northwindDataSet.Territories); this.regionTableAdapter.Fill(this.northwindDataSet.Region);
// Setup BindingSource for filtered view. 
filteredTerritoriesBS = new BindingSource(); 
DataView dv = new DataView(northwindDataSet.Tables["Territories"]); 
filteredTerritoriesBS.DataSource = dv; 
}
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) { if (e.ColumnIndex == territoryComboBoxColumn.Index) { // Set the combobox cell datasource to the filtered BindingSource DataGridViewComboBoxCell dgcb = (DataGridViewComboBoxCell)dataGridView1 [e.ColumnIndex, e.RowIndex]; dgcb.DataSource = filteredTerritoriesBS;
    // Filter the BindingSource based upon the region selected 
    this.filteredTerritoriesBS.Filter = "RegionID = " + 
        this.dataGridView1[e.ColumnIndex - 1, e.RowIndex].Value.ToString(); 
} 
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == this.territoryComboBoxColumn.Index) { // Reset combobox cell to the unfiltered BindingSource DataGridViewComboBoxCell dgcb = (DataGridViewComboBoxCell)dataGridView1 [e.ColumnIndex, e.RowIndex]; dgcb.DataSource = territoriesBindingSource; //unfiltered
    this.filteredTerritoriesBS.RemoveFilter(); 
} 
}
Posted

First point. You have tagged your question VB.Net but the code sample is C#.

Have you tried setting a breakpoint on:
C#
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) { if (e.ColumnIndex == territoryComboBoxColumn.Index) { // Set the combobox cell datasource to the filtered BindingSource DataGridViewComboBoxCell dgcb = (DataGridViewComboBoxCell)dataGridView1 [e.ColumnIndex, e.RowIndex]; dgcb.DataSource = filteredTerritoriesBS;
    // Filter the BindingSource based upon the region selected
    this.filteredTerritoriesBS.Filter = "RegionID = " +
        this.dataGridView1[e.ColumnIndex - 1, e.RowIndex].Value.ToString();
}


Then you can examine the filter text to ensure that you are getting the correct column/value?
 
Share this answer
 
Hi,
Sorry about that, I converted the code to vb.
Since I am a newbie I need some help,

1) Where should I put the code you qouted above, in the Form Load event??

2) If So how will the DataGridView be able to access the FilteredTerritoriesBS

Here is the converted Code;
VB
Private Sub Form1_Load(sender As Object, e As EventArgs)
    Me.territoriesTableAdapter.Fill(Me.northwindDataSet.Territories)
    Me.regionTableAdapter.Fill(Me.northwindDataSet.Region)

    ' Setup BindingSource for filtered view.
    filteredTerritoriesBS = New BindingSource()
    Dim dv As New DataView(northwindDataSet.Tables("Territories"))
    filteredTerritoriesBS.DataSource = dv

End Sub

Private Sub dataGridView1_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs)
    If  e.ColumnIndex = territoryComboBoxColumn.Index Then
        ' Set the combobox cell datasource to the filtered BindingSource
        Dim dgcb As DataGridViewComboBoxCell = DirectCast(dataGridView1(e.ColumnIndex, e.RowIndex), DataGridViewComboBoxCell)
        dgcb.DataSource = filteredTerritoriesBS

        ' Filter the BindingSource based upon the region selected
        Me.filteredTerritoriesBS.Filter = "RegionID = " & Me.dataGridView1(e.ColumnIndex - 1, e.RowIndex).Value.ToString()
    End If
End Sub

Private Sub dataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs)
    If e.ColumnIndex = Me.territoryComboBoxColumn.Index Then
        ' Reset combobox cell to the unfiltered BindingSource
        Dim dgcb As DataGridViewComboBoxCell = DirectCast(dataGridView1(e.ColumnIndex, e.RowIndex), DataGridViewComboBoxCell)
        dgcb.DataSource = territoriesBindingSource
        'unfiltered
        Me.filteredTerritoriesBS.RemoveFilter()
    End If
End Sub


3)Based on this sample code and my example, Can anyone guide me please what will I need to change for the "Location" Combo (Parent), and what for my "Products" combo (Child), and where I need to specify The two DataSources??
I Appreciate any help, with your help we all can become professional Developers!!
 
Share this answer
 
v2
Comments
yaddenn 21-Oct-10 23:42pm    
Can anyone help design this with the visual tools not in code, it might be a bit easier??
(a newbie`s perspective)..
yaddenn 27-Oct-10 23:03pm    
Hi evreyone!
I found a solution provided by vb-tips.com
here is my code which works (partially upon loading the form)
http://codepaste.net/wra8qw
Hi Yaddenn,

I don't know if you found a sollution for this question, but I have been looking for the same thing.

I found a very good article in the MSDN

http://msdn.microsoft.com/en-us/library/c12c1kx4.aspx[^]

After reading the article I was able to solve the issue for me with the following Code:
Important to know is that I have setup a Dataset in Visual Studio that has 2 tables.

The top table name is Crop
The Detail table name CropType
In my dataset I also created a link between the tables wich is called FK_CropType_Crop

Public Class TTCreateTemplate
    Inherits System.Windows.Forms.Form
    Dim CreateExcelClass As New CreateExcel
    Private masterBindingSource As New BindingSource()
    Private detailsBindingSource As New BindingSource()
    
Private Sub TTCreateTemplate_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Bind the DataGridView controls to the BindingSource
        ' components and load the data from the database.
        ComboBox_Crop.DataSource = masterBindingSource
        ComboBox_CropType.DataSource = detailsBindingSource
        GetData()
        ComboBox_Crop.DisplayMember = "crop_name"
        ComboBox_CropType.DisplayMember = "croptype_name"
    End Sub
    Private Sub GetData()
        'TODO: This line of code loads data into the 'TrialTracker.Crop_CropType' table. You can move, or remove it, as needed.
        Me.CropTableAdapter.Fill(Me.TrialTracker.Crop)
        'TODO: This line of code loads data into the 'TrialTracker.CropType' table. You can move, or remove it, as needed.
        Me.CropTypeTableAdapter.Fill(Me.TrialTracker.CropType)
        ' Bind the master data connector to the Customers table.
        masterBindingSource.DataSource = TrialTracker
        masterBindingSource.DataMember = "Crop"
        ' Bind the details data connector to the master data connector,
        ' using the DataRelation name to filter the information in the 
        ' details table based on the current row in the master table. 
        detailsBindingSource.DataSource = masterBindingSource
        detailsBindingSource.DataMember = "FK_CropType_Crop"
    End Sub

End Class
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900