当前位置: 首页 > 编程语言 > C#

如何设置DataGridView单元格的字体为特定颜色?分享

时间:2023-04-10 17:20:33 C#

DataGridViewcell的字体如何设置为特定的颜色?此代码用于使单元格的背景变为蓝色:DataGridViewRowdgvr=dataGridViewLifeSchedule.Rows[rowToPopulate];dgvr.Cells[colName].Style.BackColor=Color.Blue;dgvr.Cells[colName].Style.ForeColor=颜色.黄色;...但是ForeColor的效果不是我预期/希望的:字体颜色仍然是黑色,而不是黄色。如何将字体颜色设置为黄色?你可以这样做:dataGridView1.SelectedCells[0].Style=newDataGridViewCellStyle{ForeColor=Color.Yellow};您还可以在该单元格样式构造函数中设置任何样式设置(例如字体)。如果你想设置特定列的文本颜色,你可以这样做:dataGridView1.Columns[colName].DefaultCellStyle.ForeColor=Color.Yellow;dataGridView1.Columns[0].DefaultCellStyle.BackColor=Color.Blue;更新因此,如果您想要根据单元格中的值进行着色,像这样的方法可以解决问题:ColumnIndex].Value!=null&&!string.IsNullOrWhiteSpace(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString())){dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style=newDataGridViewCellStyle{ForeColor=Color.Orange,BackColor=Color.Blue};}else{dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style=dataGridView1.DefaultCellStyle;为避免性能问题(与DataGridView数据量相关),请使用DataGridViewDefaultCellStyle和DataGridViewCellInheritedStyle。参考:http://msdn.microsoft.com/en-us/library/ha5xt0d9.aspx您可以使用DataGridView.CellFormatting根据先前的代码约束绘制受影响的单元格。在这种情况下,您可能需要覆盖DataGridViewDefaultCellStyle。//编辑回复您对@itsmatt的评论。如果要给所有的行/单元格填充样式,需要以下内容:以上是C#学习教程:如何设置DataGridView单元格的字体为特定的颜色?如果分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注——//设置所有单元格的选择背景色。dataGridView1.DefaultCellStyle.SelectionBackColor=Color.White;数据网格视图1。DefaultCellStyle.SelectionForeColor=Color.Black;//设置RowHeadersDefaultCellStyle.SelectionBackColor使其默认//值不会覆盖DataGridView.DefaultCellStyle.SelectionBackColor.dataGridView1.RowHeadersDefaultCellStyle.SelectionBackColor=Colorall.Empty;和交替行。//交替行的值覆盖所有行的值。dataGridView1.RowsDefaultCellStyle.BackColor=Color.LightGray;dataGridView1.AlternatingRowsDefaultCellStyle.BackColor=Color.DarkGray;侵权请点击右侧联系管理员删除。如需转载请注明出处: