Datagridview列自动大小

时间:2018-09-06 07:58:19

标签: c# winforms datagridview

我有一个数据网格视图,其中包含许多列, AutoSizeColumnsMode 设置为 AllCells 。 现在我的问题是,为什么大多数列标题旁边都有这么多的空白空间? 例如,带有“ HBW 5/250”的列标题的空白空间几乎与文本所需的空间相同。背后有原因吗? 我的问题是,整个datagridview应该在一个屏幕(24英寸)上可见,而且我不知道如何才能使这些列的大小与最长的文本一样大。

这是一个C#WinForms应用程序。

我也研究了AutoSizeColumnsMode属性的所有其他选项,但对我没有任何帮助。

Datagridview

最诚挚的问候。

2 个答案:

答案 0 :(得分:1)

为了在datagridview中自动调整单元大小的更准确,更快速的方法,我发现了某个地方(不知道在哪里)并修改了一些功能,所以现在看起来是这样:

public static void FastAutoSizeColumns(this DataGridView targetGrid)
{
    //If targeted datagridview doesn't have rows just return
    if (targetGrid.Columns.Count < 1)
        return;
    var gridTable = new DataTable();

    // Cast out a DataTable from the target grid datasource.
    // We need to iterate through all the data in the grid and a DataTable supports enumeration.
    gridTable = (DataTable)targetGrid.DataSource;

    targetGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
    // Create a graphics object from the target grid. Used for measuring text size.
    using (var gfx = targetGrid.CreateGraphics())
    {
        // Iterate through the columns.
        for (int i = 0; i < gridTable.Columns.Count; i++)
        {
            // Leverage Linq enumerator to rapidly collect all the rows into a string array, making sure to exclude null values.
            string[] colStringCollection = gridTable.AsEnumerable().Where(r => r.Field<object>(i) != null).Select(r => r.Field<object>(i).ToString()).ToArray();

            // Sort the string array by string lengths.
            colStringCollection = colStringCollection.OrderBy((x) => x.Length).ToArray();

            if (colStringCollection.Length > 0)
            {
                // Get the last and longest string in the array.
                string longestColString = colStringCollection.Last();

                // Use the graphics object to measure the string size.
                var colWidth = gfx.MeasureString(longestColString, targetGrid.Font);

                // If the calculated width is larger than the column header width, set the new column width.
                if (colWidth.Width > targetGrid.Columns[i].HeaderCell.Size.Width)
                {
                    targetGrid.Columns[i].Width = (int)colWidth.Width;
                }
                else // Otherwise, set the column width to the header width.
                {
                    targetGrid.Columns[i].Width = targetGrid.Columns[i].HeaderCell.Size.Width;
                }
            }
            else
            {
                targetGrid.Columns[i].Width = targetGrid.Columns[i].HeaderCell.Size.Width;
            }
        }
    }
}

将此功能导入文件中的某个位置(我使用自定义扩展名使用单独的文件),并使用以下命令进行调用:

yourDataGridView.FastAutoSizeCoulmns();

重要说明:DataSource必须是DataTable的类型(因此不能为IList或其他类型,因此在绑定数据源时首先将其转换为datatable或对此功能进行一些修改)

答案 1 :(得分:1)

如果您的列是可排序的,则在右侧出现排序箭头的地方会有多余的空白。如果您不需要列可排序,则将列排序模式设置为NotSortable,这样可以节省一些空间。

public Point findStart() {
    Point location = new Point();
    for (int i = 0; i < map.length; i++) {
        for (int j = 0; j < map[i].length; j++) {
            if (map[i][j] == 's') {
                location.setLocation(i, j);
                break;
            }
        }
    }
    System.out.println(location.x + "," + location.y);
    return location;
}

不幸的是,这不会消除页眉中所有多余的空白。当Windowsforms必须自动换行时,它在管理列宽方面并不擅长。如果您用新行替换列标题中的空格(如下所示),您会注意到所有空白都消失了,并且由于不需要换行,因此可以完美调整列的大小。

dataGridView1.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;