条件DataGridView格式 [英] Conditional DataGridView Formatting

查看:100
本文介绍了条件DataGridView格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGridView。我将它的.DataSource属性设置为我自己的对象的BindingList:一个 BindingList< IChessItem>



然后为它创建了一些列。

  DataGridViewTextBoxColumn descColumn = new DataGridViewTextBoxColumn(); 
descColumn.DataPropertyName =描述;
descColumn.HeaderText =描述;
descColumn.Width = 300;

DataGridViewTextBoxColumn gameIDColumn = new DataGridViewTextBoxColumn();
gameIDColumn.DataPropertyName =GameID;
gameIDColumn.HeaderText =游戏ID;
gameIDColumn.Width = 60;

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

我的问题是..我想根据另一个字段中的数据我的BindingList)。我该怎么做?



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



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

解决方案

可以使用DataGridView的CellFormatting事件。 DataGridViewCellFormattingEventArgs包含绑定当前单元格的行和列的索引。我希望我的代码示例对你有意义:

  private void dataGridView1_CellFormatting(object sender,DataGridViewCellFormattingEventArgs e)
{
//将列与要格式化的列
进行比较if(this.dataGridView1.Columns [e.ColumnIndex] .Name ==ColumnName)
{
/ /获取当前绑定的IChessitem,使用当前行的索引访问数据源
IChessItem item = sourceList [e.RowIndex];
//检查条件
if(item == condition)
{
e.CellStyle.BackColor = Color.Green;
}
}
}


I have a DataGridView. I set its .DataSource prop to be an BindingList of my own objects: a BindingList<IChessItem>

I then created some columns for it..

    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);

My question is.. I want to color one of the columns GREEN based upon data in another field of my BindingList). How can I do this?

I don't really have to show this field, I just want to act upon the data in it..

in my case, one of the fields of IChessItem shows whether the record is new, and I want to color the other fields in the datagridview to reflect that.

解决方案

You can use the 'CellFormatting' event of the DataGridView. The DataGridViewCellFormattingEventArgs contains indexes of the row and the column of the current cell as it is being bound. I hope my code example makes some sense to you:

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;
        }
    }
}

这篇关于条件DataGridView格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆