We help IT Professionals succeed at work.

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

curiouswebster
curiouswebster asked
on
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
Comment
Watch Question

Commented:
I hope you hadn't mess configure datagridview in properties; because if you had;
Try to rename your datagridview to another name(temporary only) then add new datagridview with the same original name,  then try to run it without resizing,  if works Ok,   resize it,   it resizing works Ok too,  then compare there propeties in your old datagridview and the new one, so you will know where it has been mess config.
please share your code which you are using to display datagridview
curiouswebsterSoftware Engineer

Author

Commented:
I will check into the sizing of the DataGridView and do more testing.  Thanks.
curiouswebsterSoftware Engineer

Author

Commented:
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

curiouswebsterSoftware Engineer

Author

Commented:
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
                                }