根据另一个组合框vb.net过滤datgrid中的组合框 [英] Filter combobox in a datgrid based on another combobox vb.net

查看:63
本文介绍了根据另一个组合框vb.net过滤datgrid中的组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我认为我的问题是描述性的,或者像Microsoft在数据网格文档"中那样,问题是,我如何具有一个组合框列,以根据不同组合框列的值显示子数据集?
我有一个3表填充的DS,客户,订单,OrderDetails.订单详细信息位于具有两个组合框列的DataGridView中,其中1为位置,1为产品.两者都来自单独的查找数据集.
我想要的是,当用户选择位置"组合时,应将产品"组合过滤到其可用位置.产品通过LocationID与位置相关

这是文档中的解决方案,但对我来说确实有用.

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(); 
} 
}

推荐答案

第一点.您已标记了问题VB.Net,但代码示例为C#.

您是否尝试过在以下位置设置断点:
First point. You have tagged your question VB.Net but the code sample is C#.

Have you tried setting a breakpoint on:
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?



抱歉,我将代码转换为vb.
由于我是新手,因此需要一些帮助,

1)我应该在Form Load事件中将上面qout的代码放在哪里?

2)如果是,那么DataGridView如何能够访问FilteredTerritoriesBS

这是转换后的代码;
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;
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)根据此示例代码和我的示例,任何人都可以指导我,对于位置"组合(父项),我需要更改什么,对于产品"组合(子项),我需要更改什么,我需要在哪里指定这两个数据源?
我非常感谢您的帮助,在您的帮助下,我们所有人都可以成为专业的开发人员!!



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!!


嗨Yaddenn,

我不知道您是否找到解决此问题的方法,但我一直在寻找相同的东西.

我在MSDN中找到了一篇很好的文章

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

阅读文章后,我可以使用以下代码为我解决问题:
重要的是要知道我已经在Visual Studio中设置了具有2个表的数据集.

最上面的表格名称是Crop
明细表名称CropType
在我的数据集中,我还在表之间创建了一个链接,称为FK_CropType_Crop

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


这篇关于根据另一个组合框vb.net过滤datgrid中的组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆