加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 条件DataGridView格式

发布时间:2020-12-15 04:01:10 所属栏目:百科 来源:网络整理
导读:我有一个DataGridView.我将它的.DataSource属性设置为我自己的对象的BindingList:一个BindingList IChessItem 然后我为它创建了一些列 DataGridViewTextBoxColumn descColumn = new DataGridViewTextBoxColumn(); descColumn.DataPropertyName = "Descripti
我有一个DataGridView.我将它的.DataSource属性设置为我自己的对象的BindingList:一个BindingList< IChessItem>

然后我为它创建了一些列

DataGridViewTextBoxColumn descColumn = new DataGridViewTextBoxColumn();
    descColumn.DataPropertyName = "Description";
    descColumn.HeaderText = "Description";
    descColumn.Width = 300;

    DataGridViewTextBoxColumn gameIDColumn = new DataGridViewTextBoxColumn();
    gameIDColumn.DataPropertyName = "GameID";
    gameIDColumn.HeaderText = "Game ID";
    gameIDColumn.Width = 60;

    dataGrid.Columns.Add(descColumn);
    dataGrid.Columns.Add(gameIDColumn);

我的问题是..我想根据我的BindingList的另一个字段中的数据来颜色一列GREEN.我该怎么做?

我不是真的要显示这个字段,我只想对其中的数据采取行动.

在我的情况下,IChessItem的一个字段显示该记录是否是新的,并且我想对datagridview中的其他字段进行颜色反映.

解决方法

您可以使用DataGridView的“CellFormatting”事件. DataGridViewCellFormattingEventArgs包含绑定当前单元格的行和列的索引.我希望我的代码示例对你有意义:
private void dataGridView1_CellFormatting(object sender,DataGridViewCellFormattingEventArgs e)
{
    // Compare the column to the column you want to format
    if (this.dataGridView1.Columns[e.ColumnIndex].Name == "ColumnName")
    {
        //get the IChessitem you are currently binding,using the index of the current row to access the datasource
        IChessItem item = sourceList[e.RowIndex];
        //check the condition
        if (item == condition)
        {
             e.CellStyle.BackColor = Color.Green;
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读