I have tried everything to make this change colors it seems. I have read through a lot of stackoverflow posts and have not found my answer. The colors changed before I made the datatable populate data from the database. After that they quit changing colors. Here is my code that is firing on CellFormatting event.
public ScheduleUserControl()
{
InitializeComponent();
dataGridView.CellClick += (s, e) => OnCellClick(e.ColumnIndex, e.RowIndex);
dataGridView.CellFormatting += (s, e) => CellFormating(e.ColumnIndex, e.RowIndex);
btnAll.Click += (s, e) => Filter(Filters.All);
btnHourly.Click += (s, e) => Filter(Filters.Hourly);
btnSalary.Click += (s, e) => Filter(Filters.Salary);
}
private void CellFormating(int c, int r)
{
var cell = dataGridView[c, r];
var tagObject = cell.Tag;
if (tagObject == null)
{
cell.Style.ForeColor = Color.Black;
cell.Style.BackColor = Color.White;
}
else
{
var tagType = tagObject.GetType();
if (tagType == typeof(DayOff))
{
var avail = (DayOff)tagObject;
if (avail != null)
{
cell.Style.ForeColor = Color.White;
cell.Style.BackColor = Color.Firebrick;
}
}
else
if (tagType == typeof(DayOffRequest))
{
var request = (DayOffRequest)tagObject;
if (request.Status == DayOffRequest.RequestStatus.Approved)
{
cell.Style.ForeColor = Color.Black;
cell.Style.BackColor = Color.SkyBlue;
}
else
if (request.Status == DayOffRequest.RequestStatus.Pending)
{
cell.Style.ForeColor = Color.Black;
cell.Style.BackColor = Color.LightGoldenrodYellow;
}
}
else
if (tagType == typeof(Shift))
{
cell.Style.ForeColor = Color.White;
cell.Style.BackColor = Color.Green;
}
}
}
Any help would be greatly appreciated it. Debugging and stepping through it says it executes the cell.style changes but the datagridview just doesn't show those changes.
You are creating recursion by changing cell style in CellFormating()
method and it can cause unexpected results. So try to set your cell styles in different event, for example in DataBindingComplete
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments