当列表绑定到datagridview时修复列宽 [英] fix a columns width when a list is bound to a datagridview
问题描述
我有,我绑定到一个DataGridView的列表。我想的第一列是一个固定的大小。该数据被绑定到DataGridView和我似乎无法找到一种方法来访问个人colums属性。如果我尝试myDatagridview.colums [0]我得到一个索引出界,因为它说的列数为0。
I have a list that I have bound to a datagridview. I want the first column to be a fixed size. The data is bound to the dataGridView and I can't seem to find a way to access an individual colums properties. if I try myDatagridview.colums[0] I get an index out of bounds, since it says the columns count is 0.
private DataGridView setUpDataGrid(List<NVRlineVal> _NVRData)
{
//setup dataGridView
DataGridView NVRDataGridView = new System.Windows.Forms.DataGridView();
NVRDataGridView.ColumnHeadersHeightSizeMode =
System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
NVRDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
NVRDataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
NVRDataGridView.Name = "NVRDataGridView" + nvrIndex;
NVRDataGridView.RowHeadersWidthSizeMode =
System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders;
NVRDataGridView.TabIndex = 0;
NVRDataGridView.DataSource = _NVRData;
//var clmn = NVRDataGridView.Columns[0];
return NVRDataGridView;
}
这是如何有一个固定的列宽只有这些列之一任何想法,其余的将自动调整大小?
Any ideas on how to have a fixed column width for only one of these columns, the rest will autosize?
编辑原代码,展现出更加清理的版本,仍然有效。
edited original code, to show a more cleaned up version that still works
推荐答案
我做类似的但格式的东西。
I do something similar but with formatting.
的问题是,网格不是数据源的设置后绘制的 - 你需要做的,你在 DataBindingComplete
事件而改变。
The problem is that the grid is not drawn after the setting of the data source - you need to do you changes in the DataBindingComplete
event.
下面是一些片断......
Here are some snippets....
(从的https:/ /minisqlquery.svn.codeplex.com/svn/trunk/MiniSqlQuery/QueryForm.cs )
- 格DEF:
this.gridResults1.AllowUserToAddRows = false;
this.gridResults1.AllowUserToDeleteRows = false;
this.gridResults1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.ColumnHeader;
this.gridResults1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.gridResults1.Dock = System.Windows.Forms.DockStyle.Fill;
this.gridResults1.Location = new System.Drawing.Point(3, 3);
this.gridResults1.Name = "gridResults1";
this.gridResults1.ReadOnly = true;
this.gridResults1.Size = new System.Drawing.Size(1023, 242);
this.gridResults1.TabIndex = 0;
- 后来订阅 DataBindingComplete
事件
grid.DataBindingComplete += GridDataBindingComplete;
- 处理事件(我在这里修改的格式,但你尝试改变宽度):
-- handle the event (here I am modifying the format but you try changing the width):
private void GridDataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
DataGridView grid = sender as DataGridView;
if (grid == null)
{
return;
}
DataTable dt = grid.DataSource as DataTable;
if (dt == null)
{
return;
}
string nullText = _settings.NullText;
string dateTimeFormat = _settings.DateTimeFormat;
for (int i = 0; i < dt.Columns.Count; i++)
{
if (dt.Columns[i].DataType == typeof (DateTime))
{
DataGridViewCellStyle dateCellStyle = new DataGridViewCellStyle();
dateCellStyle.NullValue = nullText;
dateCellStyle.Format = dateTimeFormat;
grid.Columns[i].DefaultCellStyle = dateCellStyle;
}
}
}
这篇关于当列表绑定到datagridview时修复列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!