Results 1 to 3 of 3

Thread: [RESOLVED] Activate/focus datagrid row.

  1. #1

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    Resolved [RESOLVED] Activate/focus datagrid row.

    Guys,

    In the sub below I am adding a new row to a datagrid on screen. Instead of showing the msgbox at the end of the code, I would like to ensure that the row just added has focus and is visible on screen. This row is always added at the bottom, and sometimes this is off screen if I already have 20 rows in the grid. How can I ensure that the row added is focused and scrolled to if required ?

    Thanks
    Bob



    VB Code:
    1. Friend Sub AddDirectoryItem(ByVal item As cls_icontact)
    2.         Dim row As DataRow
    3.         If displaytype = ContractDirectoryDisplay.Staff Then
    4.             Dim dt As DataTable = CType(dg_contacts.DataSource, DataTable)
    5.             For Each row In dt.Rows
    6.                 If row.RowState = DataRowState.Deleted Then
    7.                 ElseIf row("id") = item.row("id") Then
    8.                     MsgBox("User is already assigned !", MsgBoxStyle.Information, "Duplicate Item")
    9.                     dt.Dispose()
    10.                     dt = Nothing
    11.                     Return
    12.                 End If
    13.             Next
    14.             dt.Dispose()
    15.             dt = Nothing
    16.         End If
    17.         row = CType(dg_contacts.DataSource, DataTable).NewRow
    18.         row.ItemArray = item.row.ItemArray
    19.         row("Contract") = CurrentContract.ID
    20.         If displaytype <> ContractDirectoryDisplay.Staff Then
    21.             row("ContactPhone") = item.row("Phone")
    22.             row("ContactFax") = item.row("Fax")
    23.         End If
    24.         mdtContacts.Rows.Add(row)
    25.         MsgBox("New Contact assigned !", MsgBoxStyle.Information, "Assigned")
    26.     End Sub

  2. #2
    Frenzied Member maged's Avatar
    Join Date
    Nov 2002
    Location
    Egypt
    Posts
    1,040

    Re: Activate/focus datagrid row.

    i do it as follows :

    make a custom control that inherits from the datagrid control

    place this sub in it like this


    VB Code:
    1. public class gridx
    2.     inherits datagrid
    3.  
    4.  'Adding scroll to row Functionality
    5.     Public Sub ScrollToRow(ByVal row As Integer)
    6.         If Not Me.DataSource Is Nothing Then
    7.             Me.GridVScrolled(Me, New ScrollEventArgs(Windows.Forms.ScrollEventType.LargeIncrement, row))
    8.         End If
    9.     End Sub

    after that you can call the method like that
    VB Code:
    1. gridx1.scrolltorow(10)    ' this will scroll to the 10th row

    i know building a custom control is overkilling but i am always extending the datagrid as it is very frusterating in vb.net2003

    rgds

  3. #3

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    Re: Activate/focus datagrid row.

    Thanks Maged,

    Yeah, I had already built a custom datagrid control with lots of added functions / subs already.

    Cheers
    Bob

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width