Datagrid View

Posted on 2005-04-29
Last Modified: 2010-04-16
Is there anyway to get the index of the first row that is show within a datagrid without actually having the row selected.  For example, scrolling a datagrid, but not selecting anything.  I would like the index of the first visible row.  How can that be done????
Question by:gvector1
    LVL 7

    Expert Comment

    I don't believe you can do this from the server. It is not part of what is sent back from the client.
    You would have to do this on the client side and it would probably be more of a pain than it is worth.


    Author Comment

    This is not a server-client architecture.
    LVL 12

    Expert Comment


    One way to get the row number of the first visible row is using HitTest method of the datagrid. What you should take care of is giving the correct location of the first cell (this is relative to the datagrid location). For example, in the below method call, x and y can have both value 1 if RowHeadersVisible property of the datagrid is false and CaptionVisible and ColumnsHeaderVisible are also set to false.

    DataGrid.HitTestInfo hi = this.dataGrid1.HitTest(x,y);

    If not, you have to calculate the correct parameters
    x = this.dataGrid1.RowHeaderWidth + 1.
    For the value of 'y' the things are a little more difficult because the datagrid caption can use different font size and, second,  a tablestyle in TableStyles collection can hide it's column headers even though ColumnsHeaderVisible property for the datagrid is 'true'


    Author Comment

    I could not get that approach to work, but I did run across an excellent alternative approach to this problem that works like a charm.  Here is the sample code that was posted and the url to the origional post :

    using System.Reflection;

    private void button1_Click(object sender, System.EventArgs e)
      FieldInfo fi = typeof(DataGrid).GetField("firstVisibleRow",BindingFlags.NonPublic | BindingFlags.Instance);

    It works like a charm.  I will leave this post open for a few more days for any replies or attempted alternative methods.  If any other methods are found and I can get them to work, I will award the points accordingly.  I like to know of as many approaches as I can.

    Thanks for the assistance,
    LVL 12

    Accepted Solution


    Your solution is more elegant, I wouldn't even look for another method since the DataGrid class has a private property 'firstVisibleRow' and you can get it with reflection.
    Just for info, the same result (reflection again) can be obtain using InvokeMember method of Type class:

       MessageBox.Show(typeof(DataGrid).InvokeMember("firstVisibleRow",BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField, null,dataGrid1,null).ToString());


    Author Comment

    Thanks, I will award the points to you being that you showed me a variation of my approach.


    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now