[RESOLVED] [2008] Simple question : hiding the last visible row in a datagridview
This must be easy but I've drawn a blank so far.
I have a datagridview which I've populated manually (ie not data bound), and I've implemented some filtering functionality that hides rows meeting certain criteria.
The problem I have is that if I hide the last visible row I get an exception..."Current cell cannot be set to an invisible cell."
I can understand that, however it must be possible to have a datagridview without any rows, so what happens then.
How can I tell the datagrid that it hasn't got a current cell?
Any help much appreciated.
Re: [2008] Simple question : hiding the last visible row in a datagridview
What if you try to call this right before you make the row throwing the exception invisible
Code:
DataGridView1.CurrentCell = Nothing
this code deselects the currentcell if there is one..
Re: [2008] Simple question : hiding the last visible row in a datagridview
Still the same problem I'm afraid.
The exception is actually thrown at the end of the CellValueChanged event, not where I suspected it was (at the point of making the row invisible).
My code is :
Code:
Private Sub dgvMain_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvMain.CellValueChanged
'write data back to object
Dim sValue As String
sValue = dgvMain.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
m_objList.Records(e.RowIndex + 1).Values(e.ColumnIndex + 1).value = sValue
'check to see if we may need to apply conditional formatting or filters
Call ProcessSingleRowFormat(dgvMain.Rows(e.RowIndex))
m_objList.IsDirty = True
End Sub
The row hiding is actually done in the "ProcessSingleRowFormat" routine, but if you step through the code that routine completes succesfully , the exception is thrown back in this subroutine when stepping on from End Sub.
Re: [2008] Simple question : hiding the last visible row in a datagridview
So its the cell that was changed and fired off this event that you are trying to make invisible?
Re: [2008] Simple question : hiding the last visible row in a datagridview
Yes.. the scenario is this -
I have a datagrid populated with 5 rows. I apply a filter so that only rows with "PCS" in the "Owner" column are shown.
That reduces the number of rows shown to just the one.
The "Owner" column is a combo-box column and I click on the owner column dropdown and change the entry to "ALS", and then hit enter.
What my code is trying to do is reapply the filter on the current row, and it correctly identifies that this row no longer fits the filter criterion and so hide it.
It does all this correctly but then as there are no visible rows it then falls over.
Re: [2008] Simple question : hiding the last visible row in a datagridview
If your filter reduced the number of rows down to 2 instead of 1, and you change the combobox for one record as you said above, do you still get the error, or is it ONLY in the case where no records remain after filtering?
Re: [2008] Simple question : hiding the last visible row in a datagridview
Ah... good call - its not just when its the last row but whenever the change in a value means it gets filtered out when I reapply the filtering within the CellValueChanged event.
Hmm... I've just done an experiment to see how Excel handles it if you have auto-filter on a table of data and you change a value which the table filters on. Excel just ignores it and doesn't re-fresh the filtered results.
I suppose I could follow that lead - not very intuitive but I guess if its good enough for Microsoft...
Re: [2008] Simple question : hiding the last visible row in a datagridview
when you said you are doing this unbound, are you looping and adding a bunch of rows?
How are you filtering? Are you using a binding source?
Re: [2008] Simple question : hiding the last visible row in a datagridview
Yes - I have an object which has both a collection of column objects which define the appearance and format of the columns in the grid, and a collection of record objects which define the data.
Each Column also has a collection of conditional format objects and filters which are applied in the "ProcessSingleRowFormat" routine to either set colour schemes for rows based on data content, or to hide them as part of filtering.
I loop through the records and add rows manually and after each one is created (or edited) the conditional formats and filters are reapplied by a call to this ProcessSingleRowFormat routine.
It seems the crux of the issue is - as you infer above - that the problem is because I'm hiding the row during an event belonging to that row. I can understand this being a problem if I was actually deleting the row, but thought that just making it invisible wouldn't cause any problems.
Re: [2008] Simple question : hiding the last visible row in a datagridview
How exactly is the filter applied though?
I feel like maybe you arent actual filtering, versus just making rows invisible..
Re: [2008] Simple question : hiding the last visible row in a datagridview
Quote:
Originally Posted by kleinma
How exactly is the filter applied though?
I feel like maybe you arent actual filtering, versus just making rows invisible..
Thats right - by filtering i mean a manual process of examining the data and hiding rows that don't meet any criteria specified in the column's filters collection. I'm not using any form of data binding.
Sorry if I'm misusing terminology - I meant as far as the user is concerned I am filtering.