Link to home
Start Free TrialLog in
Avatar of curiouswebster
curiouswebsterFlag for United States of America

asked on

Scrolling down on a DataGridView hides the last item in the list

Scrolling down on a DataGridView hides the last item in the list
 
When I move an item to the end of the list, and the list is a little longer than the visible area, the last item is drawn off the visble area and is hidden.

Does anyone know how to automaticaly scroll the window to prevent this?

Thanks,
newbieweb
ASKER CERTIFIED SOLUTION
Avatar of systan
systan
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of curiouswebster

ASKER

I will check into the sizing of the DataGridView and do more testing.  Thanks.
I asked for the question to be re-opened.

Attached is the code that does the re-ordering of the list.  This is a list of drivers in the DataGridView.  When I move the top driver down, it scrolls down perfectly.  But when it gets below the last visible driver, it's no longer visible.  When I move the scroll bar down, I see the driver made it to the bottom of the list and is hightlighted.  Once again, perfect.  But I want the scroll bar to sense that the selected driver is no longer visible, then scroll to keep it in view.
        private void ChangePosition(int rowIndex, bool moveUp)
        {
            try
            {
                Driver driverBeingMovedUp = null;
                Driver driverBeingMovedDown = null;
                if (dgvDrivers.Rows.Count > 1) // must have at least two drivers to change the driver sequence
                {
                    DataGridViewRow dgvrBeingMoved = dgvDrivers.Rows[rowIndex];
                    if (moveUp) 
                    {
                        if (rowIndex > 0)
                        {
                            string driverLabelBeingMovedUp = (string)dgvDrivers.Rows[rowIndex].Cells[0].Value;
                            string driverLabelBeingMovedDown = (string)dgvDrivers.Rows[rowIndex - 1].Cells[0].Value;
                            driverBeingMovedUp = handles.DriverList.GetDriverByLabel(driverLabelBeingMovedUp);
                            driverBeingMovedDown = handles.DriverList.GetDriverByLabel(driverLabelBeingMovedDown);
                            if (driverBeingMovedUp != null && driverBeingMovedUp.IsIdle() && driverBeingMovedDown != null && driverBeingMovedDown.IsIdle())
                            {
                                dgvDrivers.Rows.RemoveAt(rowIndex);
                                dgvDrivers.Rows.Insert(rowIndex - 1, dgvrBeingMoved);
                                DataGridViewRow row = dgvDrivers.Rows[rowIndex - 1];
                                row.Selected = true;
                            }
                        }
                    }
                    else
                        if (rowIndex < dgvDrivers.Rows.Count - 1)
                        {
                            string driverLabelBeingMovedUp = (string)dgvDrivers.Rows[rowIndex + 1].Cells[0].Value;
                            string driverLabelBeingMovedDown = (string)dgvDrivers.Rows[rowIndex].Cells[0].Value;
                            driverBeingMovedUp = handles.DriverList.GetDriverByLabel(driverLabelBeingMovedUp);
                            driverBeingMovedDown = handles.DriverList.GetDriverByLabel(driverLabelBeingMovedDown);
                            if (driverBeingMovedUp != null && driverBeingMovedUp.IsIdle() && driverBeingMovedDown != null && driverBeingMovedDown.IsIdle())
                            {
                                dgvDrivers.Rows.RemoveAt(rowIndex);
                                dgvDrivers.Rows.Insert(rowIndex + 1, dgvrBeingMoved);
                                DataGridViewRow row = dgvDrivers.Rows[rowIndex + 1];
                                row.Selected = true;
                            }
                        }
                }
                if (driverBeingMovedDown != null && driverBeingMovedUp != null && (!driverBeingMovedDown.IsIdle() || !driverBeingMovedUp.IsIdle()))
                {
                    MessageBox.Show("Only drivers in house can be re-ordered", handles.Preferences.ProductName);
                }
            }
            catch (Exception ex)
            {
                handles.LogException("OrderStatus.ChangePosition", ex, null);
            }
        }

Open in new window

I have discovered that the "Displayed" property can let my program know if further action is needed to make the driver visible.

                                if (row.Displayed)
                                {
                                            // scroll the scroll bar 1 tick
                                }