c# - How to prevent error when deleting last DataRow from DataTable bound to DataGridView? -
in winforms application, have following logic triggered button press:
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(); }); }
a bindingsource used let datagridview automatically update itself.
as of now, without following hack, error saying the "index 0 invalid".
// prevents error when removing last row data bound datagridview var placeholder = runtimedataset .runningcommands .addrunningcommandsrow(guid.newguid(), "placeholder");
with above code, causes there @ least 1 row in datagridview, works fine.
how fix this?
note: seems many others have run into, web searches have failed..
i have had same problem. after trial , error have discovered have handle several events of datagridview make go away, , requires error ignoring. don't remeber precisely have done circumvent error think following snippets may offer insight (explanations in comments):
remove row:
//when remove row has been added, not commited return function //run update function if row commited (databound) private void dgvtest_rowsremoved(object sender, datagridviewrowsremovedeventargs e) { if (_lastdatarow == null || _lastdatarow.rowstate == datarowstate.added) return; updaterowtodatabase(); }
row validating:
//i got kind of index not valid or other index errors //rowvalidating fired when entering row , when leaving //in case there 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; } }
data error:
//i had trap errors , change things accordingly private void dgvtest_dataerror(object sender, datagridviewdataerroreventargs e) { if (e.exception.message.contains("index") && e.exception.message.contains("does not have value")) { //when editing new row, after first 1 throws error //cancel , reset allow user make desired changes bindingsource.canceledit(); bindingsource.endedit(); bindingsource.allownew = false; bindingsource.allownew = true; e.cancel = true; dgvtest.visible = false; dgvtest.visible = true; } }
row enter:
//some problemes occured when entering new row //this resets bindingsource's allownew property 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; } }
hope helps!
Comments
Post a Comment