您如何自动调整 DataGridView 控件中的列大小并允许用户调整同一网格上的列大小? [英] How do you automatically resize columns in a DataGridView control AND allow the user to resize the columns on that same grid?

查看:31
本文介绍了您如何自动调整 DataGridView 控件中的列大小并允许用户调整同一网格上的列大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Windows 窗体(C# 2.0 而非 WPF)上填充 DataGridView 控件.

I am populating a DataGridView control on a Windows Form (C# 2.0 not WPF).

我的目标是显示一个网格,用单元格整齐地填充所有可用宽度 - 即右侧没有未使用的(深灰色)区域,并根据其包含的数据适当调整每列的大小,但是还允许用户根据自己的喜好调整任何列的大小.

My goal is to display a grid that neatly fills all available width with cells - i.e. no unused (dark grey) areas down the right and sizes each column appropriately according to the data it contains, but also allows the user to resize any of the columns to their liking.

我试图通过将每列的 AutoSizeMode 设置为 DataGridViewAutoSizeColumnMode.AllCells 来实现这一点,除了我按顺序设置为 DataGridViewAutoSizeColumnMode.Fill> 的列之一以确保整个网格区域都整齐地填充了数据.(我不介意当用户尝试调整此列的大小时,它会弹回到确保始终使用水平空间的大小.)

I am attempting to achieve this by setting the AutoSizeMode of each column to be DataGridViewAutoSizeColumnMode.AllCells except for one of the columns which I set to DataGridViewAutoSizeColumnMode.Fill in order to ensure the entire area of the grid is neatly filled with data. (I don't mind that when the user attempt to resize this column it springs back to a size that ensures the horizontal space is always used.)

但是,正如我所提到的,一旦加载,我希望允许用户调整列的大小以满足他们自己的要求 - 在为每列设置这些 AutoSizeMode 值时,用户似乎无法调整这些列的大小.

However, as I mentioned, once loaded I would like to allow the user to resize the columns to suit their own requirements - in setting these AutoSizeMode values for each column it appears the user is then unable to then resize those columns.

我尝试不设置允许调整大小的所有列的 AutoSizeMode 但不会根据单元格包含的数据设置初始大小.在加载数据后将网格的 AutoSizeMode 改回未设置"时会出现相同的结果.

I've tried not setting the AutoSizeMode of all the columns which does allow resizing BUT doesn't set the initial size according to the data the cells contain. The same result occurs when changing the grid's AutoSizeMode back to "Not Set" after loading the data.

这里是否缺少允许自动设置默认列宽和用户调整大小的设置,或者在填充 DataGridView 控件时是否必须使用另一种技术?

Is there a setting I'm missing here which allows automatic setting of default column widths AND user resizing or is there another technique I must use when populating the DataGridView control?

推荐答案

这个技巧对我有用:

    grd.DataSource = DT;

    // Set your desired AutoSize Mode:
    grd.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
    grd.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
    grd.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

    // Now that DataGridView has calculated it's Widths; we can now store each column Width values.
    for (int i = 0; i <= grd.Columns.Count - 1; i++)
    {
        // Store Auto Sized Widths:
        int colw = grd.Columns[i].Width;

        // Remove AutoSizing:
        grd.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;

        // Set Width to calculated AutoSize value:
        grd.Columns[i].Width = colw;
    }

在上面的代码中:您可以将 Columns AutoSize 属性设置为您需要的任何 AutoSizeMode.然后(逐列)存储每个列的宽度值(来自 AutoSize 值);禁用 AutoSize 属性,最后将 Column Width 设置为您之前存储的 Width 值.

In the Code above: You set the Columns AutoSize Property to whathever AutoSizeMode you need. Then (Column by Column) you store each column Width value (from AutoSize value); Disable the AutoSize Property and finally, set the Column Width to the Width value you previously stored.

这篇关于您如何自动调整 DataGridView 控件中的列大小并允许用户调整同一网格上的列大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆