【问题标题】:get actual column width获取实际列宽
【发布时间】:2014-03-21 20:50:10
【问题描述】:

我正在以编程方式构建一个 DataGridView 表并将其放置在表单上的 GroupBox 中。

我插入数据,然后自动调整列大小以适应数据。然后我想将 groupbox 的大小调整为 DataGridView 的大小。对于每一列和每一行,我得到它们各自的宽度和高度,这在技术上应该是准确的,并更新面板和整体 DataGridView 大小。

问题是 Column.Width 总是返回 100 像素,无论其实际大小如何(见截图:实际列宽约为 30 像素,而不是 100)。如果我手动输入宽度 = 90 像素,则调整大小非常准确!

matrix = new DataGridView();
//modify behaviour
matrix.ColumnHeadersVisible = false;
matrix.AllowUserToResizeColumns = false;
matrix.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
matrix.RowHeadersVisible = false;
matrix.AllowUserToResizeRows = false;
//modify positioning
matrix.Location = new Point(10, 20);
//matrix.Anchor = (AnchorStyles)(AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right);
matrix.Dock = DockStyle.Fill;
//set the size of the matrix
matrix.ColumnCount = col;
matrix.RowCount = row;


//Data now inserted...
matrix.AutoResizeColumns(); //correctly resizes the columns

int height = 0;
foreach (DataGridViewRow row in matrix.Rows)
{
    height += row.Height;
}
height += matrix.ColumnHeadersHeight;

int width = 0;
foreach (DataGridViewColumn col in matrix.Columns)
{
    width += col.Width;
    //PROBLEM: Width always  = 100 pixels.
}
width += matrix.RowHeadersWidth;
//width = 90; //override width manually
matrix.Size = new Size(width + 2, height + 2);
panel.Size = new Size(matrix.Width, matrix.Height);

面板这么大是因为宽度不是 90px 而是大约 357,这是错误的!

编辑:部分修复 我找到了一种方法来获得正确的单元格宽度:

DataGridView.Rows[0].Cells[0].ContentBounds.Width
//ContentBounds = a rectangle with the exact dimensions of that cell

我现在可以将 DataGridView 设置为正确的大小,但前提是它没有停靠到填充。设置 matrix.Dock = DockStyle.Fill 会阻止正确调整大小。

【问题讨论】:

    标签: c# datagridview


    【解决方案1】:

    我猜那是因为您正在自动调整行中的列大小:

    matrix.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
    

    尝试将其更改为:

    matrix.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
    

    【讨论】:

    • 如果我把它改成none,然后调用matrix.AutoResizeColumns();单元格大小不适合单元格内容尺寸(单元格确实保持在 100 像素)。尽管我没有为标题设置任何值并隐藏了它们,但是否可能是列标题强制网格为 100px?编辑:我已经显示了标题,并且在调用 AutoResizeColumns() 时这些标题会正确调整大小。调用 AutoResizeColumns 时似乎没有更新 Width 属性...
    • 很遗憾,我现在没有 Visual Studio 来试用您的代码,但这里有一篇文章可能会帮助您调整大小:msdn.microsoft.com/en-us/library/74b2wakt%28v=vs.110%29.aspx。另外据我所知,可以调用 matrix.AutoResizeRows( DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders) 之类的东西,这可能有用
    • 感谢您的链接,我暂时解决了一个问题(即将编辑我的帖子),但现在似乎有其他问题阻止了正确调整大小。
    【解决方案2】:

    我已经弄清楚问题所在了:

    1. DataGridView.Dock = DockStyle.Fill 阻止 DataGridView.AutoResizeColumns() 方法执行任何操作。

    2. Rectangle rect = matrix.Rows[0].Cells[0].ContentBounds 获取包含单元格值的矩形的尺寸在 AutoResizeColumns() 之后没有更新

    3. 我尝试手动设置列,但按照 heq 的建议,AutoSizeColumnsMode 必须设置为 None 才能修改宽度。

    4. 我手动调整了每列的宽度,单元格内每个字符为10px

    最终解决方案

    /* Create the DataGridView */
    matrix = new DataGridView();
    //modify behaviour
    matrix.ColumnHeadersVisible = false;
    // matrix.columnhea
    matrix.AllowUserToResizeColumns = false;
    matrix.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
    matrix.RowHeadersVisible = false;
    matrix.AllowUserToResizeRows = false;
    matrix.ReadOnly = true;
    //modify positioning
    matrix.Location = new Point(10, 20);
    //matrix.Dock = DockStyle.Fill;
    matrix.Anchor = (AnchorStyles)(AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right);
    //set the size of the matrix
    matrix.ColumnCount = cols;
    matrix.RowCount = rows;
    
    panel.Controls.Add(matrix);
    
    /* Set the data contents (not shown) */
    ...
    
    /* Adjust the width of columns */
    int width = 0;
    int height = 0;
    
    for(int c = 0; c < cols; c++) 
    {
      int largest = 0; //largest number of characters in cell
      for(int r = 0; r < rows; r++) 
      {
          int len = matrix.Rows[r].Cells[c].Value.ToString().Length;
          if (len > largest)
          {
              largest = len;
          }
      }
      matrix.Columns[c].Width = largest * 10;
      width += largest * 10;
    }
    
    /* Get height of table */
    foreach (DataGridViewRow row in matrix.Rows)
    {
        height += row.Height;
    }
    
    /* Set the DataGridView Size and Parent Panel size */
    //Note: first set the parent panel width, the table is anchored to all of its edges
    // as such table wont resize correctly if the panel isn't up to size first
    panel.Size = new Size(width + 20, height + 30);
    matrix.Size = new Size(width + 3, height + 3); //is it 1px per row and col?
    
    /* Parent Panel settings kept to default, no modifications */
    

    【讨论】:

      猜你喜欢
      • 2013-01-29
      • 2013-01-24
      • 1970-01-01
      • 1970-01-01
      • 2011-08-18
      • 1970-01-01
      • 2012-08-08
      • 2013-09-24
      • 2023-04-01
      相关资源
      最近更新 更多