Programmatically select multiple cells in DataGridView C# / VB.NET application
-
Hi, I'm creating a user control that displays a DataGridView control. I want to be able to programmatically select multiple cells/rows. So i made this function:
public bool SetSelectedValues(string ColumnName, List<object> CellValues)
{
bool FoundRows = false;for (int ColumnIndex = 0; ColumnIndex < gridData.Columns.Count; ColumnIndex++) { if (gridData.Columns\[0\].Name == ColumnName) { gridData.ClearSelection(); for (int RowIndex = 0; RowIndex < gridData.Rows.Count; RowIndex++) { foreach (object CellValue in CellValues) { if (gridData.Rows\[RowIndex\].Cells\[ColumnIndex\].Value.ToString() == CellValue.ToString()) { gridData.Rows\[RowIndex\].Cells\[ColumnIndex\].Selected = true; gridData.CurrentCell = gridData.Rows\[RowIndex\].Cells\[ColumnIndex\]; FoundRows = true; } } } break; } } return FoundRows;
}
It doesn't work, only the last cell gets selected, because i cannot select multiple cells in the CurrentCell. How can i solve this? Regards, Willem
-
Hi, I'm creating a user control that displays a DataGridView control. I want to be able to programmatically select multiple cells/rows. So i made this function:
public bool SetSelectedValues(string ColumnName, List<object> CellValues)
{
bool FoundRows = false;for (int ColumnIndex = 0; ColumnIndex < gridData.Columns.Count; ColumnIndex++) { if (gridData.Columns\[0\].Name == ColumnName) { gridData.ClearSelection(); for (int RowIndex = 0; RowIndex < gridData.Rows.Count; RowIndex++) { foreach (object CellValue in CellValues) { if (gridData.Rows\[RowIndex\].Cells\[ColumnIndex\].Value.ToString() == CellValue.ToString()) { gridData.Rows\[RowIndex\].Cells\[ColumnIndex\].Selected = true; gridData.CurrentCell = gridData.Rows\[RowIndex\].Cells\[ColumnIndex\]; FoundRows = true; } } } break; } } return FoundRows;
}
It doesn't work, only the last cell gets selected, because i cannot select multiple cells in the CurrentCell. How can i solve this? Regards, Willem
I don't the code here setting MultiSelect as true. See if you have done that. Plus, not sure about CurrentCell thing. Why are you setting it in first place?
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
-
Hi, I'm creating a user control that displays a DataGridView control. I want to be able to programmatically select multiple cells/rows. So i made this function:
public bool SetSelectedValues(string ColumnName, List<object> CellValues)
{
bool FoundRows = false;for (int ColumnIndex = 0; ColumnIndex < gridData.Columns.Count; ColumnIndex++) { if (gridData.Columns\[0\].Name == ColumnName) { gridData.ClearSelection(); for (int RowIndex = 0; RowIndex < gridData.Rows.Count; RowIndex++) { foreach (object CellValue in CellValues) { if (gridData.Rows\[RowIndex\].Cells\[ColumnIndex\].Value.ToString() == CellValue.ToString()) { gridData.Rows\[RowIndex\].Cells\[ColumnIndex\].Selected = true; gridData.CurrentCell = gridData.Rows\[RowIndex\].Cells\[ColumnIndex\]; FoundRows = true; } } } break; } } return FoundRows;
}
It doesn't work, only the last cell gets selected, because i cannot select multiple cells in the CurrentCell. How can i solve this? Regards, Willem
Found the solution myself, the setting of selected cell's is only effective after the datasource is bound to the control. This is not done jet when you are in the Form_Load, so you have to wait for it. This is my solution:
private string Selection_ColumnName = null;
private List<object> Selection_CellValues = null;public void SetSelectedValues(string ColumnName, List<object> CellValues)
{
Selection_ColumnName = ColumnName;
Selection_CellValues = CellValues;
gridData.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(gridData_DataBindingComplete);
}
private void gridData_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
bool CellFound = false;for (int ColumnIndex = 0; ColumnIndex < gridData.Columns.Count; ColumnIndex++) { if (gridData.Columns\[0\].Name == Selection\_ColumnName) { gridData.ClearSelection(); for (int RowIndex = 0; RowIndex < gridData.Rows.Count; RowIndex++) { foreach (object CellValue in Selection\_CellValues) { try { if (gridData.Rows\[RowIndex\].Cells\[ColumnIndex\].Value.ToString() == CellValue.ToString()) { gridData.Rows\[RowIndex\].Cells\[ColumnIndex\].Selected = true; if (!CellFound) gridData.CurrentCell = gridData.SelectedCells\[0\]; CellFound = true; } } catch { } } } break; } } gridData.DataBindingComplete -= new DataGridViewBindingCompleteEventHandler(gridData\_DataBindingComplete);
}