Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Error when applying filter to datagrid with DataView as datasource
 

Error when applying filter to datagrid with DataView as datasource

I have a datagrid with 4 columns, 3 of which are hidden.  The one that is not hidden is also the sort field and is called "cnt_ProdCatDesc".  The datasource of the datagrid is a dataview which filters the underlying dataset by a value (Business Unit) in a dropdown on the same form.  

The datagrid loads fine and shows the single field (cnt_ProdCatDesc) with all values but when I then change the dropdown it falls over and tells me it can't find the column cnt_ProdCatDesc... even though it's just displayed it no problem!

All I'm doing in the dropdown SelectedIndexChanged event is updating the RowFilter property to it's appropriate value... 
ie: dvwEquipmentCategory.RowFilter = "cnt_BusinessID = '" & <value of drop down> & "'".
I know the value of the dropdown is valid and the rowfilter is correct as I can see them in the locals window... it just won't update the datagrid with the new dataview.

I know it may not mean much but here's the error I'm getting...

Unhandled Exception: System.IndexOutOfRangeException: Cannot find column cnt_ProdCatDesc.
   at System.Data.DataTable.ParseSortString(String sortString)
   at System.Data.DataTable.GetIndex(String sort, DataViewRowState recordStates, IFilter rowFilter)
   at System.Data.DataView.UpdateIndex(Boolean force)
   at System.Data.DataView.SetIndex(String newSort, DataViewRowState newRowStates, DataFilter newRowFilter)
   at System.Data.DataView.set_RowFilter(String value)
   at Contacts.ContactManager.frmContacts.cboEQ_EquipmentCategoryBusinessUnit_SelectedIndexChanged(Object sender, EventArgs e) in D:\Projects\Contacts\Main.vb:line 13212


It looks like a sorting problem... just can't work this out - if anyone has any ideas then I'd love to hear 'em!!

TIA...

Mike.
MigrationUser 1  Thursday, February 13, 2003 8:15 PM
Well, I played around with this and found that by taking out the sort order in the dataview definition it started working again!   I really don't see why the sort order should affect a dataview being populated but there you go... a bug maybe?

Mike.
MigrationUser 1  Thursday, February 13, 2003 9:53 PM
There are other alternatives, if you don't mind performing a round-trip to the server on each selection from the combo box. That is, rather than using the RowFilter to filter the grid, how about setting the SQL that fills the grid to include a parameter that pulls its value from the combo box? That is, add the following to your SQL (assuming you're loading data from SQL Server):

SELECT cnt_ProdCatDesc FROM YourTable WHERE cnt_BusinessID = @ID

Then, in the SelectedIndexChanged event of the combo box, you'll need to satisfy that @ID parameter, which you can do like this, if you've dragged a SqlDataAdapter onto the form from the toolbox:

SqlDataAdapter1.Parameters("@ID").Value = Me.cboEQ_EquipmentCategoryBusinessUnit.SelectedValue.ToString()

or something like that. If you haven't used the UI the create the SqlDataAdapter, you'll need to add the parameter to the Parameters collection yourself.

OTOH, your technique should work fine. I have an example I use for teaching where I have a DataView with both a sort and a RowFilter set, and it works fine--it must be some other issue causing the trouble. If you get the urge, send me a small sample that doesn't work (ken_getzAThotmailDOTcom) and I'll play with it. -- Ken
MigrationUser 1  Monday, February 17, 2003 5:43 PM

You can use google to search for other answers

Custom Search

More Threads

• Child grid on another form question
• how to Add new row when datagrid source bind to collection class ?
• VS2005 WinForm Data Binding Issues
• Focus a header cell in windows datagrid
• TableAdapter Query Results Not Being Displayed in DataGridView
• How to reflect datagrid changes
• datagridviewcomboboxcolumn
• GridView data to an array?
• DataGridView With 2 or 1 TableAdapter?
• Selection of combo and reading the entered rows? of datagridview