Posts Tagged 'Winforms'

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