Karl_Schuhmann
Karl_Schuhmann

Reputation: 1332

DataGrid Select Cells in only one Row

Is there a way to let the user only select one row in a datagrid

For example that should the user can do:

enter image description here

That he should can't do:

enter image description here

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

Edit

When im using ethicallogics solution i got: enter image description here

I select a full row.. thats not what i want

Upvotes: 3

Views: 2616

Answers (5)

Mihir Joshi
Mihir Joshi

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

AkselK
AkselK

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

Khushi
Khushi

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

Martin
Martin

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

yo chauhan
yo chauhan

Reputation: 12315

Try this

<DataGrid SelectionMode="Single" SelectionUnit="Cell"

Upvotes: 0

Related Questions