Results 1 to 6 of 6

Thread: Help with deleting DataTables's row

  1. #1

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    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!

    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()

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: Help with deleting DataTables's row

    Oh....Nice suggestion, I'll try that JM...But expect some follow-up question. Thanks a lot anyway.

  4. #4

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    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:
    Cannot bind to property 'Items' because it is read-only.
    Parameter name: PropertyName

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Help with deleting DataTables's row

    You use the DataSource, DisplayMember and ValueMember properties, just like a ComboBox.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: Help with deleting DataTables's row

    Ok...Thanks a lot!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width