在 C# 中,如何像在 Excel 中一样在 DataGrid 中自动调整列宽?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/267239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In C# how do you make the columns in a DataGrid AutoFit Column Width like you can in Excel?
提问by
In C# how do you make the columns in a DataGrid AutoFit Column Width like you can in Excel? Currently my five columns are fixed width but the column headers can change so I would like the columns to autofit to the width of the column.
在 C# 中,如何像在 Excel 中一样在 DataGrid 中自动调整列宽?目前我的五列是固定宽度,但列标题可以更改,所以我希望列自动适应列的宽度。
Thanks
谢谢
回答by BFree
There's a property on the DataGridView called AutoSizeColumnsMode which is an enum. The available values are:
DataGridView 上有一个名为 AutoSizeColumnsMode 的属性,它是一个枚举。可用的值为:
AllCells
所有单元格
AllCellsExceptHeader
AllCellsExceptHeader
ColumnHeader
列标题
DisplayedCells
显示单元格
DisplayedCellsExceptHeader
DisplayedCellsExceptHeader
Fill
填
None
没有任何
回答by Jeremy Bade
To take BFree's answer a step further:
将 BFree 的回答更进一步:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.autoresizecolumns.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.autoresizecolumns.aspx
回答by JVD2C
C# this is my function for add a DataGrid on your form with all fit Columns
C# 这是我的函数,用于在您的表单上添加一个包含所有适合列的 DataGrid
public static DataGrid AddDataGrid(DataGrid DG, object Me, System.Data.DataTable DS)
{
try {
DG.DataSource = DS;
Me.Controls.Add(DG);
DataGridTableStyle TblS = new DataGridTableStyle { MappingName = DS.TableName };
DG.TableStyles.Clear();
DG.TableStyles.Add(TblS);
for (ColIndex = 0; ColIndex <= DS.Columns.Count - 1; ColIndex++) {
int maxlength = 0;
Graphics g = DG.CreateGraphics();
// Take width of one blank space and add to the new width of the Column.
int offset = Convert.ToInt32(Math.Ceiling(g.MeasureString(" ", DG.Font).Width));
int i = 0;
int intaux = 0;
string straux = null;
int tot = DS.Rows.Count;
for (i = 0; i <= (tot - 1); i++) {
straux = DS.Rows[i][ColIndex].ToString();
// Get the width of Current Field String according to the Font.
intaux = Convert.ToInt32(Math.Ceiling(g.MeasureString(straux, DG.Font).Width));
if ((intaux > maxlength)) {
maxlength = intaux;
}
}
// Assign New Width to DataGrid column.
DG.TableStyles(DS.TableName).GridColumnStyles(ColIndex).Width = maxlength + offset;
}
} catch (Exception ex) {
Debug.WriteLine(ex.Message);
} finally {
DG.Show();
}
return DG;
}
a example for using this function...
使用此功能的示例...
private void AddDataGrid(DataSet Ds)
{
AddDataGrid(new DataGrid { Dock = DockStyle.Fill }, this, Ds.Tables[0]);
}
回答by JVD2C
VB this is my function for add a DataGrid on your form with all fit Columns
VB 这是我的功能,用于在您的表单上添加一个带有所有适合列的 DataGrid
Shared Function AddDataGrid(ByVal DG As DataGrid, ByVal This As Object, ByVal DS As System.Data.DataTable) As DataGrid
Try
DG.DataSource = DS
This.Controls.Add(DG)
Dim TblS As New DataGridTableStyle() With {.MappingName = DS.TableName}
DG.TableStyles.Clear()
DG.TableStyles.Add(TblS)
For ColIndex = 0 To DS.Columns.Count - 1
Dim maxlength As Integer = 0
Dim g As Graphics = DG.CreateGraphics()
' Take width of one blank space and add to the new width of the Column.
Dim offset As Integer = Convert.ToInt32(Math.Ceiling(g.MeasureString(" ", DG.Font).Width))
Dim i As Integer = 0
Dim intaux As Integer
Dim straux As String
Dim tot As Integer = DS.Rows.Count
For i = 0 To (tot - 1)
straux = DS.Rows(i)(ColIndex).ToString()
' Get the width of Current Field String according to the Font.
intaux = Convert.ToInt32(Math.Ceiling(g.MeasureString(straux, DG.Font).Width))
If (intaux > maxlength) Then
maxlength = intaux
End If
Next
' Assign New Width to DataGrid column.
DG.TableStyles(DS.TableName).GridColumnStyles(ColIndex).Width = maxlength + offset
Next
Catch ex As Exception
Debug.WriteLine(ex.Message)
Finally
DG.Show()
End Try
Return DG
End Function
a example for using this function...
使用此功能的示例...
Private Sub AddDataGrid(ByVal Ds As DataSet)
AddDataGrid(New DataGrid With {.Dock = DockStyle.Fill}, Me, Ds.Tables(0))
End Sub