高手赐教:DataGridView控件自动伸展
   自动适应父控件的高度或宽度

解决方案 »

  1.   

    修改下面两个属性中的一个即可this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;或者this.dataGridView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                            | System.Windows.Forms.AnchorStyles.Left)
                            | System.Windows.Forms.AnchorStyles.Right)));
      

  2.   

    设置datagridview的dock属性或者anchor属性可以让实现控件随着窗体变化而变化,设置datagridview的autosizecolumnsmode属性为Fill可以实现单元格充满datagridview;
      

  3.   

    楼主,你看这个窗体,上面也有两个文本框,一个datagridview,你看文本框会不会挡住
     partial class Form1
        {
            /// <summary>
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;        /// <summary>
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }        #region Windows 窗体设计器生成的代码        /// <summary>
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.panel1 = new System.Windows.Forms.Panel();
                this.dataGridView1 = new System.Windows.Forms.DataGridView();
                this.textBox1 = new System.Windows.Forms.TextBox();
                this.textBox2 = new System.Windows.Forms.TextBox();
                this.panel1.SuspendLayout();
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
                this.SuspendLayout();
                // 
                // panel1
                // 
                this.panel1.Controls.Add(this.textBox2);
                this.panel1.Controls.Add(this.textBox1);
                this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
                this.panel1.Location = new System.Drawing.Point(0, 0);
                this.panel1.Name = "panel1";
                this.panel1.Size = new System.Drawing.Size(292, 43);
                this.panel1.TabIndex = 0;
                // 
                // dataGridView1
                // 
                this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
                this.dataGridView1.Location = new System.Drawing.Point(0, 43);
                this.dataGridView1.Name = "dataGridView1";
                this.dataGridView1.RowTemplate.Height = 23;
                this.dataGridView1.Size = new System.Drawing.Size(292, 223);
                this.dataGridView1.TabIndex = 1;
                // 
                // textBox1
                // 
                this.textBox1.Location = new System.Drawing.Point(35, 13);
                this.textBox1.Name = "textBox1";
                this.textBox1.Size = new System.Drawing.Size(100, 21);
                this.textBox1.TabIndex = 0;
                // 
                // textBox2
                // 
                this.textBox2.Location = new System.Drawing.Point(167, 13);
                this.textBox2.Name = "textBox2";
                this.textBox2.Size = new System.Drawing.Size(100, 21);
                this.textBox2.TabIndex = 1;
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(292, 266);
                this.Controls.Add(this.dataGridView1);
                this.Controls.Add(this.panel1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.panel1.ResumeLayout(false);
                this.panel1.PerformLayout();
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
                this.ResumeLayout(false);        }        #endregion        private System.Windows.Forms.Panel panel1;
            private System.Windows.Forms.TextBox textBox2;
            private System.Windows.Forms.TextBox textBox1;
            private System.Windows.Forms.DataGridView dataGridView1;
        }
      

  4.   

    首先手动调整好各控件的位置,然后
    设置dataGridView的anchor属性 为Top, Bottom, Left, Right