Reputation: 13
I have a dataGridView in a WinForm that is bound to a SQL database table. I want to refresh the data every two seconds or so, but maintain any row selection the user has made. Therefore I have set up a timer and implemented some code during the tick event. The code works fine to refresh the data, but I am struggling to get row selection to persist. What is happening is that when the timer ticks a second time, all rows in the dataGridView become selected and every tick after that. I am not sure what is causing this since my code to select the rows is inside a foreach loop and an if statement that should block this from happening. Here is my code for this function:
private void dataRefreshTimer_Tick(object sender, EventArgs e)
{
string selRows = null;
foreach (DataGridViewRow row in taskDataGrid.Rows)
{
if (row.Selected = true)
{
selRows += row.Cells[0].Value.ToString() + ",";
Console.WriteLine(selRows);
}
}
currenttasksTableAdapter.Fill(taskManagerDataSet.currenttasks);
currenttasksBindingSource.ResetBindings(false);
try
{
if (selRows != null)
{
foreach (DataGridViewRow row in taskDataGrid.Rows)
{
if (selRows.Contains(row.Cells[0].Value.ToString() + ","))
{
row.Selected = true;
}
}
}
} catch (Exception ex) { Console.WriteLine(ex); }
Can anyone tell me what I am missing that is causing all rows to become selected? Thanks for any advice!
Upvotes: 1
Views: 2745
Reputation: 205849
Can anyone tell me what I am missing that is causing all rows to become selected?
if (row.Selected = true)
Here you actually are selecting each row (=
is assignment operator). It should really be
if (row.Selected)
or
if (row.Selected == true)
Upvotes: 1