DataBinding很难过

时间:2010-02-15 22:23:18

标签: c# .net winforms data-binding datagridview

所以我有这个datagridview链接到绑定到底层数据表的Binding源。问题是我需要手动向datagridview添加行。

绑定时无法完成,因此我必须使用数据绑定。

如果我将行添加到基础数据表中,则在保存数据表时,行会重复,可能是因为绑定源以某种方式获得了副本并将其插入。

将它添加到绑定源是我一直在尝试做的事情,但它不是很有效。

让我准确解释一下我的设置:

我有一个包含两个表的数据库: CashReceiptTable和CashReceiptItemsTable

CashReceiptItemsTable包含FK到CashReceiptTable。

表单允许用户添加和修改两个表。

当用户输入新的cashreceipt时,现金收据的id为-1,cashReceiptitemstable中的FK为-1。保存数据库时,cashReceipt的id已更新,我必须手动更新cashreceiptitem的FK。

以下是问题:

当我尝试在cashreceiteitems绑定源中的多行中更新CashReceiptID(FK)时,第一行被更新,并且消失(因为它已被过滤),其他行被删除,我不能再访问它们。

我不知道为什么会这样,我还没有更新过滤器,所以他们应该仍然在那里,但尝试访问它们会抛出RowNotInTableException。

我已经管理了一个解决方法,将绑定源中的行复制到内存数组中,删除绑定源中的第一行(所有其他行都消失),更新行的FK并重新插入绑定源并保存表。

这没关系,但为什么行会消失?

我还有一个小问题。当CashReceiptsTable为空并且我向其添加新行时,如果我向CashReceiptsItemTable添加多行,则会导致问题。手动将项目添加到绑定源时,添加新行将弹出到上一行并将其推送到数据表。这隐藏了我的FK更新例程,它丢失了,它也将它从DataGridView中删除。

当我将第一行添加到CashReceiptsTable时, 才会这样做。为什么会这样做,我该如何解决?

我发布的代码在此处自动填充:

        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;
        }

这是将其保存到数据库的功能:

    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);
    }

1 个答案:

答案 0 :(得分:1)

您可能想尝试向DataGridView添加行。由于您绑定了它,DataGridView将成为您的“访问点”。

我有几个绑定到DataGridView的应用程序,在大多数情况下,当我添加一行时,我通过DataGridView这样做。它已经具有属性/方法/事件,可以让您相对轻松地添加。

如果您需要更多信息,我可以更新。