I have a readonly UltraGrid that is bound to a datasource that contains an ID column as the PK for the collection. I would like to select a specific row based upon the ID and highlight/activate it programmatically. The grid should scroll to the proper location if the selected row is not visible.
QUESTION: Is there an example of how I can do this?
The grid follows the current record of the data source to which it is bound by default, meaning you would just have to set the binding manager's Position property to the index of the record that you want to "select":
UltraGrid grid = this.ultraGrid;BindingManagerBase bm = grid.BindingContext[grid.DataSource, grid.DataMember];bm.Position = 0;
Note that you can also select and activate an UltraGridRow programmatically; set the UltraGrid.ActiveRow property to activate the row, and use the Rows property of the Selected object to select a row:
this.ultraGrid.ActiveRow = row;this.ultraGrid.Selected.Rows.Add( row );
If there are too many rows in the grid and the grid has a scroll bar , how to get that particular row in view? system shud automatically scroll the grid to that row...
If you are only dealing with a single row here, then I'd recommend that you set grid.ActiveRow to the row you want, instead of selecting the row. That will highlight the row (by default), and also scroll that row into view automatically.
But if that won't work for you, then what you can do is use the grid.ActiveRowScrollRegion. This object has a FirstRow property which determines the first visible row, so you can set that to the row you want. You could also use the ScrollRowIntoView method if you don't care if the row is first, but just want it to be somewhere in the viewable area.
Manager - Windows Forms Development
When I choose a different row in my grid I have a Message Box prompt asking if I want to save. This message box seems to make the grid lose focus so there are no rows selected. After the save, the data gets refreshed in the grid and the focus goes back to the first row. The first row is getting the focus by setting the grid.ActiveRow.
This seems to work initially but when I hover over a field in the active row, the ActiveAppearance.BackColor goes away. If I select another row in the grid, the selector indicator still shows on the first row plus also on the new row I have selected. It almost seems as if the grid row thinks it has the focus but it really doesn't.
If I just save without selecting another row it seems to work fine. Seems to be only when the row loses focus and then the grid reloads.
What version of the grid are you using? There were some old bugs a long time ago where the appearances on a row weren't properly refreshed so a row might maintain the selected or active appearance even after it was no longer selected/active.
Showing a MessageBox doesn't have any effect on the ActiveRow or the SelectedRows, by the way. Nor would losing focus on the grid. Showing a MessageBox would take the ActiveCell out of edit mode (if it was in edit mode). But that's not really the same thing.
We are on version 15.2.20152.2023
Well, the bug I was referring to was fixed years ago, so that's not it.
Can you reproduce the issue in a small sample and post it here so we can investigate?
Thanks for the quick reply.
It seems that if the MessageBox.Show is in the BeforeRowActivate then
this is where the issue is happening. It is prompting to save
(MessageBox.Show) before changing rows and if the user chooses
yes then it resets the dataview for the grid. At the point of
the prompt coming up, it looks as if the current row is no longer
Private Sub grdRequisitions_BeforeRowActivate(sender As Object, e As Infragistics.Win.UltraWinGrid.RowEventArgs) Handles grdRequisitions.BeforeRowActivate
Dim dsEmail As New DataSet
dsEmail.Tables("dsEmailOptions").Columns.Add(New DataColumn("AttachmentPath", GetType(String)))
dsEmail.Tables("dsEmailOptions").Columns.Add(New DataColumn("DisplayName", GetType(String)))
Dim drNew As DataRow = dsEmail.Tables("dsEmailOptions").NewRow
drNew.Item("AttachmentPath") = "Path"
drNew.Item("DisplayName") = "Display"
drNew = dsEmail.Tables("dsEmailOptions").NewRow
drNew.Item("AttachmentPath") = "Path2"
drNew.Item("DisplayName") = "Display2"
Dim dv As New DataView(dsEmail.Tables(0))
If Me.DesignMode OrElse Me.FormIsBusy Then Exit Sub
Dim intanswer As DialogResult
'The line below is where the grid is seeming to lose focus
intanswer = MessageBox.Show("save changes before continuing?", SuGlobal.SystemSettings.ApplicationTitle, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
If intanswer = Windows.Forms.DialogResult.Yes Then
'This would normally be where the data gets refreshed for the grid if the row has changed
Catch ex As Exception
LogError(ex, System.Reflection.MethodInfo.GetCurrentMethod().Name, "")
I should mention, I moved the MessageBox.Show into the AfterRowActivate event (so it prompts after the new row gets selected) and then it seems to work fine. As of now this seems to take care of my issue.
In order to investigate this, I'd need a small sample that I can run and debug which demonstrates the problem. This code snippet isn't really enough, because I don't know exactly which tables you are modifying, which row you are editing, what band it's in, etc.
I tried using your code snippet to duplicate the problem, but I don't see anything like what you describe happening in my test. The code you have here doesn't really even do anything. The DataSet you are creating isn't even used, so the only significant part is the MessageBox. I have attached my sample here so you can see if you get the same results. Or maybe you can modify the sample so that it shows the problem occurring.
I am having difficulty setting a row to active pro grammatically as well. I am trying:
For i As Int32 = 0 To ugrdSearch.Rows.Count - 1
If Convert.ToInt64(ugrdSearch.Rows(i).Cells(strColName).Value) = ImgID Then
ugrdSearch.ActiveRow = i
But I get intellisense that "Value of type integer cannot be converted to UltraGridRow.
New to Infragistics...