In one of my C# Winform, I have a DataGridView that I update when the user presses a refresh button.
The problem is that the selected row is lost in the process.
I would like to be able to do the following:
private void function Refresh()
{
UpdateBegin(); // Keep the selected row in memory
Update();
UpdateEnd(); // Apply the selected row to the DataGridView
}
Here is the Update function. It updates the data source, clears all the columns and bring back those that are required with the proper header text:
private void Update()
{
allItem = DataRepository.LotProvider.GetByIdProduit(detail.IdProduit)
dataGridView1.DataSource = allItem;
dataGridView1.Columns.Clear();
// Get a dictionary of the required column ID / shown text
Dictionary<string, string> dictionary = InitDisplayedFields();
foreach (KeyValuePair<string, string> column in dictionary)
// If the grid does not contain the key
if (!dataGridView1.Columns.Contains(column.Key))
{
// Add the column (key-value)
int id = dataGridView1.Columns.Add(column.Key, column.Value);
// Bind the property
dataGridView1.Columns[id].DataPropertyName = column.Key;
}
}
However, the selected rows property of my DataGridView is Read-only.
Is there a workaround to this ?