DataGridView Question

usvpn

Freshman
Joined
Apr 19, 2010
Messages
45
Hi,
I want to scroll to the first selected row in my DataGridView.
You know I programmatically select a number of rows, and then want to scroll to the 1st selected row.
I heard about FirstDisplayedScrollingRowIndex property but can't figure it.
Anyone knows how to find the 1st selected row? and then scroll to it?
Thank you :p
 
Here's some generic code I keep in my base form that all of my child forms inherit from:
Code:
    public static int GetFirstScrollingRowIndex(DataGridView dgv, string cellHeader, string cellText) {
      try {
        if ((dgv != null) && !String.IsNullOrEmpty(cellHeader) && !String.IsNullOrEmpty(cellText)) {
          if (dgv.Columns.Contains(cellHeader)) {
            for (int rowIndex = 0; rowIndex < dgv.Rows.Count; rowIndex++) {
              string dgvValue = dgv.Rows[rowIndex].Cells[cellHeader].Value.ToString();
              if (cellText == dgvValue) {
                dgv.Rows[rowIndex].Selected = true;
                return rowIndex;
              }
            }
          }
        }
      } catch (Exception err) {
        Global.LogError(_CODEFILE + "DataGridView_", err);
      }
      return 0;
    }
 
Back
Top