Conditional DataGridView Formatting

后端 未结 2 340
再見小時候
再見小時候 2020-11-29 11:37

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

I then created some columns fo

相关标签:
2条回答
  • 2020-11-29 11:38

    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;
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-29 11:57

    You may populate data in your DataGridView using any loop or datasource. Then for each DataGridViewRow in DataGridView1.Rows----

    Chk the ref value ypu want to chk and then set DataGridviewCell[index].style.backColor property.

    0 讨论(0)
提交回复
热议问题