I am looking for a way to enable multiple row selection in a DataGridView-Control, but disable multiple cell selection.
What I have tried so far:
- DataGridView.MultiSelect = true allows you to select multiple rows and cells
- ClearSelection () in the DataGridView_CellMouseClick-Event and re-selecting the last selected cell does not look very nice (you see that the old cell deselects and then the new cell is selected; SuspendLayout () and ResumeLayout () do not help)
- DataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect is not an option: if the user clicks on a cell, it should select only this cell
This is for the export function: the user should be able to export the selected lines to a file, but in general he should not select more than one cell (for copy and paste, etc.).
Hi,
Inno
----- [UPDATE] -----
Here is my implementation. Works great (comments removed for compactness):
using System.Windows.Forms;
namespace YourAmazingNamespace
{
public partial class SpecialSelectDataGridView: DataGridView
{
public SpecialSelectDataGridView()
{
InitializeComponent();
}
protected override void SetSelectedCellCore(int columnIndex, int rowIndex, bool selected)
{
ResetSelectedCells();
base.SetSelectedCellCore(columnIndex, rowIndex, selected);
}
void ResetSelectedCells()
{
foreach (DataGridViewCell cell in SelectedCells) {
base.SetSelectedCellCore(cell.ColumnIndex, cell.RowIndex, false);
}
}
}
}
Several rows are selected using MultiSelect = true (the default value), and the selected cells are currently reset by calling ResetSelectedCells () before choosing a new one.
Hth, thanks and greet
Inno
source
share