public partial class Form4 : Form { public Form4() { InitializeComponent(); } #region datagridview void RowColSet(DataGridView grid) { //不显示标题栏 grid.ColumnHeadersVisible = false; //边框线设置 grid.GridColor = Color.FromArgb(122, 122, 122); ///不显示默认第一列 grid.RowHeadersVisible = false; //不显示最后一行空白行 grid.AllowUserToAddRows= false; //整行选中 grid.SelectionMode = DataGridViewSelectionMode.FullRowSelect; ////////////////自动适应列高 grid.DefaultCellStyle.WrapMode = DataGridViewTriState.True; grid.AutoResizeColumns(); grid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders; Parallel.For(0, grid.ColumnCount, index => { this.Invoke(new Action(() => { grid.Columns[index].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; ////////////////自动适应列宽 grid.Columns[index].SortMode = DataGridViewColumnSortMode.NotSortable; //列不可排序 grid.Columns[index].ReadOnly = true; //列不可编辑 //第一第二列前景色为白色 if (index < 2) { grid.Columns[index].DefaultCellStyle.ForeColor = Color.White; } //1到4列字体大小设置 if (index < 4) { grid.Columns[index].DefaultCellStyle.Font = new Font("微软雅黑", 18); } else { grid.Columns[index].DefaultCellStyle.Font = new Font("微软雅黑", 28); } //除第二列外,居中显示 if (index != 1) { grid.Columns[index].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; } else { grid.Columns[index].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft; } })); }); //默认不选中 grid.ClearSelection(); } void SetRowOK(DataGridView grid, int index) { grid.Rows[index].DefaultCellStyle.BackColor = Color.FromArgb(0,38, 11); // 从第三列前景色为绿色 for (int index1 = 2; index1 < grid.ColumnCount; index1++) { this.Invoke(new Action(() => { grid.Rows[index].Cells[index1].Style.ForeColor = Color.FromArgb(0, 255, 28); })); } } void SetRowNG(DataGridView grid, int index) { grid.Rows[index].DefaultCellStyle.BackColor = Color.FromArgb(0, 0, 0); // 从第三列前景色为红色 for (int index1 = 2; index1 < grid.ColumnCount; index1++) { this.Invoke(new Action(() => { grid.Rows[index].Cells[index1].Style.ForeColor = Color.Red; })); } } void SetRowNA(DataGridView grid,int index) { grid.Rows[index].DefaultCellStyle.BackColor = Color.FromArgb(48, 48, 48); // 从第三列前景色为白色 for(int index1 = 2;index1< grid.ColumnCount;index1++) { this.Invoke(new Action(() => { grid.Rows[index].Cells[index1].Style.ForeColor = Color.White; })); } } #endregion private void Form4_Load(object sender, EventArgs e) { RowColSet(this.metroGridResult); this.metroGridResult.Rows.Add(new object[] { (this.metroGridResult.Rows.Count+1).ToString("00"), "3D" +Environment.NewLine+"高度1", "3.4673","mm","---" }); SetRowNA(metroGridResult, this.metroGridResult.Rows.Count-1); this.metroGridResult.Rows.Add(new object[] { (this.metroGridResult.Rows.Count+1).ToString("00"), "3D" + Environment.NewLine + "平面度1", "3.4673", "mm", "OK" }); SetRowOK(metroGridResult, this.metroGridResult.Rows.Count - 1); this.metroGridResult.Rows.Add(new object[] { (this.metroGridResult.Rows.Count + 1).ToString("00"), "3D" + Environment.NewLine + "最大最小高度差1", "3.4673", "mm", "NG" }); SetRowNG(metroGridResult, this.metroGridResult.Rows.Count - 1); this.metroGridResult.Rows[0].Selected = false; //默认不选中 } }
标签:index,Rows,常见,DataGridView,修改,metroGridResult,grid,new,index1 From: https://www.cnblogs.com/echo-efun/p/17921630.html