gpt4 book ai didi

c# - 从绑定(bind)到 DataGridView 的 DataTable 中删除最后一个 DataRow 时如何防止出错?

转载 作者:太空宇宙 更新时间:2023-11-03 13:38:50 27 4
gpt4 key购买 nike

在我的 WinForms 应用程序中,按下按钮会触发以下逻辑:

    private void ExecuteSelectedConsoleCommand()
{
var commandsRow = GetCommandsRow();
var consoleCommand = GetConsoleCommand(commandsRow);
Task.Factory.StartNew(() =>
{
var runningCommandRow =
runtimeDataSet.RunningCommands.AddRunningCommandsRow(Guid.NewGuid(),
consoleCommand.OneLineDescription);

consoleCommand.Run(null);

runningCommandRow.Delete();
});
}

BindingSource 用于让 DataGridView 自动更新自身。

截至目前,如果没有以下 hack,我会收到一条错误消息,指出“索引 0 无效”。

        // prevents error when removing last row from data bound datagridview
var placeholder = runtimeDataSet
.RunningCommands
.AddRunningCommandsRow(Guid.NewGuid(), "PLACEHOLDER");

使用上面的代码,它会导致 DataGridView 中始终至少有一行,它工作正常。

我该如何解决这个问题?

注意:这似乎是很多其他人会遇到的事情,但我的网络搜索失败了..

最佳答案

我遇到了同样的问题。经过一些尝试和错误后,我发现您必须处理 datagridview 的几个事件才能使它消失,甚至需要忽略一些错误。我不记得我做了什么来规避这个错误,但我认为以下片段可能会提供一些见解(评论中的解释):

删除行:

//When I remove a row that has been added, but not commited return from the function
//run the update function only if the row is commited (databound)
private void dgvTest_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e) {
if (_lastDataRow == null || _lastDataRow.RowState == DataRowState.Added)
return;
UpdateRowToDatabase();
}

行验证:

//I got the kind of Index not valid or other index errors
//RowValidating is fired when entering the row and when leaving it
//In my case there was no point in validating on row enter
private void dgvTest_RowValidating(object sender, DataGridViewCellCancelEventArgs e) {
if (Disposing)
return;
if (!dgvTest.IsCurrentRowDirty) {
return;
}
try {
var v = dgvTest.Rows[e.RowIndex].DataBoundItem;
} catch {
return;
}
}

数据错误:

//I had to trap some errors and change things accordingly
private void dgvTest_DataError(object sender, DataGridViewDataErrorEventArgs e) {
if (e.Exception.Message.Contains("Index") && e.Exception.Message.Contains("does not have a value")) {
//when editing a new row, after first one it throws an error
//cancel everything and reset to allow the user to make the desired changes
bindingSource.CancelEdit();
bindingSource.EndEdit();
bindingSource.AllowNew = false;
bindingSource.AllowNew = true;
e.Cancel = true;
dgvTest.Visible = false;
dgvTest.Visible = true;
}
}

行输入:

 //Some problemes occured when entering a new row
//This resets the bindingsource's AllowNew property so the user may input new data
private void dgvTest_RowEnter(object sender, DataGridViewCellEventArgs e) {
if (!_loaded)
return;
if (e.RowIndex == dgvTest.NewRowIndex)
if (String.IsNullOrWhiteSpace(dgvTest.Rows[e.RowIndex == 0 ? e.RowIndex : e.RowIndex - 1].Cells[_idColumn].Value.ToStringSafe())) {
bindingSource.CancelEdit();
bindingSource.AllowNew = false;
bindingSource.AllowNew = true;
}
}

希望这对您有所帮助!

关于c# - 从绑定(bind)到 DataGridView 的 DataTable 中删除最后一个 DataRow 时如何防止出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17825174/

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