/// <summary> /// 初始化控件 /// </summary> /// <param name="form"></param> /// <param name="dgv"></param> /// <param name="json"></param> /// <param name="usingCheck">是否使用复选列</param> /// <param name="usingNo">是否使用编号</param> /// <returns></returns> public bool Init(DataGridView dgv, string json, bool usingCheck = false, bool usingNo = false) { if (!string.IsNullOrWhiteSpace(json)) { clsList = HiCSUtil.Json.Json2Obj <List <DGVColumnInfo> >(json); } else { clsList = new List <DGVColumnInfo>(); } int index = 0; foreach (var it in clsList) { columns[it.ColumnID] = it; it.DipplayIndex = index; index++; } bool result = Init(dgv, usingCheck); if (usingCheck || ColumnHasChk()) // 用户自己添加的列不需要自动添加相关事件 { dgv.CellClick += DGViewEvent.CheckCellClick; // 设置多选控件事件 } if (usingNo) { DGViewUtil.SetRowNo(dgv); } return(true); }
private void SetRowColor_Evt(object sender, DataGridViewRowPostPaintEventArgs e) { DataGridView dgv = sender as DataGridView; if (dgv == null) { return; } DataGridViewRow row = dgv.Rows[e.RowIndex]; if (DGViewUtil.IsRowSelected(dgv, CheckBoxIndex, e.RowIndex)) { SetColor(row.DefaultCellStyle, selColor); } else { if (e.RowIndex % 2 == 0) { SetColor(row.DefaultCellStyle, defColor); } else { SetColor(row.DefaultCellStyle, altColor); } } }
/// <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); }
/// <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> /// <param name="rowIndex"></param> /// <param name="cellIndex"></param> /// <returns></returns> public string GetCellValue(int rowIndex, int cellIndex) { return(DGViewUtil.GetCellValue(myDGV, rowIndex, cellIndex)); }
/// <summary> /// 某行是否已选择 /// </summary> /// <param name="rowIndex"></param> /// <returns></returns> public bool IsRowSelected(int rowIndex) { return(DGViewUtil.IsRowSelected(myDGV, CheckBoxIndex, rowIndex)); }
/// <summary> /// 取得视图某行某列的值 /// </summary> /// <param name="rowIndex"></param> /// <param name="columnName"></param> /// <returns></returns> public string GetCellValue(int rowIndex, string columnName) { return(DGViewUtil.GetCellValue(myDGV, rowIndex, columnName)); }