Delete Records

ssabc

Well-known member
Joined
Dec 11, 2017
Messages
63
Programming Experience
10+
My next question us, deleting records. I have a bound DataGridView. It is my understanding that the whole point in this is that the database is connected, and I do not have to have a bunch of db synchronizing going on in the background. Every post I read on the matter suggests this.

So I would simply like to:

C#:
            dgvJobDetail.Rows.RemoveAt(this.dgvJobDetail.SelectedRows[0].Index);
            dgvJobDetail.Refresh();

It works on the screen, but not on the db.

Much Thanks!
 
I think that you have misunderstood what you have been reading. There is no connection at all between the grid and the database. .NET data access works by opening a connection to the database, moving data between the database and a local cache, then closing the connection. In your case, the local cache is most like a DataTable, which may or may not be in a DataSet. It is that local cache, which is not connected to the database, that your grid is bound to.

Any changes you make in the grid will be propagated to the DataTable, but that doesn't affect the database. You call Fill on a data adapter or table adapter to retrieve data from the database into the DataTable. Conversely, you call Update on the same data adapter or table adapter to save changes from the DataTable back to the database.

The code you posted is wrong for a number of reasons. Firstly, the Refresh call on the grid is completely pointless as it has nothing to do with data. That method simply redraws a control on screen. The only time you should be calling Refresh is if you're doing your own GDI+ drawing or on the rare occasions that you're doing long-running work on the UI thread and what to make the UI look like it isn't frozen.

Secondly, Remove or RemoveAt are not how you delete bound data. What you should be doing is binding the DataTable to a BindingSource and binding that to the grid and then, to delete the current record, you call RemoveCurrent on the BindingSource. That will work for simple data sources too but, in the case of a DataTable, what it does is call the Delete method of the underlying DataRow, which is what you need to do if you want to delete that record from the database. I'm not sure whether what you're doing would even work to remove the bound DataRow from the DataTable but, even if it did, that would be of no use. Removing a DataRow from a DataTable is like not retrieving it in the first place and if you don;t retrieve it then you can't delete it. Deleting a DataRow actually keeps it in the DataTable but sets its RowState to Deleted. It's that flag that causes the data adapter or table adapter to execute a SQL DELETE statement against the database for that record.
 
Additional Clarification please

I found some reference, and have come up with this:


C#:
        private void cmdDeleteDetailRow_Click(object sender, EventArgs e)
        {
            int ID = this.dgvJobDetail.SelectedRows[0].Index;


            using (var context = new ResourcePlanningEntities())
            {
                var qryDelete = from a in context.JobDetails
                                where (a.ID == ID)
                                select a;
                foreach (var record in qryDelete)
                    context.DeleteObject(record);
                    context.SaveChanges();


            }
            
        }

The issue comes in here:
C#:
context.[COLOR=#ff0000]DeleteObject[/COLOR](record);

There does not seem to be a DeleteObject item under context.

Thanks for your patience with me, as I work on this project whenever I need to depart from another VB.NET one I am more focused on, when there are no CAD licenses available.
 
I am so confused. All I want to do is delete a record from a database. How many books do I have to read to figure this out? Yes, Entity Framework 6. A am able to add rows and do updates with no problem, without connection to the database because the project was setup with Database First. All the objects for the binding sources, etc., exist.

So to delete the selected row on a datagridview, is this really this difficult??

C#:
this.jobDetailBindingSource.RemoveCurrent();
only removes it until the form is reloaded, so the data is still there.

So I added this LINQ query, as I have done elsewhere:


C#:
            int ID = this.dgvJobDetail.SelectedRows[0].Index;




            using (var context = new ResourcePlanningEntities())
            {
                var qryDelete = from a in context.JobDetails
                                where (a.ID == ID)
                                select a;
                foreach (var record in qryDelete)
                         context.DeleteObject(record);
                         context.SaveChanges();


                this.jobDetailBindingSource.RemoveCurrent(); // Removes object from datagridview only




            }

I also tried setting it up as an entity as shown in this link: https://www.aspsnippets.com/Article...-Delete-using-Entity-Framework-in-ASPNet.aspx

C#:
int ID = this.dgvJobDetail.SelectedRows[0].Index;


            using (var context = new ResourcePlanningEntities())
            using (ResourcePlanningEntities entities = new ResourcePlanningEntities())
            {
                JobDetail qryDelete = (from a in entities.JobDetails
                                       where (a.ID == ID)
                                       select a).FirstOrDefault();


                entities.DeleteObject(qryDelete);
                entities.SaveChanges();

The examples all use DeleteObject, which is not even available!
 
Last edited:
Okay!!! Here's the rocket science answer:

For the life of me, I see no value in entity framework if i have to do all of this to delete the current row! If you have anything easier, I'd love to know about it.

C#:
        private void cmdDeleteDetailRow_Click(object sender, EventArgs e)
        {
            int selectedrowindex = dgvJobDetail.SelectedCells[0].RowIndex;
            DataGridViewRow selectedRow = dgvJobDetail.Rows[selectedrowindex];
            string IDVal = dgvJobDetail.CurrentRow.Cells[0].Value.ToString();
            int IDVal_int = Int32.Parse(IDVal);


            using (var context = new ResourcePlanningEntities())
            {
                var singleRec = context.JobDetails.FirstOrDefault(a => a.ID == (IDVal_int));
                context.JobDetails.Remove(singleRec);
                context.SaveChanges();


            }


            this.jobDetailBindingSource.RemoveCurrent();
            
        }
 
Back
Top Bottom