My form will eventually include 12 different datagridviews, one datagridview for each workstation of a scheduling application.
The intent is that users will be able to drag/drop employees from one workstation to the next.
Employee rows will have different colors, depending on employee rank. This will help the scheduler manage how many employees of specific ranks are allowed on a given work station at the same time.
Currently, I've built the Unassigned Employees dataGridView and have played with the needed routines to update station assignments and row coloring.
Now for my problem.
At this point I am formatting row color with a switch statement. The statement follows:
foreach (DataGridViewRow row in Unassigned_dataVW.Rows) { string rnkClr = row.Cells[16].Value.ToString(); // rnkClr is being pulled right from a color field in the datasource.
//The field is associated with employee job title
switch (rnkClr) { case "Red": row.DefaultCellStyle.ForeColor = Color.Red; break; case "Blue": row.DefaultCellStyle.ForeColor = Color.Blue; break; case "Green": row.DefaultCellStyle.ForeColor = Color.DarkGreen; break; default: row.DefaultCellStyle.ForeColor = Color.Black; break; } }
This works well enough for formatting the Unassigned DataGridView on load of the form.
Just so you know, each employee job title in the employee table is affiliated with a color. So I can pull the needed color right from the data source.
The switch would be fine, if all I had to do is format the DataGridView on load of the form. But, whenever I drag someone out of the grid (to another workstation) and refresh the grid, all the formatting goes away. And when this problem is multiplied times the eventual 12 workstations, you can see why I've concerns with using a switch. So... I want to come up with a more efficient way of formatting the DataGridView rows.
The first thought I have about increased efficiency is simply to feed the color variable,rnkClr, to the formatting command line as follows...
instead of:
row.DefaultCellStyle.ForeColor = Color.Red;
Do the folowing:
row.DefaultCellStyle.ForeColor = Color.rnkClr;
But when I do this I get all kinds of error messages. The processor does not like me using a variable for the color portion of the above chain.
Does anyone have any ideas on how to push a variable into a color formatting script string?
Alternately - it would be possible to build a custom function, and use the same switch over and over again. But, I'd have to figure out a way to feed the name of my DataGridView to the function. Again I ran into problems with trying to assign,Unassigned_dataVW.Rows, (for example) to a variable.
I'm open minded about which approach to use, all I want is something more efficient than copying and pasting the same switch routine into the handling of 12 different DataGridViews.
Thanks in Advance - Pavilion