Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataBinding woes

So I have this datagridview that is linked to a Binding source that is binding to an underlying data table. The problem is I need to manual add rows to the datagridview.

This cannot be done while it is bound, so I have to work with the databinding.

If I add the rows to the underlying datatable, when the datatable is saved, the rows are duplicated, probably because the binding source somehow got a hold of a copy and inserted it also.

Adding it to the binding source is what I've been trying to do but it's not quite working.

Let me explain exactly what my setup is:

I have a database with two tables: CashReceiptTable and CashReceiptItemsTable

CashReceiptItemsTable contains a FK to CashReceiptTable.

The form allows the users to add, and modify the two tables.

When the user enters a new cashreceipt, the cash receipt's id is -1, and the FK in cashReceiptitemstable is -1. When the database is saved, cashReceipt's id is updated, and I have to manually update cashreceiptitem's FK.

Here are the problems:

When I try to update the CashReceiptID (the FK) in more than one row in cashreceiteitems binding source, the first row is updated, and disappears (because it's filtered), and the other rows are removed, and I can no longer access them.

I have no idea why this is, I haven't updated the filter yet so they should still be there, but trying to access them throws RowNotInTableException.

I've managed a work around that copies the rows in the the binding source to an in memory array, deletes the first row in the binding source (all the other rows just vanish), update the row's FK and reinsert them into the binding source and save the table.

This works okay, but why do the rows disappear?

I also have one more slight problem. When the CashReceiptsTable is empty and I am adding a new row to it, if I add more than one row to the CashReceiptsItemTable it causes problems. When manually adding the items to the binding source, adding a new row pops to previous row off and pushes it onto the datatable. This hides it from my FK updating routine and it is lost, it also removes it from the DataGridView.

It only does that when I'm adding the first row to CashReceiptsTable. Why does it do this, and how can I fix it?

I'm posting my code that autopopulates it here:

        private void autopopulate(decimal totalPayment) {
            //remove old rows
            for (int i = 0; i < tblCashReceiptsApplyToBindingSource.List.Count; i++) {
                DataRowView viewRow = tblCashReceiptsApplyToBindingSource.List[i] as DataRowView;
                RentalEaseDataSet.tblCashReceiptsApplyToRow row = viewRow.Row as RentalEaseDataSet.tblCashReceiptsApplyToRow;

                if (row.CashReceiptsID == this.ReceiptID) {
                    tblCashReceiptsApplyToBindingSource.List.Remove(viewRow);
                    i--;
                }
            }

            decimal payment = totalPayment;

            //look for an exact amount
            foreach (DataGridViewRow dueRow in dataViewDueRO.Rows) {
                decimal due = -1 * (Decimal)dueRow.Cells[Due.Index].Value;
                if (due == payment) {
                    String charge = (String)dueRow.Cells[Description.Index].Value;
                    int chargeID = ManageCheckbooks.findTransactionID(charge);

                    tblCashReceiptsApplyToBindingSource.AddNew();

                    RentalEaseDataSet.tblCashReceiptsApplyToRow row = ((DataRowView)tblCashReceiptsApplyToBindingSource.Current).Row as RentalEaseDataSet.tblCashReceiptsApplyToRow;
                    row.CashReceiptsID = this.ReceiptID;
                    row.ApplyTo = chargeID;

                    row.Paid = payment; //convert to positive

                    payment = 0;
                    break;
                }
            }

            //if the exact amount was found, payment will = 0, and this will do nothing, otherwise,
            //divy out everything left over (which will be everything)
            foreach (DataGridViewRow dueRow in dataViewDueRO.Rows) {
                String charge = (String)dueRow.Cells[Description.Index].Value;
                decimal due = (Decimal)dueRow.Cells[Due.Index].Value;

                if (due > 0 || payment <= 0) {
                    continue;
                }

                int chargeID = ManageCheckbooks.findTransactionID(charge);

                payment += due; //due is negative, so this will subtract how much the user owes

                tblCashReceiptsApplyToBindingSource.AddNew();

                RentalEaseDataSet.tblCashReceiptsApplyToRow row = ((DataRowView)tblCashReceiptsApplyToBindingSource.Current).Row as RentalEaseDataSet.tblCashReceiptsApplyToRow;
                row.CashReceiptsID = this.ReceiptID;
                row.ApplyTo = chargeID;

                if (payment >= 0) {
                    //payment is enough to cover this
                    row.Paid = due * -1; //convert to positive
                } else {
                    //doesn't have enough money to conver this, can only cover partial, or none
                    row.Paid = (due - payment) * -1; //math:
                    //money remaining $50, current charge = $60
                    //payment = 50 + -60 = -10
                    //row["Paid"] = (-60 - -10) * -1
                    //row["Paid"] = (-60 + 10) * -1
                    //row["Paid"] = -50 * -1
                    //row["Paid"] = 50
                }

                if (payment <= 0) {
                    break; //don't conintue, no more money to distribute
                }
            }

            isVirginRow = true;
        }

And this is the function that saves it to the database:

    protected override void saveToDatabase() {
        tblCashReceiptsBindingSource.EndEdit();
        isVirginRow = false;

        RentalEaseDataSet.tblCashReceiptsRow[] rows = rentalEaseDataSet.tblCashReceipts.Select("ID < 0") as RentalEaseDataSet.tblCashReceiptsRow[];
        int newID = -1;
        if (rows.Count() > 0) {
            tblCashReceiptsTableAdapter.Update(rows[0]);
            newID = rows[0].ID;
        }

        tblCashReceiptsTableAdapter.Update(rentalEaseDataSet.tblCashReceipts);


        //update table
        /*foreach (RentalEaseDataSet.tblCashReceiptsApplyToRow row in rentalEaseDataSet.tblCashReceiptsApplyTo.Select("CashReceiptsID = -1")) {
            row.CashReceiptsID = newID;
        }*/

        //update binding source
        DataRowView[] applicationsOld = new DataRowView[tblCashReceiptsApplyToBindingSource.List.Count];
        RentalEaseDataSet.tblCashReceiptsApplyToRow[] applicationsNew = new RentalEaseDataSet.tblCashReceiptsApplyToRow[tblCashReceiptsApplyToBindingSource.List.Count];
        tblCashReceiptsApplyToBindingSource.List.CopyTo(applicationsOld, 0);
        for (int i = 0; i < applicationsOld.Count(); i++) {
            RentalEaseDataSet.tblCashReceiptsApplyToRow row = applicationsOld[i].Row as RentalEaseDataSet.tblCashReceiptsApplyToRow;

            if (row.CashReceiptsID < 0) {
                applicationsNew[i] = rentalEaseDataSet.tblCashReceiptsApplyTo.NewRow() as RentalEaseDataSet.tblCashReceiptsApplyToRow;
                applicationsNew[i]["ID"] = row.ID;
                applicationsNew[i]["CashReceiptsID"] = this.ReceiptID;
                applicationsNew[i][2] = row[2];
                applicationsNew[i][3] = row[3];
                applicationsNew[i][4] = row[4];
                //row.Delete();
            }
        }
        for (int i = 0; i < applicationsOld.Count(); i++) {
            try {
                if ((int)applicationsOld[i].Row["ID"] < 0) {
                    applicationsOld[i].Row.Delete();
                }
            } catch (RowNotInTableException) {
                break;
            }
        }
        this.tblCashReceiptsApplyToBindingSource.Filter = "CashReceiptsID = " + this.ReceiptID;

        foreach (DataRow newRow in applicationsNew) {
            if (newRow == null) {
                break;
            }
            tblCashReceiptsApplyToBindingSource.AddNew();
            ((DataRowView)tblCashReceiptsApplyToBindingSource.Current).Row[0] = newRow[0];
            ((DataRowView)tblCashReceiptsApplyToBindingSource.Current).Row[1] = newRow[1];
            ((DataRowView)tblCashReceiptsApplyToBindingSource.Current).Row[2] = newRow[2];
            ((DataRowView)tblCashReceiptsApplyToBindingSource.Current).Row[3] = newRow[3];
            ((DataRowView)tblCashReceiptsApplyToBindingSource.Current).Row[4] = newRow[4];
        }

        tblCashReceiptsApplyToBindingSource.EndEdit();

        checkForBadRows();

        tblCashReceiptsApplyToTableAdapter.Update(rentalEaseDataSet.tblCashReceiptsApplyTo);
        tblCashReceiptsApplyToTableAdapter.Fill(rentalEaseDataSet.tblCashReceiptsApplyTo);
    }
like image 248
Malfist Avatar asked Feb 15 '10 22:02

Malfist


People also ask

What is data binding?

Data binding is a technique used when you want to glue pieces of information (your data) to some visual user input elements. In this process, whenever the input gets updated, the data behind it gets updated as well.

What is the default class name of the data binding library?

Each layout file is then associated with a Data Binding class that has been produced by the Library. In most cases, the default class name corresponds to the name of the layout file, followed by the Binding postfix, e.g. HomeActivityBinding.kt. Below are the advantages of using the Data Binding Library in your Android application:

What is data binding in Android jetpack?

Android Jetpack is a set of libraries designed to help developers follow best practices and create code quickly and simply. The Data Binding Library is one of them. Data Binding allows you to effortlessly communicate across views and data sources.

What is the data binding library in Android?

In Android, the Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically. What we are going to build in this article? Here is a sample video of what we are going to build in this project.


1 Answers

You might want to try adding rows to the DataGridView. Since you are binding to it, the DataGridView becomes your 'access point'.

I've got several applications that bind to DataGridView and for most circumstances when I add a row I do so through the DataGridView. It already has properties/methods/events that let you add with relative ease.

If you need some more information I can update.

like image 63
Tony Abrams Avatar answered Sep 20 '22 17:09

Tony Abrams