简体   繁体   中英

bound text boxes do not show adding new row

I'm using a datagridview, bindingnavigator and textboxes hooked to a bindingsource (set in code). When I select row in the grid the contents show in the textboxes, all is well.

txtQty.DataBindings.Add("Text", bsInvoiceDetails, "Detail_Quantity");

Next I click the add on the binding navigator. It adds a blank row to the datagrid which is fine, but it does not clear the textboxes. The highlighted row in the grid is a different row. When I manually select the new row the textboxes still do not change.

If I type in the texboxes with the new row selected in the grid, it in fact changes a different rows values.

I can't figure out why this is our how to fix it.

  • Help

Update:

Ran a query, returned a datatable... attached it to a dataset... the dataset is the datasource for the bindingsource ...

dsInvoiceDetails = new 
dsInvoiceDetails.Tables.Add(dtInvoiceDetails);

bsInvoiceDetails.DataSource = dsInvoiceDetails;
bsInvoiceDetails.DataMember = "Invoice_Detail";

Code to hook the grid and the binding navigator to the bindingsource...

gridInvoiceDetails.DataSource = bsInvoiceDetails;
bnInvoiceDetails.BindingSource = bsInvoiceDetails;

The binding source populates the textboxes above "automatically" when a grid row is selected. Works perfectly.

The binding navigator has a cool button to add rows. Click on that. It adds a row to the grid. Great. But when the new row is selected nothing changed with the bound text boxes.

I may need to handle the "AddingNew" event on the bindingsource...

Hope that helps define the question a bit more for ya.

Thanks for the help.

It has been a long time since I asked this, but it continues to get attention, so here is some additional info. I basically create a new instance of the invoice detail and assign it to the events NewObject.

So I did add an event that is triggered when adding a new row:

        bsInvoiceDetails.AddingNew += new AddingNewEventHandler(bsInvoiceDetails_AddingNew);

And Here is the code fired:

    void bsInvoiceDetails_AddingNew(object sender, AddingNewEventArgs e)
    {
        try
        {
            InvoiceDetail id = new InvoiceDetail();
            id.InvoiceID = invoice.InvoiceID;   // Make sure we are hooked to this invoice.
            id.DetailDate = DateTime.Today;
            id.DetailQuantity = 0;
            id.DetailRate = 0;
            id.DetailTotal = 0;
            id.DetailDescription = "* New Detail Description *";

            e.NewObject = id;
        }
        catch (Exception ex)
        {
            ErrorManager em = new ErrorManager(ex, "InvoiceEditForm.cs", "bsInvoiceDetails_ListChanged", "Adding a new detail item", "sql");
        }
    }

I think that is all I did to get around the problem. Feel free to post another question if this does not help resolve the problem for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM