Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

datagrid - Filter combobox in a datgrid based on another combobox vb.net

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

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I found a soulution provided by vb-tips.com It seems to work great and filters the DGV upon loading the form here is my code; http://codepaste.net/wra8qw


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...