/// <summary> /// 设置数据表格样式,并将最后一列填充其余空白 /// </summary> /// <param name="dgv">数据表格</param> /// <param name="column">最后一列</param> public static void SetDataGridViewStyle(DataGridViewEx dgv, DataGridViewColumn column) { if (column != null) { column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; //最后一列填充 } var dataGridViewCellStyle1 = new DataGridViewCellStyle(); var dataGridViewCellStyle2 = new DataGridViewCellStyle(); var dataGridViewCellStyle3 = new DataGridViewCellStyle(); var dataGridViewCellStyle4 = new DataGridViewCellStyle(); dataGridViewCellStyle1.BackColor = Color.WhiteSmoke; dgv.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1; dataGridViewCellStyle2.Alignment = DataGridViewContentAlignment.MiddleCenter; dataGridViewCellStyle2.BackColor = Color.FromArgb(240, 241, 243); dataGridViewCellStyle2.Font = new Font("微软雅黑", 9F, FontStyle.Bold); dataGridViewCellStyle2.ForeColor = Color.FromArgb(51, 51, 51); dataGridViewCellStyle2.SelectionBackColor = SystemColors.Highlight; dataGridViewCellStyle2.SelectionForeColor = SystemColors.HighlightText; dataGridViewCellStyle2.WrapMode = DataGridViewTriState.True; dgv.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle2; //选中行颜色 dataGridViewCellStyle3.SelectionBackColor = Color.FromArgb(183, 233, 156); dataGridViewCellStyle3.Font = new Font("宋体", 9F, FontStyle.Regular); dataGridViewCellStyle3.ForeColor = Color.Black; dataGridViewCellStyle3.SelectionForeColor = Color.FromArgb(51, 51, 51); dgv.RowsDefaultCellStyle = dataGridViewCellStyle3; dgv.GridColor = Color.FromArgb(221, 221, 221); //序号列样式-行标题背景色 选中色 dataGridViewCellStyle4.BackColor = Color.FromArgb(224, 236, 255); dataGridViewCellStyle4.SelectionBackColor = Color.FromArgb(224, 236, 255); dgv.RowHeadersDefaultCellStyle = dataGridViewCellStyle4; //行标题边框样式 dgv.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single; }
/// <summary> /// 设置数据表格样式 /// </summary> /// <param name="dgv">数据表格</param> public static void SetDataGridViewStyle(DataGridViewEx dgv) { SetDataGridViewStyle(dgv, null); }