Datagrids on tab pages resizing bug

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: wjousts (wjousts_at_discussions.microsoft.com)
Date: 03/04/05


Date: Fri, 4 Mar 2005 10:23:02 -0800

Hi,

I've noticed a problem with datagrids when they are on tab pages. For
example, if you create a windows application, add a tab control (set to dock
fill) to the main form, then add two tab pages to the tab control. On each
tab page add a datagrid with it's dock set to fill. Give the datagrids some
data to display and run the program.
When the program starts reduce the size of the form until the on the first
tab page is forced to display scroll bars. Now switch to the other tab page,
it also has scroll bars and everything is fine. Now expand the form again and
the current datagrid should size with the form, but now if you switch back to
the first tab page the datagrid has resized correctly but the scroll bars are
still stuck in the old position right across your datagrid!
Has anybody else seen this problem and know of a work around?

Thanks,

WJ

Here's some code that reproduces the problem:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Datagrid_bug
{
        /// <summary>
        /// Summary description for Form1.
        /// </summary>
        public class Form1 : System.Windows.Forms.Form
        {
                private System.Windows.Forms.TabControl tabControl1;
                private System.Windows.Forms.TabPage tabPage1;
                private System.Windows.Forms.TabPage tabPage2;
                private System.Windows.Forms.DataGrid dataGrid1;
                private System.Windows.Forms.DataGrid dataGrid2;
                private System.Data.DataSet dataSet1;
                private System.Data.DataTable dataTable1;
                private System.Data.DataColumn dataColumn1;
                private System.Data.DataColumn dataColumn2;
                /// <summary>
                /// Required designer variable.
                /// </summary>
                private System.ComponentModel.Container components = null;

                public Form1()
                {
                        //
                        // Required for Windows Form Designer support
                        //
                        InitializeComponent();

                        for (int i=0; i<50; i++)
                        {
                                DataRow dr = dataTable1.NewRow();
                                dr[dataColumn1] = i;
                                dr[dataColumn2] = i*i;
                                dataTable1.Rows.Add(dr);
                        }
                }

                /// <summary>
                /// Clean up any resources being used.
                /// </summary>
                protected override void Dispose( bool disposing )
                {
                        if( disposing )
                        {
                                if (components != null)
                                {
                                        components.Dispose();
                                }
                        }
                        base.Dispose( disposing );
                }

                #region Windows Form Designer generated code
                /// <summary>
                /// Required method for Designer support - do not modify
                /// the contents of this method with the code editor.
                /// </summary>
                private void InitializeComponent()
                {
                        this.tabControl1 = new System.Windows.Forms.TabControl();
                        this.tabPage1 = new System.Windows.Forms.TabPage();
                        this.tabPage2 = new System.Windows.Forms.TabPage();
                        this.dataGrid1 = new System.Windows.Forms.DataGrid();
                        this.dataGrid2 = new System.Windows.Forms.DataGrid();
                        this.dataSet1 = new System.Data.DataSet();
                        this.dataTable1 = new System.Data.DataTable();
                        this.dataColumn1 = new System.Data.DataColumn();
                        this.dataColumn2 = new System.Data.DataColumn();
                        this.tabControl1.SuspendLayout();
                        this.tabPage1.SuspendLayout();
                        this.tabPage2.SuspendLayout();
                        ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
                        ((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).BeginInit();
                        ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit();
                        ((System.ComponentModel.ISupportInitialize)(this.dataTable1)).BeginInit();
                        this.SuspendLayout();
                        //
                        // tabControl1
                        //
                        this.tabControl1.Controls.Add(this.tabPage1);
                        this.tabControl1.Controls.Add(this.tabPage2);
                        this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
                        this.tabControl1.Location = new System.Drawing.Point(0, 0);
                        this.tabControl1.Name = "tabControl1";
                        this.tabControl1.SelectedIndex = 0;
                        this.tabControl1.Size = new System.Drawing.Size(504, 342);
                        this.tabControl1.TabIndex = 0;
                        //
                        // tabPage1
                        //
                        this.tabPage1.Controls.Add(this.dataGrid1);
                        this.tabPage1.Location = new System.Drawing.Point(4, 22);
                        this.tabPage1.Name = "tabPage1";
                        this.tabPage1.Size = new System.Drawing.Size(496, 316);
                        this.tabPage1.TabIndex = 0;
                        this.tabPage1.Text = "tabPage1";
                        //
                        // tabPage2
                        //
                        this.tabPage2.Controls.Add(this.dataGrid2);
                        this.tabPage2.Location = new System.Drawing.Point(4, 22);
                        this.tabPage2.Name = "tabPage2";
                        this.tabPage2.Size = new System.Drawing.Size(496, 316);
                        this.tabPage2.TabIndex = 1;
                        this.tabPage2.Text = "tabPage2";
                        //
                        // dataGrid1
                        //
                        this.dataGrid1.DataMember = "";
                        this.dataGrid1.DataSource = this.dataTable1;
                        this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
                        this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
                        this.dataGrid1.Location = new System.Drawing.Point(0, 0);
                        this.dataGrid1.Name = "dataGrid1";
                        this.dataGrid1.Size = new System.Drawing.Size(496, 316);
                        this.dataGrid1.TabIndex = 0;
                        //
                        // dataGrid2
                        //
                        this.dataGrid2.DataMember = "";
                        this.dataGrid2.DataSource = this.dataTable1;
                        this.dataGrid2.Dock = System.Windows.Forms.DockStyle.Fill;
                        this.dataGrid2.HeaderForeColor = System.Drawing.SystemColors.ControlText;
                        this.dataGrid2.Location = new System.Drawing.Point(0, 0);
                        this.dataGrid2.Name = "dataGrid2";
                        this.dataGrid2.Size = new System.Drawing.Size(496, 316);
                        this.dataGrid2.TabIndex = 0;
                        //
                        // dataSet1
                        //
                        this.dataSet1.DataSetName = "NewDataSet";
                        this.dataSet1.Locale = new System.Globalization.CultureInfo("en-US");
                        this.dataSet1.Tables.AddRange(new System.Data.DataTable[] {
                                                                                                                                                  this.dataTable1});
                        //
                        // dataTable1
                        //
                        this.dataTable1.Columns.AddRange(new System.Data.DataColumn[] {
                                                                                                                                                          this.dataColumn1,
                                                                                                                                                          this.dataColumn2});
                        this.dataTable1.TableName = "Table1";
                        //
                        // dataColumn1
                        //
                        this.dataColumn1.ColumnName = "Column1";
                        //
                        // dataColumn2
                        //
                        this.dataColumn2.ColumnName = "Column2";
                        //
                        // Form1
                        //
                        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                        this.ClientSize = new System.Drawing.Size(504, 342);
                        this.Controls.Add(this.tabControl1);
                        this.Name = "Form1";
                        this.Text = "Form1";
                        this.tabControl1.ResumeLayout(false);
                        this.tabPage1.ResumeLayout(false);
                        this.tabPage2.ResumeLayout(false);
                        ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
                        ((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).EndInit();
                        ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit();
                        ((System.ComponentModel.ISupportInitialize)(this.dataTable1)).EndInit();
                        this.ResumeLayout(false);

                }
                #endregion

                /// <summary>
                /// The main entry point for the application.
                /// </summary>
                [STAThread]
                static void Main()
                {
                        Application.Run(new Form1());
                }
        }
}



Relevant Pages

  • Re: TextBox and PictureBox in Single Column of Windows For DataGrid + C#
    ... Maybe this FAQ will provide a solution to the tab skipping over your column. ... > I am dot designing new TextBox control, I just designing DataGrid Column ... >> public int FormLeft ... >> private void LeaveTextBox ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: How to get value among subclasses
    ... this is possible by using a static variable in the super class. ... tab object, each datagrid sits on a tab, a total of four datagrids/ ... Every time when a row on a datagrid is clicked, ... customer on the other three datagrids/tabs. ...
    (comp.lang.java.programmer)
  • Re: Tab Stops in DataGrid
    ... One way to do TAB processing is to create a custom DataGrid that derives ... from "System.Windows.Forms.DataGrid" and override the "ProcessCmdKey" ...
    (microsoft.public.dotnet.framework.windowsforms)
  • How do I make my data grid, row selectable rather than cell
    ... I have a Datagrid on the screen that I can tab into at the relevant point in ... my tab cycle. ... datagrid it highlights a cell not a row. ... but then when you navigate with the arrows it just ...
    (microsoft.public.dotnet.general)
  • Re: TAB (key) to next control instead of next column (in datagrid)
    ... "SelectNextControl" you specify that you desire it to be fired on TAB as one ... You also specify which control to go to next as the first ... > User writes something in textbox - then hits TAB to go to datagrid... ...
    (microsoft.public.dotnet.languages.vb)