简体   繁体   中英

How can I prevent cells in the “new” datagridview row from being selected if there are multiple datagridview rows selected?

Don't ask why, but I need a way to prevent the user from entering a cell in the 'new' datagridview row WHILE they've got multiple rows selected.

Currently, the cell in the first column of the row that the mouse is hovering over during the click and drag is being selected. If you click on the cell, then the rows aren't selected anymore, so you can't use any cell click events or anything.

Any suggestions are welcome.

PS don't try editing the currentcell from the selectionchanged event, already tried that!

Thanks,

Isaac

I have an idea that the DataGridView control has a RowState property. When your row is selected, check the state. I'm not sure what the states are (if I'm even in the ballpark)... anyway, you may be able to check that route.

I'm looking for more info...

Okay... think I found a decent way to do this:

void DataGridView1_RowsAdded (object sender, DataGridViewRowsAddedEventArgs e)
{
    currentNewRow = e.RowIndex;
}

void DataGridView1_CellMouseClick(Object sender, DataGridViewCellMouseEventArgs e)
{
    if (e.RowIndex == currentNewRow)
    {
        // don't add to Selection
    }
} 

Use CellStateChanged event:

private void dataGridView_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e) {
            if (e.Cell.OwningRow.IsNewRow && dataGridView.SelectedRows.Count > 1)
                e.Cell.Selected = false;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM