Link to home
Start Free TrialLog in
Avatar of RekhaShah
RekhaShah

asked on

Sort or filter unbound column of a bound datagridview

I have a bound datagridview. I have added two unbound columns. After i verify the data of the  grid, set forcolor of the cell that has invalid value and i set this unbound column's checkbox to be true. In order for me to be able to check, i had to set the grid's virtualmode = false . So far everything is working well.
Now, I want to sort my grid based on this new unbound column i call "FailedData" so that all the rows with checked "FailedData" will be together so that user knows which rows should be edited. i can either filter and show only the rows with checkbox checked or sort. But according to msdn, you can not sort  unbound column of a bound datagriedview.
Any suggestion?
Avatar of UnifiedIS
UnifiedIS

Can you include the 2 new columns with your bound data source?  They don't need a value until your validation and you could hide them (visible = false) until you need to show them if you want.

Avatar of RekhaShah

ASKER

i guess, I can try it.  But ideally, i would like to sort of filter. Can you filter rows based on formatting? e.g.display only the rows with any of the cell with red fore color?
If your 2 new columns are part of your data source then you will be able to sort by those columns.  

yes, could filter based on formatting but I don't think you could sort based on formatting.
How would you filter based on formatting?
 also, even for filtering based on formatting, I do need to make these columns part of my database. right?
Filtering based on formatting is possible but logically, it's backwards.

As for part of the database, you don't need to add columns to tables but you do need to add columns to your query.

A SQL query with a "fake" column to be populated later in code:
SELECT *, '' AS FailedData FROM Table

So,  i think, i should populate  my FailedData column in the stored procedure and then get the recordset, and now business as usual? Let me try it this evening and post back again.  i was  thinking about doing the same thing, but thought if I can do it at UI level, it will save me the trouble of writing a complex stored procedure.
Thanks
I think that will do it.  Your stored procedure doesn't need to include any additional logic than is currently being used to provide data.  You just need to add a column which you will populate via your UI.  When you verify your data, populate the FailedData column with some value and you will be able to sort those to the top or bottom using the datagridview's sort method.
It does not work because, I have all the columns readonly. I added FailedData column to database table. but now during verify process, when I set the checkbox of this column to true, first time it works and then it gives me an error that datareader is already open.close first!
ASKER CERTIFIED SOLUTION
Avatar of UnifiedIS
UnifiedIS

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks you for your help.
I implemented nested datareader so that going through each row, if i found any invalid data, i could set FailedData field to true. I am posting this comment so that it may be useful to someone else. But inorder to tdo nested Datareaders, I had to set MARS=true in sql connection string. That did the trick.