Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

  • 3 weeks later...
Posted
Here's some generic code I keep in my base form that all of my child forms inherit from:
    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;
   }

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...