【问题标题】:Windows Forms: How to make the Form and a Grid Change Sizes Together?Windows 窗体:如何使窗体和网格一起更改大小?
【发布时间】:2009-07-17 16:34:00
【问题描述】:

在 C# 中,我有一个使用 DataGridView 显示的表格。该表格明显小于其所在的表格,因此表格只占表格左上角的一小部分。

我的问题:我如何(以编程方式)制作:(1)表格自动放大以填充表格,或(2)使表格自动缩小到表格的大小? (而且,两者都有可能吗?)

using System ;
using System.Windows.Forms ;
using System.Data ;

public class NiftyForm : System.Windows.Forms.Form
    {
    private     DataGridView        myDataGridView ;
    private     System.Data.DataTable   myDataTable ;

    public NiftyForm ( )
        {
        this.Load  +=  new EventHandler ( NiftyFormLoadEventHandler ) ;
        }

    private void NiftyFormLoadEventHandler ( System.Object sender,
                                             System.EventArgs ea )
        {
        this.Location  =  new System.Drawing.Point ( 40, 30 ) ;
        this.Size      =  new System.Drawing.Size ( 800, 600 ) ;

        myDataTable  =  new DataTable ( ) ;
        DataColumn  myDataColumn  =  new DataColumn ( ) ;
        myDataColumn.DataType           =   typeof(string) ;
        myDataColumn.ColumnName         =   "Name";
        myDataColumn.ReadOnly           =   true;
        myDataTable.Columns.Add ( myDataColumn ) ;

        myDataColumn    =  new DataColumn ( ) ;
        myDataColumn.DataType   =   typeof(int) ;
        myDataColumn.ColumnName =   "Age";
        myDataColumn.ReadOnly   =   true;
        myDataTable.Columns.Add ( myDataColumn ) ;

        string [ ]  Name  =  new string [ 5 ]
                             { "Dwight", "Abe", "Cal", "Bill", "Eisenhower" } ;
        int    [ ]  Age   =  new int    [ 5 ] { 123, 45, 6, 78, 9 } ;
        for ( int i = 0 ; i < 5 ; i ++ )
            {
            DataRow     myDataRow  =  myDataTable.NewRow ( ) ;
            myDataRow [ "Name" ]    =   Name [ i ] ;
            myDataRow [ "Age" ] =   Age  [ i ] ;
            myDataTable.Rows.Add ( myDataRow ) ;
            }

        this.myDataGridView             =   new DataGridView ( ) ;
        this.myDataGridView.DataSource      =   myDataTable ;
        this.myDataGridView.Dock        =   DockStyle.Fill ;
        this.Controls.Add ( this.myDataGridView ) ;
        }

    [ STAThreadAttribute ( ) ]

    static void Main ( )
        {
        Application.Run ( new NiftyForm ( ) ) ;
        }

    }

【问题讨论】:

    标签: c# winforms datagridview datatable


    【解决方案1】:
    this.myDataGridView.Dock = DockStyle.Fill;
    

    这将使 DataGrid 填满整个 Form

    你也可以像这样使用锚。

    this.myDataGridView.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
    

    这意味着 DataGrid 将在 Form 调整大小时调整大小。

    【讨论】:

    • 您的答案中缺少 AnchorStyles.Bottom。
    • 那是因为我没有将它锚定到表单的底部,只是展示了如何锚定以选择表单的一部分:)
    【解决方案2】:

    使用网格的 Dock 和 Anchor 属性 :) 此外,您可能想尝试表单的 AutoSize 属性,看看它是否可以按您的意愿工作。

    【讨论】:

      【解决方案3】:

      设置表单 AutoSize=True 和 AutoSizeMode=GrowAndShrink,您的表单将调整为网格大小。

      【讨论】:

        【解决方案4】:

        我还没有遇到过使用Anchor 属性无法解决的此类问题。对我来说,Dock 似乎更难使用。我猜如果表单上没有其他控件,DockStyle.Fill 就可以了。大多数表单往往都有其他控件,因此,锚定到所有四个边要容易得多。

        【讨论】:

          猜你喜欢
          • 2016-10-29
          • 1970-01-01
          • 2010-11-12
          • 1970-01-01
          • 1970-01-01
          • 2012-05-11
          • 2011-12-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多