Scrolling to a given row in WinForms DataGridView

We needed to feature to take the user directly to the row which they search by an ID. Even though we have the Row.Selected property for the DataGridView it selected to the row but did not scroll to it. So I used a little hack to scroll to the DataGridView:

I wrote a loop to get the specific data the user is searching for in the grid (dgvDisabilityInformation) and I used the underlying binding source (bsDisability in my case) to set the current position as given below.

for (int iCounter = 0; iCounter < dgvDisabilityInformation.Rows.Count; iCounter++) { objRow = dgvDisabilityInformation.Rows[iCounter]; objCell = objRow.Cells[0]; if (objCell.Value.ToString() == txtGotoRowID.Text.Trim()) { objRow.Selected = true; bsDisability.CurrencyManager.Position = objRow.Index; Found = true; break; } } [/sourcecode] Works like a charm 🙂 Hope this helps. Happy Programming!!! Cheers, Raja

Advertisements

0 Responses to “Scrolling to a given row in WinForms DataGridView”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s





%d bloggers like this: