简体   繁体   English

Windows窗体:如何使窗体和网格更改大小一起?

[英]Windows Forms: How to make the Form and a Grid Change Sizes Together?

In C#, I have a table displayed using a DataGridView . 在C#中,我有一个使用DataGridView显示的表。 The table is significantly smaller than the form in which it appears, and so the table fills only a small portion of the upper-left-hand corner of the form. 该表格明显小于其显示的形式,因此表格仅填充表格左上角的一小部分。

My question: how can I (programmatically) make either: (1) the table automatically enlarge so that it fills the form, or (2) make the form automatically shrink to the size of the table? 我的问题:我怎么能(以编程方式)制作:(1)表格自动放大以填充表格,或(2)使表格自动缩小到表格的大小? (And, are both possible?) (而且,都可能吗?)

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 ( ) ) ;
        }

    }
this.myDataGridView.Dock = DockStyle.Fill;

This will make the DataGrid fill the entire Form 这将使DataGrid填充整个表单

You can also use an Anchor like so. 您也可以像这样使用锚点。

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

This means that the DataGrid will resize when the Form resizes. 这意味着当窗体调整大小时, DataGrid将调整大小。

使用Grid的Dock和Anchor属性播放:)此外,您可能希望尝试使用表单的AutoSize属性来查看它是否按照您的意愿运行。

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

I have yet to meet a problem like this that I couldn't solve with the Anchor property. 我还没有遇到这样的问题,我无法用Anchor属性解决。 For me, Dock seems much harder to use. 对我来说, Dock似乎更难使用。 I guess if there are no other controls on the form, DockStyle.Fill is OK. 我想如果表单上没有其他控件, DockStyle.Fill就可以了。 Most forms tend to have other controls on them so, anchoring to all four sides is much easier. 大多数形式往往对它们有其他控制因此,锚定到所有四个方面都更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM