C# DataGrid deleted items?

Discussion in 'OT Technology' started by dexedrine, Jun 19, 2003.

  1. dexedrine

    dexedrine Guest

    How do I know when I deleted an item in the DataGrid?

    Is there an event I should use?
     
  2. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    web or windows?
     
  3. dexedrine

    dexedrine Guest

    windows

    i'm an ASP.NET master, not Windows Forms.
     
  4. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    The DataTable object has a RowDeleted event;

    Code:
    private void AddHandler(){
       myDataTable = new DataTable("myDataTable");
       myDataTable.RowDeleted +=
          new System.Data.DataRowChangeEventHandler(myDataTable_Deleted);
    }
    
    protected void myDataTable_Deleted(object sender,
    System.Data.DataRowChangeEventArgs e) { 
       //... blah blah blah [Deleted code]
    }
    
    
     
  5. dexedrine

    dexedrine Guest

    cool thanks ;)
     
  6. dexedrine

    dexedrine Guest

    what about adding an item?
     
  7. dexedrine

    dexedrine Guest

    I tried using this...however it works "one behind". I think that the row hasn't been created or something yet.

    Code:
    private void something(...)
    {
    	ds.Tables[0].RowChanged += new DataRowChangeEventHandler(frmScan_RowChanged);
    }
    
    private void frmScan_RowChanged(object sender, DataRowChangeEventArgs e)
    {
    	/* frmScan_RowChanged
    	 * fires when an item is modified or created...i think
    	 */
    	
    	if (e.Action == System.Data.DataRowAction.Add)
    	{
    		// just insert the item, update the datagrid.
    		modify_table(); /* this is pretty much just a .Update on the SqlDataAdapter */
    		update_listview();	
    	}
    }
    
     
  8. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    basicly the same thing, but use the "RowChanged" Event and use the e.Row.RowState property to check the state to see whether it was added or whatever...

    Code:
    private void AddHandler(){
       myDataTable = new DataTable("myDataTable");
       myDataTable.RowChanged +=
          new System.Data.DataRowChangeEventHandler(myDataTable_Changed);
    }
    
    protected void myDataTable_Changed(object sender,
    System.Data.DataRowChangeEventArgs e) { 
       if( e.Row.RowState == DataRowState.Added )
          MessageBox.Show( "Row added" );
    
    }
    
     
  9. dexedrine

    dexedrine Guest

    any clue about the behind by one thing?
     
  10. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    :confused: what are you referring to?
     
  11. dexedrine

    dexedrine Guest

    sorry...

    when i insert an item, the item shows up in the datagrid, however the sql db doesn't have the new information

    i add a new item, and it shows up in the datagrid too, however the previous record that i entered shows up in the sql db now.
     
  12. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    when you change the row on the datagrid, that is when it actually goes and adds it to the database. You can, however, update the datasource yourself by just capturing the RowColChange Event of the datagrid and executing Update() of the DataAdapter.

    Code:
    // sometimes you have to force the .EndEdit() on the row
    DataTable.Rows[ dgAvailable.CurrentRowIndex ].EndEdit();
    YourDataAdapter.Update( YourDataSet, "TableName" );
    
     

Share This Page