特定 dataGridView 行中的粗体字体
Bold font in specific dataGridView rows
我正在寻找并尝试不同的解决方案来解决我的问题,但 none 有效。如果具有特定索引的单元格具有特定值,我需要在一行中的所有单元格中设置粗体。我做错了什么?
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[14].Value.ToString() == "Yes")
{
row.DefaultCellStyle.Font = new Font(dataGridView1.Font, FontStyle.Bold);
}
}
编辑:
对不起,是我的错。条件语句一直检查错误的单元格,我的代码运行完美。
因此,如果 DataGridViewCell 具有特定值,您希望以特定字体打印该值。
用于显示 DataGridViewCell 值的字体位于单元格的 DataGridViewCellStyle 中。
因此我们可以使您的要求更通用:如果 DataGridViewCell 具有特定值,您希望单元格使用特定的 DataGridViewCellStyle 来显示其值。
您可以使用两种方法:
- 每当单元格更改值时,如果它已获得“特定值”,则为该单元格赋予特殊样式,每当它更改为另一个值时,让它使用默认样式。
- 每当必须格式化单元格的值时,请格式化单元格。
对我来说,第一种方法似乎是最简单的方法。大多数单元格不会经常更改值,所以在我看来,仅在值更改时执行此操作更有效,而不是每次都必须格式化单元格的值。
关于 DataGridViewCellStyles
一个 DataGridViewCell 可以有一个 DataGridViewCellStyle。如果单元格有样式,则使用此样式。如果它没有样式(值等于 null),它将使用 DataGridViewColumn 的 DefaultCellStyle。如果该列也没有样式,则使用 DataGridView.DefaultCellStyle。要让 Cell 或 Column 使用父级的单元格样式,只需将 (Default)CellStyle 设为 null。实际使用的样式,勾选CellStyle后,列的单元格样式等在DataGridViewCell.InheritedStyle
因此,如果您希望某个单元格使用非默认的 DataGridViewCellStyle,只需分配您想要的单元格样式即可。如果您想再次使用该列的默认单元格样式,只需分配 null。
每当值更改时更改单元格样式
const string specialDisplayValue = "yes";
IEqualityComparer<string> cellTextcomparer = StringComparer.CurrentCultureIgnoreCase;
void OnCellValueChanged(DataGridViewCell cell)
{
string displayedCellValue = this.GetDisplayedValue(cell);
if (cellTextComparer.Equals(displayedCellValue, specialDisplayValue))
{
// special text shown, use special style with special font:
cell.Style = new DataGridViewCellStyle(cell.Column.InheritedStyle);
cell.Style.Font = new Font(cell.Style.Font, FontStyle.Bold);
}
else
{
// other value, use the default style:
cell.Style = null; // the style from the column will be used.
}
}
如果Columns的InheritedStyles不会改变,考虑在构造函数中只定义一次特殊样式。然后你只需要决定是否分配 null 或特殊样式。如果所有列都使用相同的样式,您只需使用一种特殊样式,以 DataGridView.CellStyle 作为模板创建。
自己格式化单元格
使用事件:
dataGridView1.CellFormatting += dataGridView1_CellFormatting;
private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
string displayValue = e.Value.ToString();
if (cellTextComparer.Equals(displayValue, specialDisplayValue))
{
// special value, special font
e.CellStyle.Font = new Fone(e.CellStyle.Font, FontStyle.Bold);
}
// else: different value, don't change the font
}
注意:每次必须格式化单元格时都会调用此方法,即使单元格的值未更改也是如此。因此效率较低。
我正在寻找并尝试不同的解决方案来解决我的问题,但 none 有效。如果具有特定索引的单元格具有特定值,我需要在一行中的所有单元格中设置粗体。我做错了什么?
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[14].Value.ToString() == "Yes")
{
row.DefaultCellStyle.Font = new Font(dataGridView1.Font, FontStyle.Bold);
}
}
编辑: 对不起,是我的错。条件语句一直检查错误的单元格,我的代码运行完美。
因此,如果 DataGridViewCell 具有特定值,您希望以特定字体打印该值。
用于显示 DataGridViewCell 值的字体位于单元格的 DataGridViewCellStyle 中。
因此我们可以使您的要求更通用:如果 DataGridViewCell 具有特定值,您希望单元格使用特定的 DataGridViewCellStyle 来显示其值。
您可以使用两种方法:
- 每当单元格更改值时,如果它已获得“特定值”,则为该单元格赋予特殊样式,每当它更改为另一个值时,让它使用默认样式。
- 每当必须格式化单元格的值时,请格式化单元格。
对我来说,第一种方法似乎是最简单的方法。大多数单元格不会经常更改值,所以在我看来,仅在值更改时执行此操作更有效,而不是每次都必须格式化单元格的值。
关于 DataGridViewCellStyles
一个 DataGridViewCell 可以有一个 DataGridViewCellStyle。如果单元格有样式,则使用此样式。如果它没有样式(值等于 null),它将使用 DataGridViewColumn 的 DefaultCellStyle。如果该列也没有样式,则使用 DataGridView.DefaultCellStyle。要让 Cell 或 Column 使用父级的单元格样式,只需将 (Default)CellStyle 设为 null。实际使用的样式,勾选CellStyle后,列的单元格样式等在DataGridViewCell.InheritedStyle
因此,如果您希望某个单元格使用非默认的 DataGridViewCellStyle,只需分配您想要的单元格样式即可。如果您想再次使用该列的默认单元格样式,只需分配 null。
每当值更改时更改单元格样式
const string specialDisplayValue = "yes";
IEqualityComparer<string> cellTextcomparer = StringComparer.CurrentCultureIgnoreCase;
void OnCellValueChanged(DataGridViewCell cell)
{
string displayedCellValue = this.GetDisplayedValue(cell);
if (cellTextComparer.Equals(displayedCellValue, specialDisplayValue))
{
// special text shown, use special style with special font:
cell.Style = new DataGridViewCellStyle(cell.Column.InheritedStyle);
cell.Style.Font = new Font(cell.Style.Font, FontStyle.Bold);
}
else
{
// other value, use the default style:
cell.Style = null; // the style from the column will be used.
}
}
如果Columns的InheritedStyles不会改变,考虑在构造函数中只定义一次特殊样式。然后你只需要决定是否分配 null 或特殊样式。如果所有列都使用相同的样式,您只需使用一种特殊样式,以 DataGridView.CellStyle 作为模板创建。
自己格式化单元格
使用事件:
dataGridView1.CellFormatting += dataGridView1_CellFormatting;
private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
string displayValue = e.Value.ToString();
if (cellTextComparer.Equals(displayValue, specialDisplayValue))
{
// special value, special font
e.CellStyle.Font = new Fone(e.CellStyle.Font, FontStyle.Bold);
}
// else: different value, don't change the font
}
注意:每次必须格式化单元格时都会调用此方法,即使单元格的值未更改也是如此。因此效率较低。