gpt4 book ai didi

c# - 从给定索引的 DataGridView 中删除一行

转载 作者:太空宇宙 更新时间:2023-11-03 11:52:44 25 4
gpt4 key购买 nike

我的 DataGridView 是单行选择,并且有一个 rowEnter 事件,每次所选行更改时我都会在其中获取行索引。

    private void rowEnter(object sender, DataGridViewCellEventArgs e)
{
currentRowIndex = e.RowIndex;
}

当我按下删除按钮时,我使用相同的索引来删除行

            myDataSet.Avaliado.Rows[currentRowIndex].Delete();
avaliadoTableAdapter.Update(myDataSet.Avaliado);

如果 DataGridView 中没有列被排序,它工作正常,否则会出现错误。 DataGridView 中的行索引对应的数据集中的行索引应该如何获取?

最佳答案

您无需在每次选择新行时都获取当前行索引。尝试这样的事情:

if (_Grid.SelectedRows.Count <= 0) { return; } // nothing selected

DataGridViewRow dgvRow = _Grid.SelectedRows[0];

// assuming you have a DataTable bound to the DataGridView:
DataRowView row = (DataRowView)dgvRow.DataBoundItem;
// now go about deleting the row however you would do that.

如果您将某种其他类型的数据类型绑定(bind)到网格的每一行,只需将 DataGridViewRow.DataBoundItem 转换为您的数据类型即可。

关于c# - 从给定索引的 DataGridView 中删除一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1662938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com