【问题标题】:Setting the size of dataGrids that having been generated on new tabs设置已在新选项卡上生成的 dataGrids 的大小
【发布时间】:2014-05-19 22:06:25
【问题描述】:

选择 excel 文件时。我为每个 Excel 工作表名称生成了新选项卡。在每个选项卡上,我还生成了一个新的 dataGridView。如何设置自动设置的新 dataGridView 的尺寸,而无需在设计视图中物理单击并拖动它们?此代码提供从 excel 文件中获取我的工作表名称,使用这些名称创建我的新选项卡,并为每个选项卡创建 dataGridViews...只需要 dgv 的大小更大。

foreach (DataRow row in dt1.Rows)
        {
            string name = row["TABLE_NAME"].ToString();
            var tabPage = new TabPage(name);
            DataGridView grid = new DataGridView();

            tabPage.Controls.Add(grid);
            comboBox1.Items.Add(name);
            tabControl2.TabPages.Add(tabPage);
        }

【问题讨论】:

    标签: c# wpf excel datagridview datagrid


    【解决方案1】:

    如果只想让DataGridView占用所有可用空间,可以用DockStyle.Fill初始化DataGridView的Dock属性:

    foreach (DataRow row in dt1.Rows)
    {
        string name = row["TABLE_NAME"].ToString();
        var tabPage = new TabPage(name);
        DataGridView grid = new DataGridView {
            Dock = DockStyle.Fill
        };
    
        tabPage.Controls.Add(grid);
        comboBox1.Items.Add(name);
        tabControl2.TabPages.Add(tabPage);
    }
    

    设置 DockStyle.Fill 可确保 DataGridView 的边缘停靠到其包含控件的边缘并调整大小。

    您也可以选择自己设置位置和大小:

    grid.Location = new Point(500, 500);
    gird.Size = new Size(500, 500);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多