[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

Datagrid View

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????
0
gvector1
Asked:
gvector1
  • 3
  • 2
1 Solution
 
jj819430Commented:
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.

Sorry,
0
 
gvector1Author Commented:
This is not a server-client architecture.
0
 
sumixCommented:

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);
MessageBox.Show(hi.Row.ToString());


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'

Regards,
Sumix
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
gvector1Author Commented:
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 :
http://www.windowsforms.net/Forums/ShowPost.aspx?tabIndex=1&tabId=41&PostID=6339

using System.Reflection;
...

private void button1_Click(object sender, System.EventArgs e)
{
  FieldInfo fi = typeof(DataGrid).GetField("firstVisibleRow",BindingFlags.NonPublic | BindingFlags.Instance);
  MessageBox.Show(fi.GetValue(dataGrid1).ToString());
}


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,
Kendal
0
 
sumixCommented:

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());

Regards
0
 
gvector1Author Commented:
Thanks, I will award the points to you being that you showed me a variation of my approach.

Thanks,
Kendal
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now