470,566 Members | 1,494 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,566 developers. It's quick & easy.

C# DataGridView & Scrollbar position setting problem

I'll start off with what i'm trying to do:

i want to search my data programmatically and then jump to the row in the datagrid.
My data will almost allways go out of the datagrids bounds so if the found result is not on screen at the time i want to 'scroll' to the result.

sounds simple enough right?


Some things i've tried to get the datagrid to do what i want:

-setting the autoscrollposition property, i've tried setting it after finding the result and tried setting it in the paint loop (which was an advice found while looking for an awnser on the net)

neither had any visible effect, the property changed but didn't show in the controll after invalidation & painting. so i discarded this avenue as the incorrect appoach


-Getting the VScrollBar from the datagridview.controls list and then setting its value directly to the position on which i want it to be.

now this actually had some effect: the scrollbars handle appeared on the correct position, but the datagrid did not scroll along. now if i touch/drag the handle of the scrollbar then it does update the position and i'm near my result (depending on howfar i dragged the handle)
so the problem here seems to be that the datagrid doesn't seem to pick up the change until i actually use a physical mouseinput on the scrollbar

i'm currently trying to invoke the scrollbars mouseclick/drag whatever but so far i havn't made any process in that area yet.


Now my question is:
-does anybody have any idea of how to get the datagrid to pick up the change, or does anybody have another method of setting the scrolloffset which actually shows.

basically i'm running into walls when i'm looking through the internet/msdn and i'm running out of ideas so any help would be welcome.
Jul 31 '07 #1
4 9594
Plater
7,872 Expert 4TB
I always accomplished it by setting the selectedrows property to the one I wanted.
Jul 31 '07 #2
I always accomplished it by setting the selectedrows property to the one I wanted.
that was 1 of the things that came to mind, when i was kicking against this problem heres how i do my selection atm:

Expand|Select|Wrap|Line Numbers
  1.  
  2.             foreach (DataGridViewRow r in dataGrid.Rows)
  3.             {
  4.                 if (((String)r.Cells[col].Value).Equals(text))
  5.                 {
  6.                     r.Selected = true;
  7. //rest of the code
  8. }
  9. }
which highlights the selection but doesn't seem to scrolldown to the first highlighted entry

after reading your post i tried to simply set the datagrid.SelectedRows list but thats read only, so would u mind posting a quick example of how you do it?

btw i forgot to mention i'm using .Net 2.0
Jul 31 '07 #3
Plater
7,872 Expert 4TB
Well I take that back, I double-checked and I use the CurrentCell property
Expand|Select|Wrap|Line Numbers
  1. //dgvParts is my dataGridView
  2. //setting currentcell would push the focus to that cell, but I have "select whole row" enabled so it selects the row
  3. private void SelectPartIDMatching(string PartID)
  4.         {
  5.             int myidx = -1;
  6.             for (int i = 0; i < dgvParts.Rows.Count; i++)
  7.             {
  8.                 if (dgvParts["PartID", i].Value.ToString() == PartID)
  9.                 {
  10.                     myidx = i;
  11.                     break;
  12.                 }
  13.             }
  14.             if (myidx != -1)
  15.             {
  16.                 dgvParts.CurrentCell = dgvParts.Rows[myidx].Cells[0];
  17.             }
  18.         }
  19.  
Jul 31 '07 #4
Well I take that back, I double-checked and I use the CurrentCell property
Expand|Select|Wrap|Line Numbers
  1. //dgvParts is my dataGridView
  2. //setting currentcell would push the focus to that cell, but I have "select whole row" enabled so it selects the row
  3. private void SelectPartIDMatching(string PartID)
  4.         {
  5.             int myidx = -1;
  6.             for (int i = 0; i < dgvParts.Rows.Count; i++)
  7.             {
  8.                 if (dgvParts["PartID", i].Value.ToString() == PartID)
  9.                 {
  10.                     myidx = i;
  11.                     break;
  12.                 }
  13.             }
  14.             if (myidx != -1)
  15.             {
  16.                 dgvParts.CurrentCell = dgvParts.Rows[myidx].Cells[0];
  17.             }
  18.         }
  19.  
That works just fine! Thanks a lot now i can finally stop wrestling with this issue
Jul 31 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

24 posts views Thread by Jeff Johnson | last post: by
2 posts views Thread by carmen | last post: by
6 posts views Thread by Colin McGuire | last post: by
1 post views Thread by orofiamma | last post: by
reply views Thread by =?Utf-8?B?Q2hyaXN0aWFuIEhhdmVs?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.