GridView中所有列的列宽 [英] Column Width for all Columns in GridView
问题描述
各位大家好,
我很长一段时间没有写过一行,而且我正在学习c#,我是编程新手。
我正在处理表格和列,我无法为网格视图中的所有列设置固定宽度。
任何人都可以帮帮我?
提前谢谢。
David
Datagridview有一个属性AutoSizeColumnsMode,带有以下选项
//摘要:
//定义值 for 指定如何调整列的宽度。
public enum DataGridViewAutoSizeColumnsMode
{
//汇总:列width 执行 不自动调整。
无= 1 ,
//
//摘要:列宽调整 适合列标题单元格的 。
ColumnHeader = 2 ,
//摘要:
//列宽调整
AllCellsExceptHeader = 4 ,
//
//摘要:
//列宽调整> 符合列 列列列列的内容标题单元格。
AllCells = 6 ,
//
//摘要:列宽调整 所有单元格 列
//中的内容当前显示在屏幕上的行中的,不包括标题单元格。
DisplayedCellsExceptHeader = 8 ,
//
//摘要:
//列宽调整> 符合所有单元格的内容 列
//当前显示在屏幕上的 行,包括标题单元格。
DisplayedCells = 10 ,
//
//汇总:
//调整列宽以便宽度所有列的 所有列都完全填充控件的
//显示区域,仅需要水平滚动 保持
//列宽在System.Windows.Forms.DataGridViewColumn.MinimumWidth
//属性值之上。相对列宽由相对System.Windows.Forms.DataGridViewColumn.FillWeight
//属性值确定。
Fill = 16 ,
}
设置属性:AutoSizeColumnsMode = ColumnHeader它将根据列标题字符串宽度自动调整列的大小。
GridView1.Columns [i] .ItemStyle.Width = colWidth ;
第二解决方案:::::::
step1:创建一个函数,如下所示
public DataGridViewTextBoxColumn GetDataGridViewColumn(ref DataGridView DGV,string colName,string colHeader,int colWidth,bool colVisibility = true,string DataPropertyName =)
{
DataGridViewTextBoxColumn DGVTbCol = new DataGridViewTextBoxColumn();
DGVTbCol.Name = colName;
if(string.IsNullOrEmpty(DataPropertyName)){
DGVTbCol.DataPropertyName = colName;
} else {
DGVTbCol.DataPropertyName = DataPropertyName;
}
DGVTbCol.HeaderText = colHeader;
DGVTbCol.DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;
DGVTbCol.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
DGVTbCol.Width = colWidth;
DGVTbCol.Visible = colVisibility;
if(colVisibility){
DGV.Tag + = colWidth;
}
返回DGVTbCol;
}
第二步: -
String C_Id =1stColumnName
String C_Id2 =2ndColumnName
private void SetColumns( ref DataGridView DGV)
{
int AlignLeft = 0 ;
int AlignRight = 1 ;
int AlignCenter = 2 ;
int ControlStart = 0 ;
int CtrlTop = 0 ;
int ColWidth = 0 ;
DGV.AutoGenerateColumns = false ;
DGV.MultiSelect = true ;
DGV.ReadOnly = false ;
DGV.Tag = 0 ;
DGV.Columns.Clear();
ColWidth = 70 ; // 指定您想要的宽度
DGV.Columns.Add(GetDataGridViewColumn(DGV,C_Id) , ColumnName,ColWidth,False))
DGV.Columns.Add(GetDataGridViewColumn( DGV,C_Id2, 2ndColumnName,ColWidth,True))
}
Hello everybody,
I am new in programming after a long period of 10 years without writing a line and I am learning c# by myself.
I am dealing with Forms and Columns and I can not get the way of setting a fixed width for all the columns in a Grid View.
Can anyone help me?
Thank you in advance.
David
Datagridview has a property AutoSizeColumnsMode with following options
// Summary: // Defines values for specifying how the widths of columns are adjusted. public enum DataGridViewAutoSizeColumnsMode { //Summary:The column widths do not automatically adjust. None = 1, // //Summary:The column widths adjust to fit the contents of the column header cells. ColumnHeader = 2, // Summary: //The column widths adjust to fit the contents of all cells in the columns,excluding header cells. AllCellsExceptHeader = 4, // // Summary: //The column widths adjust to fit the contents of all cells in the columns,including header cells. AllCells = 6, // //Summary:The column widths adjust to fit the contents of all cells in the columns //that are in rows currently displayed onscreen, excluding header cells. DisplayedCellsExceptHeader = 8, // // Summary: //The column widths adjust to fit the contents of all cells in the columns //that are in rows currently displayed onscreen, including header cells. DisplayedCells = 10, // // Summary: //The column widths adjust so that the widths of all columns exactly fill the //display area of the control, requiring horizontal scrolling only to keep //column widths above the System.Windows.Forms.DataGridViewColumn.MinimumWidth //property values. Relative column widths are determined by the relative System.Windows.Forms.DataGridViewColumn.FillWeight //property values. Fill = 16, }
set the property :AutoSizeColumnsMode=ColumnHeader it will auto resize your column based on the Column Header string width.
GridView1.Columns[i].ItemStyle.Width = colWidth;
2nd Solution:::::::
step1:-Create a function,given below
public DataGridViewTextBoxColumn GetDataGridViewColumn(ref DataGridView DGV, string colName, string colHeader, int colWidth, bool colVisibility = true, string DataPropertyName = "") { DataGridViewTextBoxColumn DGVTbCol = new DataGridViewTextBoxColumn(); DGVTbCol.Name = colName; if (string.IsNullOrEmpty(DataPropertyName)) { DGVTbCol.DataPropertyName = colName; } else { DGVTbCol.DataPropertyName = DataPropertyName; } DGVTbCol.HeaderText = colHeader; DGVTbCol.DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft; DGVTbCol.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter; DGVTbCol.Width = colWidth; DGVTbCol.Visible = colVisibility; if (colVisibility) { DGV.Tag += colWidth; } return DGVTbCol; }
2nd Step:-
String C_Id = "1stColumnName" String C_Id2="2ndColumnName"
private void SetColumns(ref DataGridView DGV) { int AlignLeft = 0; int AlignRight = 1; int AlignCenter = 2; int ControlStart = 0; int CtrlTop = 0; int ColWidth = 0; DGV.AutoGenerateColumns = false; DGV.MultiSelect = true; DGV.ReadOnly = false; DGV.Tag = 0; DGV.Columns.Clear(); ColWidth=70; //Specify width as u want DGV.Columns.Add(GetDataGridViewColumn(DGV, C_Id, "ColumnName", ColWidth, False)) DGV.Columns.Add(GetDataGridViewColumn(DGV, C_Id2, "2ndColumnName",ColWidth, True)) }
这篇关于GridView中所有列的列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!