你如何自动调整列DataGridView控件,并允许用户调整上同一电网中的列? [英] How do you automatically resize columns in a DataGridView control AND allow the user to resize the columns on that same grid?
问题描述
我填充一个Windows窗体(C#2.0不WPF)的DataGridView控件。
我的目标是显示整齐地填满了细胞的所有可用宽度的网格 - 即没有未使用(深灰色)地区降权和大小每列适当根据它包含的数据,,但还允许用户调整任何列到他们的喜好。
我试图通过设置每列AutoSizeMode为 DataGridViewAutoSizeColumnMode.AllCells ,除了我在顺序设置为 DataGridViewAutoSizeColumnMode.Fill 的一列来实现这一以确保网格的整个区域被整齐地填充有数据。 (我不介意,当用户试图调整这一栏弹回的大小,确保水平空间总是使用。)
不过,正如我所说,一旦加载我想允许用户调整列,以满足自己的要求 - 在它出现在用户每列设置这些AutoSizeMode值,然后无法再调整这些列<。 / p>
我试过不设置所有这确实允许调整但根据单元格包含的数据不设置初始大小列的AutoSizeMode。装载数据后更改网格的AutoSizeMode当回未设置出现相同的结果。
有我在这里失踪的设置,允许的默认列宽和调整大小的用户自动设置或有另一种方法填充DataGridView控件时,我必须用?
这招对我的作品:
grd.DataSource = DT 设置自动调整模式
grd.Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
grd.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
grd.Columns(2).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill 数据网格计算出它的宽度,所以我们可以存储它们
对于i = 0到grd.Columns.Count - 1
店里的宽度自动调整大小
昏暗colw作为整数= grd.Columns(我).WIDTH
自动调整大小删除
grd.Columns(我).AutoSizeMode = DataGridViewAutoSizeColumnMode.None
设置宽度由自动调整到计算
grd.Columns(I)= .WIDTH colw
下一个
这里发生的是,你将AutoSize设为whathever由您存储从自动调整计算得到的宽度列你需要的模式,然后栏中移除自动调整大小并设置宽度价值,你之前存储的。
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 To grd.Columns.Count - 1
'store autosized widths
Dim colw As Integer = grd.Columns(i).Width
'remove autosizing
grd.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnMode.None
'set width to calculated by autosize
grd.Columns(i).Width = colw
Next
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屋!