Windows Develop Bookmark and Share   
 index > Windows Forms General > How to focus on a row in a Datagridview control
 

How to focus on a row in a Datagridview control

In a Windowws form, I have a textbox and a datagridview control. The textbox is used to search a record on the datagridview control and to set focus on that record. It will locate the record and highlight it when that record is displayed on the datagridview. If that record is on the next page(s) on the datgridview and it will highlight some other record showing on the first page ie how to make the datagridview scroll to the page with the found record.

Private Sub txt_search_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txt_search.KeyUp

With Me.dgv_son_physician_member.Columns(0)

For Each dgvr As DataGridViewRow In Me.dgv_son_physician_member.Rows

Dim name As String

Dim row As Integer

row = dgvr.Index

name = Me.dgv_son_physician_member.Item(0, row).Value.ToString

Dim rx As New Regex(Me.txt_search.Text.ToString, RegexOptions.Compiled Or RegexOptions.IgnoreCase)

If rx.IsMatch(name) Then

Me.dgv_son_physician_member.Item(0, row).Selected = DataGridViewElementStates.Displayed

Exit Sub

End If

Next

End With

End Sub

pmak  Tuesday, July 31, 2007 2:06 AM

Hi pmak,

To make the datagridview scroll to the page with the found record, you can set DataGridView.FirstDisplayedScrollingRowIndex Property or DataGridView.FirstDisplayedCell Property. However, to find the record in DataGridView, I prefer to use the BindingSource.Find method. See my sample below:

Code Snippet

Class DGVSearchItem

Private dt As New DataTable()

Private bs As New BindingSource()

Private Sub DGVSearchItem_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

dt.Columns.Add("fldID")

dt.Columns.Add("fldName")

For i As Integer = 0 To 49

dt.Rows.Add(i.ToString(), "Name" + i.ToString())

Next

bs.DataSource = dt.DefaultView

Me.DataGridView1.EditMode = DataGridViewEditMode.EditOnF2

Me.DataGridView1.DataSource = bs

Me.DataGridView1.AllowUserToAddRows = False

End Sub

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

Me.dataGridView1.BindingContext(bs).Position = bs.Find("fldID", Me.textBox1.Text)

End Sub

Class

Hope this helps.

Regards

Rong-Chun Zhang  Wednesday, August 01, 2007 11:36 AM

Hi pmak,

To make the datagridview scroll to the page with the found record, you can set DataGridView.FirstDisplayedScrollingRowIndex Property or DataGridView.FirstDisplayedCell Property. However, to find the record in DataGridView, I prefer to use the BindingSource.Find method. See my sample below:

Code Snippet

Class DGVSearchItem

Private dt As New DataTable()

Private bs As New BindingSource()

Private Sub DGVSearchItem_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

dt.Columns.Add("fldID")

dt.Columns.Add("fldName")

For i As Integer = 0 To 49

dt.Rows.Add(i.ToString(), "Name" + i.ToString())

Next

bs.DataSource = dt.DefaultView

Me.DataGridView1.EditMode = DataGridViewEditMode.EditOnF2

Me.DataGridView1.DataSource = bs

Me.DataGridView1.AllowUserToAddRows = False

End Sub

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

Me.dataGridView1.BindingContext(bs).Position = bs.Find("fldID", Me.textBox1.Text)

End Sub

Class

Hope this helps.

Regards

Rong-Chun Zhang  Wednesday, August 01, 2007 11:36 AM

You can use google to search for other answers

Custom Search

More Threads

• Saving Files command
• Stupid ?
• does anyone build windowsform with c++?
• How to determine File Type?
• prefix 'this.'
• MDI Keystrokes
• Fade
• Problem with ToolStripDropDown not closing immediately
• Application.DoEvents and repeating events
• Deploy .NET Framework with application?