Can't change datagridview cell color when using a datasource

前端 未结 2 1236
南旧
南旧 2021-01-05 02:35

I\'ve got an interesting issue. I am trying to use a datatable as a data source for a datagridview. I want to color some of the cells of the table to indicate various things

2条回答
  •  执念已碎
    2021-01-05 03:09

    This Is something I have implemented recently, I dont know if it will help??

    private void dgvOutstandingReports_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
                {
                    int colIndex = e.ColumnIndex;
                    int rowIndex = e.RowIndex;
    
    
                    if (rowIndex >= 0 && colIndex >= 0)
                    {
                        DataGridViewRow theRow = dgvOutstandingReports.Rows[rowIndex];
    
    
                        if (theRow.Cells[colIndex].Value.ToString() == "Daily Report")
                        {
                            theRow.DefaultCellStyle.BackColor = Color.LightYellow;
                        }
                        else if (theRow.Cells[colIndex].Value.ToString() == "Monthly Report")
                        {
                            theRow.DefaultCellStyle.BackColor = Color.LightGray;
                        }
                        else if (theRow.Cells[colIndex].Value.ToString() == "SMP Report")
                        {
                            theRow.DefaultCellStyle.BackColor = Color.Snow;
                        }
                        else if (theRow.Cells[colIndex].Value.ToString() == "Weekly Report")
                        {
                            theRow.DefaultCellStyle.BackColor = Color.Pink;
                        }
                        else if (theRow.Cells[colIndex].Value.ToString() == "Hourly Report")
                        {
                            theRow.DefaultCellStyle.BackColor = Color.LightSteelBlue;
                        }
                    }
                }
    

提交回复
热议问题