简体   繁体   English

如果选择了多个 datagridview 行,如何防止选择“新”datagridview 行中的单元格?

[英]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.不要问为什么,但我需要一种方法来防止用户在选择了多行时在“新”datagridview 行中输入单元格。

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! PS 不要尝试从 selectionchanged 事件中编辑当前单元格,已经尝试过了!

Thanks,谢谢,

Isaac艾萨克

I have an idea that the DataGridView control has a RowState property.我知道 DataGridView 控件具有 RowState 属性。 When your row is selected, check the state.选择您的行后,检查 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:使用 CellStateChanged 事件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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