You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何设置DataGridView选中行字体?是否支持属性配置?

DataGridView选中行字体修改方案

DataGridView本身没有内置属性能直接单独修改选中行的字体样式,这个需求得通过手动编码来实现。

最常用的方式是利用CellFormatting事件——这个事件会在单元格准备显示内容时触发,刚好可以根据行的选中状态动态调整字体样式。给你个实用的示例代码:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    var dataGridView = sender as DataGridView;
    if (dataGridView.Rows[e.RowIndex].Selected)
    {
        // 将选中行的字体设置为粗体,你也可以换成其他FontStyle组合
        e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Bold);
    }
}

小提醒:别忘了在窗体初始化时(比如构造函数里,或者通过设计器的事件面板)把这个方法绑定到dataGridView1的CellFormatting事件上,不然代码不会生效哦。

内容的提问来源于stack exchange,提问作者daddycool

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:02:47