- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在我的 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/
我已经看到 Datagridview 不允许复制和粘贴多个单元格的文本,是否有一个简单的设置来启用它,或者我是否必须使用键处理程序和剪贴板数据存储来包含该功能。 用户想要在一行中复制 3 个单元格,并
我有一个 DataGridView我想限制用户只为特定列下的单元格输入数值的控件。我怎样才能在 DataGridView 中完成这种类型的验证细胞? 当我创建一个简单的文本框时是可能的,但是我如何验证
我正在尝试格式化 DataGridView,使用样式颜色等。DGV 在表单启动时加载(通过 buildGrid 方法),如构造函数代码中所示: public Report1(DataSet d
第一列中的小黑三角的官方DataGridView命名法描述是什么? 好像是标记了DataGridView.CurrentRow的位置,但是只是一个get属性,我想设置一下。 Grid with arr
我有点卡在我写的一些代码上 一个大纲是我正在从 SQL 数据库中读取一些数据,并希望将其显示在表单上的 DataGridView 中。我已经确认有数据从数据库返回,但不确定为什么没有出现。我遵循了互联
我的 DataTable从数据库中提取了三列,而我只需要将其中的两列绑定(bind)到 DataGridView .你能帮我吗? 最佳答案 这可能有用 DataSet ds = new DataSet
我有一个包含 Dataset ds 和 DataGridView dgv 的 WinForms 应用程序。 dgv 绑定(bind)到 ds。 ds 通过 Task() 更新,该 Task() 使用
我有DataGridView(dgHome),在parentForm(Home)中有一些列。子 Form(bill) 有 DataGridView(dgbill) 。我需要当我单击 dgHome 中的
我有两个具有相同列架构的 DataGridView(尽管有两个不同的 DataView 作为 DataSource - 如果这很重要)。将一行从一个数据 GridView 移动到另一个数据 GridV
基本上我有 2 个 DataGridView,我需要将行从一个复制到另一个。 到目前为止我已经尝试过: DataGridViewRowCollection tmpRowCollection = Dat
我正在尝试根据它包含的行数使我的 DataGridView 的高度自动调整。目前,我能够通过以下行完成此操作: dataGridView_SearchResults.AutoSize = true;
使用 WinForms、C# .Net 2.0 我正在向非绑定(bind) DataGridView 添加行。我想在其中一列中有一个 DataGridViewButtonColumn,在单击时删除该行
我有一个 datagridview(dataGridView1,其中包含一个 dataGridview2 列标题),其中我有一个复选框列,称为 dgvCkb,在 dataGridView1。当我在任何
对不起,如果这是一个愚蠢的问题。我在这方面很陌生。我应该如何将组合框添加到数据表,然后将其加载到数据 GridView 中?这可以做到吗?这是最好的方法吗?非常感谢有关如何执行此操作的提示和教程。先感
我正在尝试将 .txt 文件导入 DataGrid。问题是虽然代码大部分工作正常,但在导入 .txt 文件时,它会创建额外的行,例如 this ; OpenFileDialog ofd
datagridview 加载速度非常慢。我该如何优化它? datagridview有4-5千行。 我必须根据几个参数动态生成一个 datagridview。(来自数据库的数据,列数) 我必须从数据库
我怎样才能强制DataGridView.CellValueChanged DataGridViewCell.Value 立即引发事件(并将更改实际提交给 ComboBox 属性)单元格中的编辑控件更改
我有一个通过数据集从数据库中获取数据的功能 public DataSet getDataSet(string query) { DataSet ds = new DataSet()
客户希望能够键入一个字母并让系统滚动 DataGridView,这样具有与字母匹配的单元格的第一行将滚动到 DataGridView 的顶部 任何建议将不胜感激 最佳答案 我不确定您将如何有效地查找与
好吧,我有一个 DataGridView,用户可以在其中单击列标题进行排序。当他们在应用排序时添加新行时,直到验证该行时才会创建记录(在退出 newRow 之前他们无法执行此操作)。排序后如何才能选择
我是一名优秀的程序员,十分优秀!