/// <summary> /// 设置列对齐方式 /// </summary> /// <param name="info"></param> /// <param name="column"></param> public static void SetAlign(DGVColumnInfo info, DataGridViewColumn column) { if (info.Align == null) { return; } string align = info.Align.Trim().ToLower(); if (align.Equals("left")) { column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft; column.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft; return; } if (align.Equals("right")) { column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; column.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight; return; } if (align.Equals("center")) { column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; column.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter; return; } }
/// <summary> /// 创建并初始化控件 /// </summary> /// <param name="dic"></param> /// <param name="dgv"></param> public static DataGridViewCheckBoxColumn CreateAndInitControls(List <DGVColumnInfo> lst, DataGridView dgv) { DataGridViewCheckBoxColumn chkColumn = null; foreach (var it in lst) { if (it.Control == null) { it.Control = DGVColumnInfo.CreateControl(it); if (it.Control is DataGridViewCheckBoxColumn) { chkColumn = it.Control as DataGridViewCheckBoxColumn; } dgv.Columns.Add(it.Control); } } foreach (var it in lst) { it.Control.HeaderText = it.ColumnText; it.Control.DataPropertyName = it.ColumnName; it.Control.Name = it.ColumnID; } return(chkColumn); }
/// <summary> /// 对已存在的列进行处理,并创建多选列 /// </summary> private void InitExistColumns8Tag(DataGridView dgv) { foreach (DataGridViewColumn it in dgv.Columns) { string key = Convert.ToString(it.Tag); if (string.IsNullOrWhiteSpace(key)) { key = it.Name; } DGVColumnInfo ex = null; if (!columns.TryGetValue(key, out ex)) { continue; } if ((string)it.Tag != key) { it.Tag = key; } if (ex != null) { ex.Control = it; } if (it is DataGridViewCheckBoxColumn) { CheckBoxIndex = it.Index; } } }
/// <summary> /// 初始化控件 /// </summary> /// <param name="form"></param> /// <param name="dgv"></param> /// <param name="cls"></param> /// <returns></returns> private bool Init(DataGridView dgv, bool isUsingCheck) { if (dgv == null) { return(false); } myDGV = dgv; dgv.AutoGenerateColumns = false; dgv.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders; dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; InitExistColumns8Tag(dgv); // 对已存在的列进行处理 if (CheckBoxIndex < 0 && isUsingCheck && !ColumnHasChk()) // 自动创建多选列 { checkColumn = DGViewUtil.CreateCheckBoxColumn(20, true); myDGV.Columns.Add(checkColumn); } DataGridViewCheckBoxColumn chk = DGVColumnInfo.CreateAndInitControls(clsList, dgv); // 创建不存在的列并设置相关信息 if (chk != null) // 自动创建多选列 { checkColumn = chk; } OnResize(); // 设置列宽 return(true); }
private bool ColumnHasChk() { if (initHasChk != null) { return(initHasChk.Equals("1")); } initHasChk = DGVColumnInfo.HasChkColumn(clsList) ? "1" : "0"; return(initHasChk.Equals("1")); }
/// <summary> /// 控件改动时修改列宽度 /// </summary> /// <param name="lst"></param> /// <param name="width"></param> /// <param name="displayStartIndex"></param> public static void ControlsResize(List <DGVColumnInfo> lst, int width, int displayStartIndex) { foreach (var it in lst) { if (it.Width > 0) { it.Control.Width = it.Width; } else { it.Control.Width = (width * it.WidthPercent) / 100; } it.Control.DisplayIndex = it.DipplayIndex + displayStartIndex; DGVColumnInfo.SetAlign(it, it.Control); } }
/// <summary> /// 常见列 /// </summary> /// <param name="info"></param> /// <returns></returns> private static DataGridViewColumn CreateControl(DGVColumnInfo info) { if (info.Type.ToLower().Equals("chk")) { return(DGViewUtil.CreateCheckBoxColumn()); } else if (info.Type.ToLower().Equals("chk_head")) { return(DGViewUtil.CreateCheckBoxColumn(20, true)); } else { DataGridViewColumn ex = new DataGridViewTextBoxColumn(); ex.Tag = info.ColumnID; if (info.IsShow == 0) { ex.Visible = false; } ex.DefaultCellStyle.WrapMode = DataGridViewTriState.True; return(ex); } }
/// <summary> /// 设置列宽 /// </summary> public void OnResize() { if (myDGV == null) { return; } int width = myDGV.Size.Width - 5; if (myDGV.RowHeadersVisible) { width -= myDGV.RowHeadersWidth; } if (checkColumn != null) { CheckBoxIndex = checkColumn.Index; } int startIndex = 0; if (IsAutoCreateChkColumn()) { width -= checkColumn.Width; startIndex++; } int pinWidth = DGVColumnInfo.GetWidths(clsList); width -= pinWidth; if (width < 0) { width = myDGV.Size.Width; } DGVColumnInfo.ControlsResize(clsList, width, startIndex); // 重新设置列宽度 }