Reputation: 1332
Is there a way to let the user only select one row in a datagrid
For example that should the user can do:
That he should can't do:
i saw this but it dont see how to use that for my problem: https://stackoverflow.com/a/3072929/1764978
I wanted to use the SelectionChanged
on the DataGrid, but it doesnt trigger when i change selected cells, just only SelectedCellsChanged
and there is no Handled
-Property
When im using ethicallogics solution i got:
I select a full row.. thats not what i want
Upvotes: 3
Views: 2616
Reputation: 818
int _selectedRow = -1;
int _selectedColumn = -1;
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
switch (dataGridView1.SelectedCells.Count)
{
case 0:
// store no current selection
_selectedRow = -1;
_selectedColumn = -1;
return;
case 1:
// store starting point for multi-select
_selectedRow = dataGridView1.SelectedCells[0].RowIndex;
_selectedColumn = dataGridView1.SelectedCells[0].ColumnIndex;
return;
}
foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
{
if (cell.RowIndex == _selectedRow)
{
if (cell.ColumnIndex != _selectedColumn)
{
_selectedColumn = -1;
}
}
else if (cell.ColumnIndex == _selectedColumn)
{
if (cell.RowIndex != _selectedRow)
{
_selectedRow = -1;
}
}
// otherwise the cell selection is illegal - de-select
else cell.Selected = false;
}
}
Upvotes: 1
Reputation: 2593
This might be optimized, but this worked for me.
It keeps track of the first selected item, meaning the row, and if there is a selection outside of that row, it removes the whole selection (and replaces it with the newly selected cell). It could be enhanced, probably, to restore the initial selection, but my usecase didn't need that.
Anyway, see if this works for you.
private object selectedItem;
private void DataGrid_OnSelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
var dg = (sender as DataGrid);
if (selectedItem == null)
selectedItem = e.AddedCells.First().Item;
var allInSameRow = e.AddedCells.All(info => info.Item == selectedItem);
if (!allInSameRow)
{
dg.SelectedCells.Clear();
selectedItem = null;
}
}
Upvotes: 2
Reputation: 1051
If my guess is correct : you want is
1) Allow the user to select one cell from the whole grid.
2) After first selection you want the user not to select any cells from any other row.
3) If user wants to select multiple cells then he is allowed to do so but in the same row in which he has selected the first cell.
I don't know the exact solution or I don't have any code to guide you.
But If you can then disable other rows at the time of first selection.
Upvotes: 1
Reputation: 1
I am currently working on something like that and I am creating string variables for every record that I want to take. I use this line of code:
string somename = ((DataRowView)DataGridName.SelectedItem).Row["ColumnName"].ToString();
this code will take only one cell (it depends of the ColumnName
which record will be stored in the variable) from the row the user selected.
Upvotes: 0
Reputation: 12315
Try this
<DataGrid SelectionMode="Single" SelectionUnit="Cell"
Upvotes: 0