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

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

问题描述

我在Windows窗体(C#2.0不是WPF)上填充一个DataGridView控件。



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



我试图通过将每列的AutoSizeMode设置为 DataGridViewAutoSizeColumnMode.AllCells 来实现此目的,除了我设置为 DataGridViewAutoSizeColumnMode的列之外.Fill ,以确保网格的整个区域整齐地填充数据。 (我不介意,当用户尝试调整此列的大小时,它将恢复到一个大小,以确保水平空间始终被使用。)



然而,因为我一旦加载,我想允许用户调整列的大小以满足自己的要求 - 在为每个列设置这些AutoSizeMode值时,用户然后无法调整这些列的大小。



我尝试没有设置允许调整大小的所有列的AutoSizeMode,但是根据单元格包含的数据,不会设置初始大小。在加载数据后将网格的AutoSizeMode更改回未设置时,会发生相同的结果。



是否有一个我在此丢失的设置,允许自动设置默认值列宽和用户调整大小,还是在填充DataGridView控件时必须使用另一种技术?

解决方案

这个技巧适用于我:

  grd.DataSource = DT; 

//设置自动调整模式
grd.Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
grd.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
grd.Columns(2).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

// datagrid已经计算了它的宽度,所以我们可以存储
(i = 0; i <= grd.Columns.Count - 1; i ++){
/ /存储自动调整的宽度
int colw = grd.Columns(i).Width;
//删除自动调整
grd.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
//设置宽度由autosize计算
grd.Columns(i).Width = colw;
}

这里发生的是将自动调整设置为所需的模式,然后列通过列存储从自动调整计算得到的宽度,删除自动调整并将宽度设置为以前存储的值。


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.

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.)

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.

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.

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?

解决方案

This trick works for me:

grd.DataSource = DT;

//set autosize mode
grd.Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
grd.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
grd.Columns(2).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

//datagrid has calculated it's widths so we can store them
for (i = 0; i <= grd.Columns.Count - 1; i++) {
    //store autosized widths
    int colw = grd.Columns(i).Width;
    //remove autosizing
    grd.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
    //set width to calculated by autosize
    grd.Columns(i).Width = colw;
}

What happens here is that you set autosize to whathever mode you need and then column by column you store the width it got from autosize calculation, remove autosizing and set width to value you stored before.

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

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