I use the following code to find a row in DataGridView and highlight the row.
private void btnSearch_Click(object sender, EventArgs e)
{
currentMode = ModeSelection.Search;
if (cmbSearchBy.SelectedIndex == Convert.ToInt16(SearchBy.MaterialID))
{
dgvSearchResults.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
int rowIndex = -1;
try
{
foreach (DataGridViewRow row in dgvSearchResults.Rows)
{
if (row.Cells[1].Value.ToString().Equals(materialLocation.MaterialID))
{
//Select the row here
rowIndex = row.Index;
dgvSearchResults.Rows[rowIndex].Selected = true;
break;
}
}
}
catch (Exception ex) { throw ex; }
}
It works perfectly. Problem is my DataGridView has over 500 records and if the selected row is near the bottom of the DataGridView, users have to scroll all the way down to the bottom. Which code can I use to jump to the row that I am looking for? Any help will be very much appreciated!
I found out that I can use DataGridView.FirstDisplayedScrollingRowIndex Property to scroll to the selected row index and display it as the first row in DataGridView.
This is how I used in my program-
if (row.Cells[1].Value.ToString().Equals(materialLocation.MaterialID))
{
rowIndex = row.Index;
dgvSearchResults.ClearSelection();
dgvSearchResults.Rows[rowIndex].Selected = true;
dgvSearchResults.FirstDisplayedScrollingRowIndex = rowIndex;
dgvSearchResults.Focus();
break;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With