none
What is the difference between row.delete() and row.remove()? RRS feed

  • Question

  • Hi,
    I want to delete a row from a datagrid and update the database. I want to know which is more appropriate -> row.delete() or row.remove(). I have written a code using row.remove(), but i encounter an error "Value cannot be null". The code is pasted below. I want to know how to implement the row.delete(). i did not find the delete() method in intellisense

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    namespace NewAddressBook
    {
    public partial class AddrBuk : Form
    {
    string AddrBukConnString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\AddressBook.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
    string query = "select * from AddressBookTable";
    SqlConnection AddrBukConn;
    SqlDataAdapter AddrBukAdapter;
    DataSet AddrBukDataSet;
    SqlCommand AddrBukCommand;
    public AddrBuk()
    {
    InitializeComponent();
    }
    private void AddrBuk_Load(object sender, EventArgs e)
    {
    AddrBukConn =
    new SqlConnection(AddrBukConnString);
    AddrBukCommand =
    new SqlCommand(query, AddrBukConn);
    AddrBukAdapter =
    new SqlDataAdapter(AddrBukCommand);
    AddrBukDataSet =
    new DataSet();
    try
    {
    AddrBukConn.Open();
    AddrBukAdapter.Fill(AddrBukDataSet,
    "AddrbookTable");
    }
    catch (Exception ex1)
    {
    MessageBox.Show(ex1.Message);
    }
    try
    {
    dataGridView1.DataSource = AddrBukDataSet;
    dataGridView1.DataMember =
    "AddrbookTable";
    }
    catch (Exception ex2)
    {
    MessageBox.Show(ex2.Message);
    }
    /*finally
    {
    AddrBukConn.Close();
    }*/
    }
    private void Closebtn_Click(object sender, EventArgs e)
    {
    this.Close();
    }
    private void Discardbtn_Click(object sender, EventArgs e)
    {
    try
    {
    dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
    MessageBox.Show("Row Deleted");
    AddrBukAdapter.Update(AddrBukDataSet.Tables[
    "AddressBookTable"]);
    }
    catch (Exception ex3)
    {
    MessageBox.Show(ex3.Message);
    }
    finally
    {
    AddrBukConn.Close();
    }
    }
    }
    }

    Friday, January 16, 2009 1:27 AM

Answers

  •  

    Hi user_nithya,

    Welcome to MSDN.

    I have read your code. You have used DataGridView.Rows.Remove method to remove a row in the DataGridView which you want to delete. When dataGridView1.CurrentRow == null, it will throw "Value cannot be null" exception. If you want to avoid this, you can add an if statement.
    if (dataGridView1.CurrentRow != null)
    {
        dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
        MessageBox.Show("Row Deleted");
        AddrBukAdapter.Update(AddrBukDataSet.Tables["AddressBookTable"]);
    }

    As for the row.remove() and row.delete() you have mentioned in your post. Here is the documentation.

    DataRow.Delete Method
    If the RowState of the row is Added, the RowState becomes Detached and the row is removed from the table when you call AcceptChanges.

    The RowState becomes Deleted after you use the Delete method on an existing DataRow. It remains Deleted until you call AcceptChanges. At this time, the DataRow is removed from the table.

    A deleted row can be undeleted by invoking RejectChanges.
    http://msdn.microsoft.com/en-us/library/system.data.datarow.delete.aspx

    DataRowCollection.Remove Method
    When a row is removed, all data in that row is lost. You can also call the Delete method of the DataRow class to just mark a row for removal. Calling Remove is the same as calling Delete and then calling AcceptChanges.

    You can also use the Clear method to remove all members of the collection at one time.
    http://msdn.microsoft.com/en-us/library/system.data.datarowcollection.remove.aspx


    May be you have made a mistake about "DataGridViewRowCollection.Remove" and "DataRowCollection.Remove". DataGridViewRowCollection doesn't have Delete method. If you want to call delete method, you need to call "dtSource.Rows[index].Delete();"(dtSource is the a DataTable that has been bound to DataGridView).

    If you have any question, please feel free to tell me.

    Sincerely,
    Kira Qian


    Please mark the replies as answers if they help and unmark if they don't.
    • Marked as answer by user_nithya Tuesday, January 20, 2009 11:50 PM
    Monday, January 19, 2009 4:12 AM

All replies

  •  

    Hi user_nithya,

    Welcome to MSDN.

    I have read your code. You have used DataGridView.Rows.Remove method to remove a row in the DataGridView which you want to delete. When dataGridView1.CurrentRow == null, it will throw "Value cannot be null" exception. If you want to avoid this, you can add an if statement.
    if (dataGridView1.CurrentRow != null)
    {
        dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
        MessageBox.Show("Row Deleted");
        AddrBukAdapter.Update(AddrBukDataSet.Tables["AddressBookTable"]);
    }

    As for the row.remove() and row.delete() you have mentioned in your post. Here is the documentation.

    DataRow.Delete Method
    If the RowState of the row is Added, the RowState becomes Detached and the row is removed from the table when you call AcceptChanges.

    The RowState becomes Deleted after you use the Delete method on an existing DataRow. It remains Deleted until you call AcceptChanges. At this time, the DataRow is removed from the table.

    A deleted row can be undeleted by invoking RejectChanges.
    http://msdn.microsoft.com/en-us/library/system.data.datarow.delete.aspx

    DataRowCollection.Remove Method
    When a row is removed, all data in that row is lost. You can also call the Delete method of the DataRow class to just mark a row for removal. Calling Remove is the same as calling Delete and then calling AcceptChanges.

    You can also use the Clear method to remove all members of the collection at one time.
    http://msdn.microsoft.com/en-us/library/system.data.datarowcollection.remove.aspx


    May be you have made a mistake about "DataGridViewRowCollection.Remove" and "DataRowCollection.Remove". DataGridViewRowCollection doesn't have Delete method. If you want to call delete method, you need to call "dtSource.Rows[index].Delete();"(dtSource is the a DataTable that has been bound to DataGridView).

    If you have any question, please feel free to tell me.

    Sincerely,
    Kira Qian


    Please mark the replies as answers if they help and unmark if they don't.
    • Marked as answer by user_nithya Tuesday, January 20, 2009 11:50 PM
    Monday, January 19, 2009 4:12 AM
  • Hi Kira,

    I tried to delete rows from a database using the DataRow.Delete() method, but when i expecting this method to mark the rows as Deleted or Detached it simply removes the DataRow from the DataRow Collection. Do i missed something ?

    Here is the code i'm using



    public

     

    void Preload()

    {

     

    OdbcDataAdapter data_adpt = m_odbccmdb.DataAdapter;

    data_adpt.SelectCommand.CommandText = buildQuery();

    m_odbccmdb.RefreshSchema();

    m_dataset.Clear();

     

    if ((m_maps == null) && (data_adpt.TableMappings == null || data_adpt.TableMappings.Count == 0))

    {

    m_maps = (

    DataTableMapping[])XmlManager.setXml(FileManager.Read(Settings.Default.MappingsPath + ClassName + "_map.xml"), typeof(DataTableMapping[]));

     

    data_adpt.TableMappings.AddRange(m_maps);

    }

    data_adpt.FillSchema(m_dataset,

    SchemaType.Mapped, m_table_name);

    data_adpt.FillSchema(m_dataset,

    SchemaType.Source, m_table_name);

     

    }



    public

     

    void delete(TrackedItems trks)

    {

     

    if (string.IsNullOrEmpty(m_xml))

    {

     

    throw new NullReferenceException("Xml document is Null or Empty.");

    }

     

    else

    {

     

    if (m_dataset.DataSetName == "NewDataSet") Preload();

     

    //Load XML into the Dataset

     

    DataSet dataset_temp = m_dataset.Clone();//Copy the Original Dataset with all schema and constraints but without data

     

    StringReader srd = new StringReader(m_xml);

    dataset_temp.ReadXml(srd,

    XmlReadMode.Auto);

     

    int count=dataset_temp.Tables[0].Rows.Count;

    //here Delete removes rows from teh DataRowCollection instead of marking them as Deleted Why?


    while
    (dataset_temp.Tables[0].Rows.Count > 0) dataset_temp.Tables[0].Rows[dataset_temp.Tables[0].Rows.Count-1].Delete();

     

     

    //Merged incoming data with those previously loaded and compare

    m_dataset.Merge(dataset_temp);

     

    //Collects the changes/add/delete into a Trackeditems collection

     

    if (trks != null) trks.TrackChanges(m_dataset);

    m_odbccmdb.DataAdapter.Update(m_dataset, m_table_name);

    m_dataset.AcceptChanges();

    }

    }

    Thanks you for any assistance on this issue

    Eric

    Monday, August 10, 2009 8:04 AM