lunedì 9 agosto 2010 #

Access the Deleted Rows of a Datatable

Today, I’m updating the Code Generator I’ve made in 2006 to build automatically the Data Provider classes for my applications. I’m passing my applications to Framework 4.0 and instead of a simple framework change, I’ve decided to make some cleaning in Old Common Libraries code and add some new features in the Data Classes.

One of the new features in my data classes, will be some events, raised when data operations complete, RowAdded and RowDeleted events are two of them. In both cases, it would probably be useful if you Handle the Event in your code, to have some informations on the Added Row or Deleted Row, so I decided to pass the whole DataRow as a property of the event argument.

Pass the Added row as an argument is very simple, I have the object in my method.

Pass the Deleted row is a little less simple. I’ve made a simple test project to find out how to get it, to be precise, the exact phrase is: How to get a copy of it.

The sample form is the following:

righecancellate

On the form there are 3 buttons – Carica = Load, Test Aggiunta = Test AddRow, Test cancellazione = Test Deletion. There is also a textbox, set as Multiline with vertical scrollbar.

The test code is the following:

        DataTable mDt;
        private const string FLD_ID = "ID";
        private const string FLD_Description = "Description";
        private const string FLD_Date = "Date";
        private const string FLD_Price = "Price";

        public Form1()
        {
            InitializeComponent();
            mDt = new DataTable("Tbtest");
            DataColumn col = new DataColumn(FLD_ID, typeof(int));
            col.AutoIncrement = true;
            col.AutoIncrementSeed = 1;
            col.AutoIncrementStep = 1;
            mDt.Columns.Add(col);
            col = new DataColumn(FLD_Description, typeof(string));
            mDt.Columns.Add(col);
            col = new DataColumn(FLD_Date, typeof(DateTime));
            mDt.Columns.Add(col);
            col = new DataColumn(FLD_Price, typeof(Decimal));
            mDt.Columns.Add(col);
        }

The initialization code, I build a Datatable on the Constructor code of the form.

        private void btnCarica_Click(object sender, EventArgs e)
        {
            mDt.Rows.Clear();

            DataRow row = mDt.NewRow();
            row[FLD_Description] = "Quaderno";
            row[FLD_Date] = DateTime.Now;
            row[FLD_Price] = 1.5;
            mDt.Rows.Add(row);

            row = mDt.NewRow();
            row[FLD_Description] = "Penna";
            row[FLD_Date] = DateTime.Now;
            row[FLD_Price] = 2.8;
            mDt.Rows.Add(row);

            row = mDt.NewRow();
            row[FLD_Description] = "Gomma";
            row[FLD_Date] = DateTime.Now;
            row[FLD_Price] = 0.7;
            mDt.Rows.Add(row);

            row = mDt.NewRow();
            row[FLD_Description] = "Matita";
            row[FLD_Date] = DateTime.Now;
            row[FLD_Price] = 1.1;
            mDt.Rows.Add(row);

            row = mDt.NewRow();
            row[FLD_Description] = "Block notes";
            row[FLD_Date] = DateTime.Now;
            row[FLD_Price] = 2.8;
            mDt.Rows.Add(row);

            row = mDt.NewRow();
            row[FLD_Description] = "Pennarelli 12 colori";
            row[FLD_Date] = DateTime.Now;
            row[FLD_Price] = 6.7;
            mDt.Rows.Add(row);

            mDt.AcceptChanges();

            Mostra();
        }

Here I build some rows, put them in the DataTable and call the Mostra method, wich shows the rows in the textbox.

        private void Mostra()
        {
            this.txtResult.Text = string.Empty;
            for( int i=0; i< mDt.Rows.Count;i++)
            {
                MostraRiga(mDt.Rows[i]);
            }
    
        }

        private void MostraRiga(DataRow pRow)
        {
            this.txtResult.Text += "ID: " + pRow[FLD_ID].ToString() + Environment.NewLine;
            this.txtResult.Text += "Description: " + pRow[FLD_Description].ToString() + Environment.NewLine;
            this.txtResult.Text += "Date: " + pRow[FLD_Date].ToString() + Environment.NewLine;
            this.txtResult.Text += "Price: " + pRow[FLD_Price].ToString() + Environment.NewLine;
            this.txtResult.Text += Environment.NewLine;
        }

This is the Mostra method and the MostraRiga Method, made to show the content of the table rows on the textbox.

        private void btnAdd_Click(object sender, EventArgs e)
        {

            DataRow row = mDt.NewRow();
            mDt.Rows.Add(row);
            row[FLD_Description] = string.Format("Nuova riga {0}", row[FLD_ID]);
            row[FLD_Date] = DateTime.Now;
            decimal pippo = Convert.ToDecimal(row[FLD_ID]);
            row[FLD_Price] = pippo*0.98m;

            mDt.AcceptChanges();

            MostraRiga(row);
        }

Here is the AddRow test, that simply builds a row, adds it to the table and then shows it on the textbox.

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DataRow row = mDt.Rows[0];
            int id = Convert.ToInt32(row[FLD_ID]);
            row.Delete();
            string filter = string.Format("{0} = {1}", FLD_ID, id);
            DataView vi = new DataView(mDt, filter,"", DataViewRowState.Deleted);
            DataTable tt = vi.ToTable();
            mDt.AcceptChanges();
            MostraRiga(tt.Rows[0]);
        }

And finally the most important bit of code, the code that helps us to show the informations of the deleted row.
As you can see, the method simply deletes the first row of the DataTable, saving it’s ID.
After the deletion, to be able to get the whole row, I build a dataview that retrieves the row I’ve deleted and then uses the ToTable method of the DataView to put the data in a new Table. Then I give the Row to the MostraRiga (ShowRow) method and I can see its content on my textbox.

The ToTable passage is very important, because if you try to pass to the function something like:

MostraRiga(vi[0].Row);

Whichis perfectly legal with a non deleted row, you instead receive an Exception, because you cannot access deleted rows data.

posted @ lunedì 9 agosto 2010 17.03 | Feedback (0)

Copyright © Sabrina C.

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski