【问题标题】:How can I fake an "AlternatingColumnsDefaultCellStyle" for the DataGridView?如何为 DataGridView 伪造“交替列 DefaultCellStyle”?
【发布时间】:2012-08-24 00:11:54
【问题描述】:

DataGridView 有一个“AlternatingRowsDefaultCellStyle”,它就像一个冠军。但我需要相反的分界/装饰。是否有一个属性可以使列背景的颜色与默认颜色不同,还是我需要为 CellFormatting 事件编写代码,或者...?

更新

好吧,这看起来很奇怪:

使用此代码,来自 Ascension:

dataGridView1.Columns[i].ItemStyle.BackColor = Color.Blue;

...我明白了:

“System.Windows.Forms.DataGridViewColumn”不包含“ItemStyle”的定义,并且找不到接受“System.Windows.Forms.DataGridViewColumn”类型的第一个参数的扩展方法“ItemStyle”(您是否缺少 using 指令或程序集引用?)

但是,如果我只输入第一部分 (dataGridView1.Columns[i].),我会通过 Intellisense 获得“ItemStyle”作为选择的有效选项,但是当它变为红色时我愿意(可能是 Resharper 效果)。之后的点允许选择 BackColor 属性,然后不是红色。

为什么会出现这种奇怪的行为,是否有解决方法?

并且:红色的智能感知项目是否表示无法访问,如果是,为什么会显示它们? “预告”成员有什么价值吗?

更新 2

这很有效(受 Ascension 的启发,所以我给了他/她正确的答案):

DataGridViewCell cell = new DataGridViewTextBoxCell();
cell.Style.BackColor = Color.Wheat;
dataGridView1.Columns[i].CellTemplate = cell;

【问题讨论】:

    标签: c# winforms datagridview datagridviewcolumn


    【解决方案1】:

    试试这个:

    DataGridView Property -> Misc -> Columns ... -> ItemStyle -> BackColor
    

    或下面的脚本

    protected void Page_Load(object sender, EventArgs e)
            {
                for (int i = 0; i < GridView1.Columns.Count; i++)
                {
                    if (i % 2 == 0)
                    {
                        GridView1.Columns[i].ItemStyle.BackColor = Color.Red;
                    }
                    else
                    {
                        GridView1.Columns[i].ItemStyle.BackColor = Color.Blue; ;
                    }
                }
            }
    

    【讨论】:

    • 对不起,我没有用DataGridView是GridView。
    【解决方案2】:

    您是否尝试过为每列设置颜色? DataGridView 属性 -> 杂项 -> 列(打开集合) -> DefaultCellStyle

    【讨论】:

    • 我会在我上班的时候捷克语出来;但是,我没有提到我的 DGV 中的列和行是动态创建的。
    猜你喜欢
    • 2019-01-01
    • 1970-01-01
    • 2021-07-16
    • 2010-11-15
    • 1970-01-01
    • 1970-01-01
    • 2012-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多