简体   繁体   中英

Preventing Edits to specific rows in DataGridView

I want to prevent the user from editing or deleting the first three rows of a datagridview.

How can I do this?

Solution:

private void dataGridView3_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) {
    if (e.RowIndex < 3) {
        e.Cancel = true;
    }
}

private void dataGridView3_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e) {
    if (e.Row.Index < 3) {
        e.Cancel = true;
    }
}

One way to do it is to capture _CellBeginEdit event, check if any edits on the targeted row are allowed, and cancel event if no edits allowed:

private void dataGridViewIndexesSpecs_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) {

        if (e.RowIndex <= 3)
            e.Cancel = true;

    }

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