1
votes

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.

1
I don't understand your question. Can you elaborate it? - user1108948
I'll try to add details above - pStan
I've added code to handle the _AddingNew event on the bindingsource... that allows me to put in default values that show in the grid, however that new row still does not populate the textboxes which are bound to the same bindingsource as the grid. Still stumped. - pStan
Im facing the same problem now. in one form it clear the text boxes but in the other form it only increment the record count by 1 but not clear the text boxes for a new record. Last row data still there. Did you find a solution ? Is this something relate to having primary keys with auto increment or not ? The form works fine has a PK with auto increment but the other one's PK is not auto increment - Peck_conyon
I'll look back at my code to see what I did. This has been a while ... - pStan

1 Answers

1
votes

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.