C# DataGridView & Scrollbar position setting problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Blackbrand
    New Member
    • Jul 2007
    • 3

    C# DataGridView & Scrollbar position setting problem

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

    i want to search my data programmaticall y 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 autoscrollposit ion 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.co ntrols 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.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    I always accomplished it by setting the selectedrows property to the one I wanted.

    Comment

    • Blackbrand
      New Member
      • Jul 2007
      • 3

      #3
      Originally posted by Plater
      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:

      Code:
                  foreach (DataGridViewRow r in dataGrid.Rows)
                  {
                      if (((String)r.Cells[col].Value).Equals(text))
                      {
                          r.Selected = true;
      //rest of the code
      }
      }
      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.Select edRows 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

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Well I take that back, I double-checked and I use the CurrentCell property
        Code:
        //dgvParts is my dataGridView
        //setting currentcell would push the focus to that cell, but I have "select whole row" enabled so it selects the row
        private void SelectPartIDMatching(string PartID)
                {
                    int myidx = -1;
                    for (int i = 0; i < dgvParts.Rows.Count; i++)
                    {
                        if (dgvParts["PartID", i].Value.ToString() == PartID)
                        {
                            myidx = i;
                            break;
                        }
                    }
                    if (myidx != -1)
                    {
                        dgvParts.CurrentCell = dgvParts.Rows[myidx].Cells[0];
                    }
                }

        Comment

        • Blackbrand
          New Member
          • Jul 2007
          • 3

          #5
          Originally posted by Plater
          Well I take that back, I double-checked and I use the CurrentCell property
          Code:
          //dgvParts is my dataGridView
          //setting currentcell would push the focus to that cell, but I have "select whole row" enabled so it selects the row
          private void SelectPartIDMatching(string PartID)
                  {
                      int myidx = -1;
                      for (int i = 0; i < dgvParts.Rows.Count; i++)
                      {
                          if (dgvParts["PartID", i].Value.ToString() == PartID)
                          {
                              myidx = i;
                              break;
                          }
                      }
                      if (myidx != -1)
                      {
                          dgvParts.CurrentCell = dgvParts.Rows[myidx].Cells[0];
                      }
                  }
          That works just fine! Thanks a lot now i can finally stop wrestling with this issue

          Comment

          Working...