得到实际的列宽

时间:2014-03-21 01:25:28

标签: c# datagridview

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

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

问题是Column.Width始终返回100像素而不管其实际大小(请参见屏幕截图:实际列宽约为30px,而不是100)。如果我手动输入width = 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);

screenshot

面板太大了,因为宽度不是90px而是大致357,这是错误的!

编辑:PARTIAL FIX 我找到了一种获得正确宽度的方法:

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

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

2 个答案:

答案 0 :(得分:1)

我想这是因为您要在行中自动调整列:

matrix.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

尝试将其更改为:

matrix.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;

答案 1 :(得分:0)

我已经弄清楚问题是什么:

  1. DataGridView.Dock = DockStyle.Fill阻止了DataGridView.AutoResizeColumns()方法做任何事情。

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

  3. 后未更新
  4. 我尝试手动设置列,但建议 heq 时,必须将AutoSizeColumnsMode设置为None才能修改宽度。

  5. 我手动调整了每列的宽度,对于单元格内的每个字符都是10px

  6. 最终解决方案

    /* 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 */