Programmatically selecting/activating a row?

Answered (Not Verified) This post has 0 verified answers | 11 Replies | 4 Followers Thread's RSS feed.

ericgeas
Points 1,170
Replied On: Wed, Sep 16 2009 5:05 PM Reply

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?

  • Post Points: 20

All Replies

Answered (Not Verified) Replied On: Thu, Sep 17 2009 9:55 AM Reply

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 );

  • Post Points: 20
Swe2
Points 1,475
Replied On: Sat, Mar 26 2011 2:39 PM Reply

Brian,

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...

Thanks!

Swetha.

  • Post Points: 20
Mike Saltzman
Points 458,400
Infragistics Employee
Answered (Not Verified) Replied On: Mon, Mar 28 2011 9:59 AM Reply
Suggested by Tess1243

Hi Swetha,

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.

 

Mike Saltzman

Manager - Windows Forms Development

Infragistics, Inc.

  • Post Points: 35
ljksui
Points 85
Replied On: Wed, Aug 10 2016 4:18 PM Reply

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.

Any suggestions?

  • Post Points: 20
Mike Saltzman
Points 458,400
Infragistics Employee
Replied On: Wed, Aug 10 2016 5:07 PM Reply

Hi ljksui,

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.

Mike Saltzman

Manager - Windows Forms Development

Infragistics, Inc.

  • Post Points: 20
ljksui
Points 85
Replied On: Wed, Aug 10 2016 5:09 PM Reply

We are on version 15.2.20152.2023

  • Post Points: 20
Mike Saltzman
Points 458,400
Infragistics Employee
Replied On: Thu, Aug 11 2016 1:36 PM Reply

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? 

Mike Saltzman

Manager - Windows Forms Development

Infragistics, Inc.

  • Post Points: 20
ljksui
Points 85
Replied On: Thu, Aug 11 2016 2:32 PM Reply
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 
selected.
  
Private Sub grdRequisitions_BeforeRowActivate(sender As Object, e As Infragistics.Win.UltraWinGrid.RowEventArgsHandles grdRequisitions.BeforeRowActivate
        Try
 
            Dim dsEmail As New DataSet
            dsEmail.Tables.Add("dsEmailOptions")
            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"
            dsEmail.Tables(0).Rows.Add(drNew)
 
            drNew = dsEmail.Tables("dsEmailOptions").NewRow
            drNew.Item("AttachmentPath") = "Path2"
            drNew.Item("DisplayName") = "Display2"
            dsEmail.Tables(0).Rows.Add(drNew)
 
            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
            End If
        Catch ex As Exception
            LogError(ex, System.Reflection.MethodInfo.GetCurrentMethod().Name, "")
 
        End Try
    End Sub
  • Post Points: 5
ljksui
Points 85
Replied On: Thu, Aug 11 2016 2:59 PM Reply

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.

  • Post Points: 20
Mike Saltzman
Points 458,400
Infragistics Employee
Replied On: Fri, Aug 12 2016 10:49 AM Reply

Hi, 

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. 

Mike Saltzman

Manager - Windows Forms Development

Infragistics, Inc.

  • Post Points: 5
Matthew Carr
Points 5
Replied On: Fri, Aug 12 2016 2:45 PM Reply

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

       Exit Sub

   End If

Next

But I get intellisense that "Value of type integer cannot be converted to UltraGridRow.

New to Infragistics...

  • Post Points: 5
Page 1 of 1 (12 items) | RSS