DataGridView, DataConnector and Deleting Quirkiness

I was playing around with my copy of Whidbey Beta.

Perhaps it's due to inexperience, perhaps it's something else, but for the love of God, I couldn't figure out how to add Delete functionality to the new DataGridView WinForm control that would not crash the application. My first hunch was it was something stupid that I did not do. I could not find any resource online either that will explain why my application would crash everytime I press the Delete button on a specific DataGridViewRow or when I try to execute the delete through coding (User press a button that in the end will try to delete the bound record).

The code was simple enough. The DataGridView's DataSource property is bound to my DataConnector object. The DataConnector object's DataSource property is bound to a Typed DataSet which data comes from an Oracle database. I was using Oracle Standard Edition 8.1.7 (which is the minimal requirement you absolutely must have to use the System.Data.OracleClient namespace). I added the necessary code to hook up the OracleTableAdapter Insert, Update and Delete Commands to work properly.

In the end, I just have to find a workaround for this problem. And so, I improvised a little. I know that I can update the data from the dataset to my Oracle database if I call my OracleDataAdapter.Update method (which is already properly setup). I know if I do not connect the DataGridView to the DataConnector object I can actually delete a row from my DataGridView. So... the culprit must be in the DataConnector class.

The solution that I used in the end is to trap the DataGridView's UserDeletingRow event and temporarily disconnect the DataConnector from both the DataGridView and the DataSet object and update the DataSet object manually. Commit the changes and reconnect them again. That seems to work alright.

I strongly feel that this is an actual bug in the the new DataConnector class. Any input that can prove me wrong is most welcomed.

private bool EndEdit()
{
   
try
   
{
       
this.productCategoryLineDataGridView.EndEdit(DataGridViewDataErrorContext.Commit);
       
this.ProductCategoryLineDataConnector.EndEdit();

       
return true;
   
}
   
catch (Exception ex)
   
{
       
MessageBox.Show(ex.Message);

       
return false;
   
}
}

private void productCategoryLineDataGridView_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
   
if (EndEdit())
   
{
        // Disconnect the DataConnector and the DataGridView DataSource
        this.productCategoryLineDataGridView.DataSource = null;
       
this.ProductCategoryLineDataConnector.DataSource = null;

        // Remove the selected row from the ProductCategoryLine DataTable
       
DataRow[] rows = this.productCategoryDataSet.ProductCategoryLine.Select("CategoryID=" + CategoryID + " AND ID=" + Convert.ToInt32(e.Row.Cells["ID"].Value);
       
if (rows != null)
            rows[0].Delete();

        // Reconnect the DataConnector and the DataGridView DataSource
       
this.ProductCategoryLineDataConnector.DataSource = productCategoryDataSet.ProductCategoryLine;
       
this.productCategoryLineDataGridView.DataSource = ProductCategoryLineDataConnector;
   
}

    // Althought it's supposed to be successful, but cancel the event anyhow to
    // avoid the crash bug.
   
e.Cancel = true;
}

P.S.
Microsoft must have fixed this behaviour in the Whidbey Beta 1 Refresh. I tested the behaviour both using SqlClient and OracleClient and it seems to work fine now.

Share this post: | | | |
Published Tuesday, December 21, 2004 2:09 AM by Jimmy Chandra
Filed under: ,

Comments

# re: DataGridView, DataConnector and Deleting Quirkiness

This is really great. Thanks Jimmy. Please help our friend here at Indonesia.

Tuesday, December 21, 2004 2:21 AM by Jimmy Chandra

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: