简体   繁体   English

如何在设计时为 DataGridView 中的列设置样式

[英]How to set style in for a column in DataGridView at design time

There's a question about changing cell background color in a DataGridView programatically有一个关于以编程方式更改 DataGridView 中的单元格背景颜色的问题

But is there any way to set the background color for a particular column using the WinForms Designer ?但是有没有办法使用 WinForms Designer为特定列设置背景颜色?

  1. Right click the DataGridView and select Edit Columns右键单击 DataGridView 和 select 编辑列

    编辑列

  2. Select the DefaultCellStyle Property and click on the Style Wizard Select DefaultCellStyle 属性并点击样式向导

    默认单元格样式

  3. In the CellStyle Builder window, you can set whatever properties you'd like, including BackColor在 CellStyle Builder window 中,您可以设置任何您想要的属性,包括BackColor

    细胞样式生成器

What this will do is set the property inside of the form.designer.cs file during InitializeComponent()这将在InitializeComponent()期间设置form.designer.cs文件中的属性

private void InitializeComponent()
{
    /// ...
    this.ShadedColumn.DefaultCellStyle.BackColor = Color.Silver;
    /// ...
}

* where this.ShadedColumn is the name of the DataGridViewTextBoxColumn control you added * 其中this.ShadedColumn是您添加的DataGridViewTextBoxColumn控件的名称

Any code in InitializeComponent will execute during the designer view, and technically you can update most styles directly here, but it's advised to use the GUI form builder to make any changes against this file InitializeComponent中的任何代码都将在设计器视图期间执行,从技术上讲,您可以直接在此处更新大多数 styles,但建议使用 GUI 表单构建器对此文件进行任何更改

Further Reading : Set Default Cell Styles and Data Formats for the Windows Forms DataGridView Control Using the Designer进一步阅读使用设计器设置默认单元格 Styles 和 Windows Forms DataGridView 控件的数据格式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM