Hello!
I have a grid in which I show messages. All messages have a date column and the rows are sorted according to date.
So we have old messages, messages that are for the future and we have "hot" messages which are to be shown today so to speak.
What I want to do is to scroll the grid automatically to get the first message today to appear in the top of the grid.
After a lot of reading in here within this topic, I still couldn't find a solution that worked.
So I am kindly asking for help.
What I have is a grid with many columns but all of them except one is hidden. I have changed the visible cell data using the editor component, and this is done in InitializeRow event.
I am binding the datasource when the form is loaded, and then I use the InitializeLayout for column changes and the InitializeRow for theeditor component work.
I also evaluate which row that should be in the top of the grid in InitializeRow event.
My problems are:
1. I have placed the ScrollIntoViewCode the row after the datasource binding. I get a timing problem the first time I run it since the row to be placed in the top isn't set. Where shall I place the code to functionally all the time. I have used both:
grdNotifications.ActiveRowScrollRegion.FirstRow = ActiveRow_Notifications grdNotifications.ActiveRowScrollRegion.ScrollRowIntoView(ActiveRow_Notifications)
Which one is to prefer?
2. Secondly, when I have a row to scroll for, it looks like the rows are squezed together.
I have set the row height to AutoFixed, but since I have a usercontrol as the editor component, it might be the problem. It looks like the grid returns to standard row height after the scrolling.
I have attached to images that might help out
Hi,
A part of this code in the paint event is likely causing the Red X. We'll require a sample to further investigate this for you. Will you be able to isolate this behavior and send us sample? Let me know if you have any questions regarding this matter.
OK, it is implemented.
In the paint event, I am looping through the GetFilteredInNonByGroupByRows Collections, since I Believe that it is that one that gives me the visible rows.
I am also using a flag in the pain event, so when I have looped trough the rows of the grid and set which row to be the first one, I am setting the flag to prevent the code from being executed over and over again.
The grid datasource is bound when the form is loaded, or when the timer strikes.
So in the sub routine that binds the datasource, I start that one with setting the flag so that it can enter the paint event again.
It works as far as I can see. The problem is that when I try to set the first row in the grid, in the paint even, the renderingen of the Control fails and I get some sort of undercover error. I get a big red cross over the grid Control.
Do you know why?
Here is my code.
Private Sub InitGrid_Notifications()
Try
mPaint = True
grdNotifications.DataSource = gManager.GetActiveNotificationsDB(gUserDomain.DomainID, gUser.UserID)
Dim argsType As New BeforeRowFilterDropDownEventArgs(grdNotifications.DisplayLayout.Bands(0).Columns("Type"), grdNotifications.Rows, grdNotifications.DisplayLayout.Bands(0).Columns("Type").ValueList)
grdNotifications_BeforeRowFilterDropDown(grdNotifications, argsType)
Catch ex As Exception
MsgBox("InitGrid_Notifications")
End Try
End Sub
Private Sub grdNotifications_Paint(sender As Object, e As PaintEventArgs) Handles grdNotifications.Paint
If mPaint Then
Dim ActiveRow As UltraGridRow = Nothing
For Each r As UltraGridRow In grdNotifications.Rows.GetFilteredInNonGroupByRows
If CType(r.Cells("TargetDate").Value, Date).ToShortDateString = Date.Now.Date.ToShortDateString Then
ActiveRow = r
Exit For
End If
Next
grdNotifications.ActiveRowScrollRegion.FirstRow = ActiveRow ' Fails here if not commented out
mPaint = False
'
MsgBox("grdNotifications_Paint")
Yes. Just keep in mind that you only want to do it the first time the grid paints. Unless you want it to happen continuously and not allow the user to scroll at all. Even so, the Paint event is probably not very efficient if you are going to do it continuously. If that's what you need, then I'd say use a timer, but don't start the timer until the first time the grid paints.
Good point there Mike. I missed the filters.
So your suggestion is to loop though the grid in the paint event, find the row that i want to have as the top row, and set the firstrow property?
Hi Henrik,
I'm not quite following the problem with Initialize Row. But there is no reason to both set the FirstRow AND also call ScrollRowIntoView.
Setting FirstRow will scroll so that the row is the first visible row in the ScrollRegion. So it will always be in view. It is possible for FirstRow to fail if your ScrollBounds is set to ScrollToFill, but I think in that case, it scrolls it as close as possible to the top.
ScrollRowIntoView just ensures that the row is somewhere in view - it's a completely arbitrary position, though.
It's not really clear to me exactly what you are trying to do. Getting the index of the row in the data source and then using that index to get the grid row is a bit strange. If your grid is sorted or filtered, then that index will be meaningless, since the order of the rows in the grid will be different from the data source. I think you need to loop through the grid rows, instead, to find the row you want.