locked
Datagrid scrollbars madness RRS feed

  • Question

  • Hello everybody,

    I'm working with c# on a Windows CE device with Compact Framework 3.5.

    I've got a problem with a DataGrid component on my GUI. This DataGrid is updated every 2 or 4 seconds automatically by an external device. Until here OK.

    The problem is that the data shown on the grid is bigger than the component size and each time I try to see the data on the bottom of the table by scrolling down the vertical and horizontal scrollbars, the DataGrid is refreshed and the focus point goes again to the top left of the table, so there's no time to look at anything.

    Here comes the code of how do I populate the DataGrid:

    // Get column widths 
    int[] width = ChannelColumnAutoWidth(dev);
    
     // Set styles
    DataGridTableStyle ts = new DataGridTableStyle();
    DataGridColumnStyle csMeta = new DataGridTextBoxColumn(); csMeta.MappingName = "Meta";
    csMeta.HeaderText = "Meta";
    csMeta.Width = width[0]*7;
    DataGridColumnStyle csName = new DataGridTextBoxColumn();
    csName.MappingName = "Name";
    csName.HeaderText = "Name";
    csName.Width = width[1] * 7;
    DataGridColumnStyle csUnit = new DataGridTextBoxColumn();
    csUnit.MappingName = "Unit";
    csUnit.HeaderText = "Unit";
    csUnit.Width = width[2] * 7;
    DataGridColumnStyle csValue = new DataGridTextBoxColumn();
    csValue.MappingName = "Value";
    csValue.HeaderText = "Value";
    csValue.Width = width[3] * 7;
    
    // Bind GridColumnStyles to DataGridStyle
    ts.GridColumnStyles.Add(csMeta);
    ts.GridColumnStyles.Add(csName);
    ts.GridColumnStyles.Add(csUnit);
    ts.GridColumnStyles.Add(csValue);
    
    // Bind Channels List to the TableStyle
    ts.MappingName = dev.Channels.GetType().Name;
    
    // Bind data to DataGrid
    dataGrid.TableStyles.Clear();
    dataGrid.TableStyles.Add(ts);
    

    Each time the DataGrid is updated with this code, the scrollbars of the component go top left, and I want them to stay in the same position as before the DataGrid was refreshed.

    Any idea how to do this?

     

    Friday, October 22, 2010 10:05 AM

Answers

  • You consider updating data in the data source (e.g. keep unchanged records as is), do not recreate everything each time. The code you have should be executed one time while you setting it all up.


    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by Marc Jordana Monday, October 25, 2010 12:32 PM
    Friday, October 22, 2010 5:48 PM

All replies

  • You consider updating data in the data source (e.g. keep unchanged records as is), do not recreate everything each time. The code you have should be executed one time while you setting it all up.


    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by Marc Jordana Monday, October 25, 2010 12:32 PM
    Friday, October 22, 2010 5:48 PM
  • I partially solved the problem using the property Controls of the DataGrid. That's a list that contains the controls binded to the DataGrid.

    Like this:

    ScrollBar vScrollBar = null;
    ScrollBar hScrollBar = null;
    int vOffset = 0;
    int hOffset = 0;
    
    // If there are 2 ScrollBars...
    if (dataGrid.Controls != null)
    {
      if (dataGrid.Controls.Count >= 2)
      {
        // Get ScrollBars and it's values
        vScrollBar = (ScrollBar)dataGrid.Controls[1];
        hScrollBar = (ScrollBar)dataGrid.Controls[0];
        vOffset = vScrollBar.Value;
        hOffset = hScrollBar.Value;
      }
    }
    
    /*** OLD CODE ***/
    
    if (dataGrid.Controls.Count >= 2)
    {
      if ((vScrollBar != null) && (hScrollBar != null))
      {
         // Reset scroll bar values
         vScrollBar.Value = vOffset;
         hScrollBar.Value = hOffset;
      }
    }
    
    Works, but Ilya's way is more efficient.

    Monday, October 25, 2010 12:40 PM