【问题标题】:Resize DataGridView width to fit Columns调整 DataGridView 宽度以适应列
【发布时间】:2014-05-16 01:50:20
【问题描述】:

我有一个以编程方式填充的DataGridView。列设置为根据单元格内容自动调整大小。

DataGridView 将填充有关液压和气动原理图的零件信息。我的表单只有SplitContainerPictureBoxDataGridViewSplitterDistance 链接到DataGridView 的宽度。

DataGridView 最多只有 6 列 (“索引”、“部件号”、“序列号”、“图号”、“页码”、“修订号”) em> 和至少 2 列,具体取决于原理图要求。所以我想相应地调整控件的大小。

如何让DataGridView 控件调整为列的总宽度,以便不显示滚动条?

【问题讨论】:

  • DataGridView 将填充有关液压和气动原理图的零件信息,我的表单只有一个 SplitContainer、一个 PictureBox 和 DataGridView,SplitterDistance 与 DataGridView 的宽度相关联,DataGridView 将只最多有 6 列:“索引”、“部件号”、“序列号”、“图号”、“页码”、“修订号”,最少 2 列,具体取决于原理图要求。所以我想相应地调整控件的大小
  • DV 与液压完全无关

标签: c# winforms datagridview


【解决方案1】:

在网格加载数据并相应调整列大小后执行以下代码(假设您在运行时设置列的 AutoSize 属性)。

dataGridView1.Width =
    dataGridView1.Columns.Cast<DataGridViewColumn>().Sum(x => x.Width)
    + (dataGridView1.RowHeadersVisible ? dataGridView1.RowHeadersWidth : 0) + 3;

它在做什么:

  • 合计所有列的宽度(使用 LINQ),
  • 查看“行标题”是否可见并添加该宽度,如果是,则
  • 添加 3 更多,因为没有它,水平滚动条会一直显示 - 可能是因为网格周围的边距/填充,我不确定。

【讨论】:

  • 如果需要,可以将+3 替换为dataGridView1.Margin.Horizontal;并且可以使用(new int[] {...).Min(); 来限制Parent.WidthdataGridView1.Maximum.Width 之类的宽度,以解决上述@GrantWinney 的问题
  • 考虑使用隐藏列:Columns.Cast&lt;DataGridViewColumn&gt;().Where(x =&gt; x.Visible).Sum(x =&gt; x.Width) - 这不在原始问题中,但其他人(如我)可能会在这种情况下尝试使用此代码。
  • 您还应该添加 SystemInformation.VerticalScrollBarWidth
【解决方案2】:

好吧,澄清你想要什么(并回答它)

我想你想让 DataGridView 的宽度扩大,这样就不会出现水平滚动条? (顺便说一句,当您使用它时,您也可以增加包含它的表单)

例如,我现在有

那里有第 1、2 和 3 列。

我希望 datagridview 扩展到适合所有列的大小

假设添加了更多数据

我可以做这两行来扩展单元格,

dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells; 
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

但我仍然得到一个水平滚动条,因为 datagridview 的大小没有改变。即使每列的大小都有。

我可以看到有一个 datagridview1.Size 属性和一个 dataGridView1.Width 属性。两者都可以。

还要注意在第一列之前有一个有趣的列。

因此,如果您确实使 dataGridView1.Width 等于 cols 1,2,3 的大小,您仍然会有一个滚动条,因为那个有趣的列就像标记为“column 1”的列左侧的东西.我看到它的宽度为 50 个单位。因此,如果你让 dataGridView1.Width = 50 加上每一列的宽度,那么灰色的 dataGridView 区域将总是足够大以包含所有列。

我画了一个datagridview和一个文本框,文本框显示了datagridview.Width的大小和所有列的总宽度,以及每一列的宽度。

这对我有用。

因此,根据其中的内容量,将列设置为扩大或缩小大小,但不仅如此.. DataGridView.Width 将增加 50(最左边的有趣列),加上所有常规/其余列的大小。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace automaticallyexpanddatagridviewsizeandformsize
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
          //  MessageBox.Show(dataGridView1.Size.Width.ToString());

            // note that with these two set you can't change the width of a column
            // also  MininimumWidth would limit changing the width of a column too, 
            //http://stackoverflow.com/questions/2154154/datagridview-how-to-set-column-width
            // but that doesn't matter because we aren't programmatically changing the width of any column.

            dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells; 
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;




        }



        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {

            int olddgvsize = dataGridView1.Width;

            textBox1.Text = dataGridView1.Columns[0].Width.ToString();
            int h=dataGridView1.Height;

            int tw = 0;
            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
              //  MessageBox.Show(dataGridView1.Columns[i].Width.ToString());
                tw += dataGridView1.Columns[i].Width;

            }
            tw += 50; // column before col 0..

           //you only need one of these.
           //though to better understand the code you can try to comment out both and see how the total width of all columns (tw)(+50) so ALL the columns, differs from  the DataGridView.Width (the total area which includes all columns plus some grey if it's much bigger than all the columns)

            dataGridView1.Size = new Size(tw, h);
            dataGridView1.Width = tw;

                textBox1.Text = "tw=" + tw + " " + "dgvw=" + " " +dataGridView1.Width+ "  "+"col 1:" + dataGridView1.Columns[0].Width + " col 2:"  + dataGridView1.Columns[1].Width + " col 3:"+ dataGridView1.Columns[2].Width;

                int newdgvsize = dataGridView1.Width;
                int differenceinsizeofdgv = newdgvsize - olddgvsize;
                this.Width = this.Width + differenceinsizeofdgv;


        }


    }



}

所以例如我有

tw 是所有列的总宽度(所有列,包括第 1 列左侧的奇怪列,我认为它的宽度为 50,也许是这样)

dgvw 是 dataGridView.Width

感谢上面的代码,dgvw 用 tw 扩展。

上面的代码也将表单扩展了dataGridView扩展的量。

【讨论】:

【解决方案3】:

这里是一个调整datagridview宽度的例子

private void Form1_Load(object sender, EventArgs e)
{
    //Create datagridview and button below it.

    DataGridView dgv = new DataGridView();
    dgv.Columns.Add("Column1","Column1");
    dgv.Rows.Add(2);
    dgv.AllowUserToAddRows = false;
    this.Controls.Add(dgv);

    dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
    dgv.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;

    int tw;

    tw = dgv.Columns.GetColumnsWidth(DataGridViewElementStates.None) + dgv.RowHeadersWidth + 2;
    dgv.Width = tw;

    // the cellleave, is done before the autoresize.
    // but performing a button click, that triggers a cell autoresize!
    //performing a click to a button added to the form..does the autoresize.
    // so the resie has to be done after the click... either in the click code, or in the celleave procedure after the performclick.

    Button by = new Button();
    this.Controls.Add(by); // necessary for the dgv to resize
    // but doesn't need the code to necessarily be within the click.

    by.Click += (object ssender, EventArgs ee) => { };

    dgv.CellEndEdit += (object ssender, DataGridViewCellEventArgs ee) => {
        by.PerformClick();
        tw = dgv.Columns.GetColumnsWidth(DataGridViewElementStates.None) + dgv.RowHeadersWidth + 2;
        dgv.Width = tw;
    };

}

【讨论】:

  • 另一种方法是绘制一个 datagridview 和 2 个按钮和一个文本框。这两个按钮,一个“调整大小”一个“显示宽度”,“显示宽度”将写入文本框 "tw=___ dgvwidth=__" 并且调整大小将调整它的大小。
  • 这个问题中提到的技术resize a datagridview stackoverflow.com/questions/37635932/…
猜你喜欢
  • 2017-02-10
  • 1970-01-01
  • 2017-04-21
  • 1970-01-01
  • 1970-01-01
  • 2011-05-27
  • 1970-01-01
相关资源
最近更新 更多