代码修改WinForm datagridview 样式 及数据绑定
#region 表格设置 /// <summary> /// 调整表格 /// </summary> /// <param name="dataGrid"></param> private void AdjustDataGridView(DataGridView dataGrid) { //设置背景颜色 dataGrid.BackgroundColor = Color.White; //去边框 dataGrid.BorderStyle = BorderStyle.None; dataGrid.CellBorderStyle = DataGridViewCellBorderStyle.None; dataGrid.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.None; //禁止表头点击排序 for (int i = 0; i < dataGrid.Columns.Count; i++) { dataGrid.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable; } //调整表头 dataGrid.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing; dataGrid.ColumnHeadersHeight = 48; dataGrid.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing; //调整行高 dataGrid.RowTemplate.Height = 48; dataGrid.EnableHeadersVisualStyles = false; //表头样式设置 dataGrid.ColumnHeadersDefaultCellStyle = new DataGridViewCellStyle() { BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFAFAFA"), ForeColor = ColorTranslator.FromHtml("#FF999999"), Padding = new Padding() { Left = 10 }, Font = new Font("微软雅黑", 10.5F, FontStyle.Regular, GraphicsUnit.Point, 134) }; //单元格样式 dataGrid.DefaultCellStyle = new DataGridViewCellStyle() { ForeColor = ColorTranslator.FromHtml("#FF333333"), Padding = new Padding() { Left = 10 }, Font = new Font("微软雅黑", 10.5F, FontStyle.Regular, GraphicsUnit.Point, 134) }; //列填充整个表格 dataGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; //禁止用户添加行 dataGrid.AllowUserToAddRows = false; //禁止显示行头 dataGrid.RowHeadersVisible = false; //点击选择整行 dataGrid.SelectionMode = DataGridViewSelectionMode.FullRowSelect; //禁止调整列宽 dataGrid.AllowUserToResizeColumns = false; //禁止调整行高 dataGrid.AllowUserToResizeRows = false; //禁止删除行 dataGrid.AllowUserToDeleteRows = false; //禁止手动定位 dataGrid.AllowUserToOrderColumns = false; //禁用编辑 dataGrid.ReadOnly = true; //去掉多余列 dataGrid.AutoGenerateColumns = false; } //生成普通表头 private void CreateDataGridTextBoxColumnHeader(DataGridView dg, string[] headers) { foreach (var h in headers) { DataGridViewTextBoxColumn dc = new DataGridViewTextBoxColumn() { Name = h }; dg.Columns.Add(dc); } } //生成操作列(可点击的列) private void CreateDataGridLinkButtonHeader(DataGridView dg, string[] headers) { for (int i = 0; i < headers.Length; i++) { DataGridViewLinkColumn dc = new DataGridViewLinkColumn() { Name = headers[i], LinkBehavior = LinkBehavior.NeverUnderline }; dg.Columns.Add(dc); } } /// <summary> /// 表格数据绑定 /// </summary> /// <param name="source"></param> private void DataGridBinds(DataGridView dg, object source, string[] headerText, string[] properts) { //if (source is DataTable) //{ // DataTable dt = source as DataTable; // if (dt.Rows.Count > 0) // { // } //} //else //{ //} CreateDataGridTextBoxColumnHeader(dg, headerText); CreateDataGridLinkButtonHeader(dg, new string[] { "操作" }); AdjustDataGridView(dg); for (int i = 0; i < dg.Columns.Count; i++) { try { dg.Columns[i].DataPropertyName = properts[i]; } catch (Exception ex) { MessageBox.Show(ex.Message); break; } } dg.DataSource = source; } #endregion
选中表格行不改变背景色
private void dgRegistDev_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { e.CellStyle.SelectionForeColor = e.CellStyle.ForeColor; e.CellStyle.SelectionBackColor = e.CellStyle.BackColor; }