Help with deleting DataTables's row
hi guys! can you help me please..What im trying to do here is, Delete particular row/s from my DatSet(dsSearchResult) Table index '0' base from what user selected in my List (LstSettingsUserResult)
Code:
dsSearchResult.Tables[0].Rows[LstSettingsUserResult.SelectedIndices[i]].Delete();
and re-display the rows of the updated DatSet(dsSearchResult) Table index '0'
Code:
LstSettingsUserResult.BeginUpdate();
foreach (DataRow drUserAccount in dsSearchResult.Tables[0].Rows)
{
LstSettingsUserResult.Items.Add(drUserAccount["User_LoginName"].ToString());
}
LstSettingsUserResult.EndUpdate();
But it has an error..can you help me guys to figure it out..Thanks!
Quote:
System.Data.DeletedRowInaccessibleException was unhandled
Message="Deleted row information cannot be accessed through the row."
Source="System.Data"
StackTrace:
at System.Data.DataRow.GetDefaultRecord()
at System.Data.DataRow.get_Item(String columnName)
at RCPSProject.FrmSettings.FuncPopulateSearchResultList() in C:\Documents and Settings\absalvamante\My Documents\Visual Studio 2005\Projects\RCPSProject\RCPSProject\FrmSettings.cs:line 86
at RCPSProject.FrmSettings.BtnSettingsUserRemove_Click(Object sender, EventArgs e) in C:\Documents and Settings\absalvamante\My Documents\Visual Studio 2005\Projects\RCPSProject\RCPSProject\FrmSettings.cs:line 341
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at RCPSProject.Program.Main() in C:\Documents and Settings\absalvamante\My Documents\Visual Studio 2005\Projects\RCPSProject\RCPSProject\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Re: Help with deleting DataTables's row
As the error message says, you cannot get data from a deleted row. You need to ignore deleted rows when repopulating your control:
Code:
LstSettingsUserResult.BeginUpdate();
foreach (DataRow drUserAccount in dsSearchResult.Tables[0].Rows)
{
if (drUserAccount.RowState != DataRowState.Deleted)
{
LstSettingsUserResult.Items.Add(drUserAccount["User_LoginName"].ToString());
}
}
LstSettingsUserResult.EndUpdate();
That said, wouldn't you be better off just binding this table to the control, in which case the item will be automatically removed from the control when you delete the row?
Re: Help with deleting DataTables's row
Oh....Nice suggestion, I'll try that JM...But expect some follow-up question. :D Thanks a lot anyway.
Re: Help with deleting DataTables's row
can you help me to bind the listbox from my binding source? I have tried the code below but i have no luck..
Code:
bsSearchResult.DataSource = dsSearchResult.Tables[0];
LstSettingsUserResult.DataBindings.Add("Items", bsSearchResult, "User_loginName");
ERROR:
Quote:
Cannot bind to property 'Items' because it is read-only.
Parameter name: PropertyName
Re: Help with deleting DataTables's row
You use the DataSource, DisplayMember and ValueMember properties, just like a ComboBox.
Re: Help with deleting DataTables's row