C # DataGridView how to highlight current list <Action>
I would like to highlight the method that is currently being implemented in the datagridview.
This is the list:
public List<Action> functions = new List<Action>();
And inside I have methods. adding eg.
functions.Add(waypoint1);
My datagridview method after add method looks like this:
And I just want hightlight current waypoint.
to run the list I am using:
foreach (Action func in functions)
{
func();
}
For example, when the func()
number 2 is only activated, highlight the second row. When the func()
number 3 is only activated, highlight the 3rd row. I searched the forum but didn't find a solution. Please be patient for newbies. Thank.
EDIT 1: Added foreach loop code to backgroundworker as you want.
private void DoWork_backgroundworker(object sender, DoWorkEventArgs e)
{
while (true)
{
foreach (Action func in functions)
{
func();
}
}
}
And the button to run:
private void metroButton7_Click(object sender, EventArgs e)
{
DoWork_backgroundworker.RunWorkerAsync();
}
source to share
If you feel good that you are using the property Selected
to allocate, you can use a regular for loop (which will give you the index) and index the rows that belong to the elements. Just set a property of Selected
this Row
- true
:
for (int i = 0; i < functions.Count; i++)
{
//select the row before action is started
dataGridView1.Rows[i].Selected = true;
// execute your action
functions[i]();
//unselect the row when job is finished
dataGridView1.Rows[i].Selected = false;
}
EDIT:
If you want to change the highlight / highlight color use the property dataGridView1.RowsDefaultCellStyle.SelectionBackColor
. Place this line before the for loop:
dataGridView1.RowsDefaultCellStyle.SelectionBackColor = System.Drawing.Color.Black;
source to share