Link to home
Start Free TrialLog in
Avatar of wiswalld
wiswalldFlag for United States of America

asked on

Insert a new row into a bound datagridview

Insert a new row into a bound datagridview

Database table - assignedvehicles
Tableadapter - assignedvehiclestableadapter
Dataset - _cad_050107dataset
Datagridview - Datagridview2

I want to insert a value from a textbox into cell 0 on the datagridview and insert cell 0 value from datagridview1 current row.
SOLUTION
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wiswalld

ASKER

When I do this I want to add a new row to the datagridview (datagridview2). I also want to pull some info from a selected row in datagridview1
Ok,

You must do cad_050107dataset.Tables(0).Rows.Add to add a new row

To pull som info you can datagridview1.CurrentRow.Cells(0).Value.ToString
Me._CAD_050107DataSet.Tables(0).Rows.Add()

<column eventnumber does not allow nulls>

eventnumber is not null
You have no values in the datagridview ?
Yes/Sometimes. Sometimes the datagridview will have no values. My datagridview shows assigned vehicles associated with the eventnumber from the form. So if the form eventnumber is 3 then all assigned vehicles for eventnumber 3 will or there will be none and I will add them using this method.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
How can I do this from datagridview1. It works from a button.
Avatar of Sancler
Sancler

A bound datagridview is a display (and editing) mechanism for the data in the datatable to which it is bound.  If you add a record to that datatable then - provided it passes through any filter that you may have put between the datatable and the datagridview - it will, as soon as the

    myDataTable.Rows.Add(myRow)

is hit - appear in the datagridview.  So you can "do it" from anywhere.  If you put that code - adapted for your names, values, etc - into the Click event for the button you are referring to, it should work.

Roger
Dim dr As DataRow = Me._CAD_050107DataSet.Tables(0).NewRow
dr(1) = EventNumber.Text
dr(2) = Datagridview1.CurrentRow.Cells(1).Value.ToString
Me._CAD_050107DataSet.Tables(0).Rows.Add(dr)

I am not sure how to adapt for the names for datagridview2

Sorry for the "do it" not really politically correct. LOL
Sorry I got it. Didn't mean to sound stupid.

Dim dr As DataRow = Me._CAD_050107DataSet.Tables("assignedvehicles").NewRow
        'Put values into row
        dr(1) = EventNumber.Text
        dr(2) = Datagridview1.CurrentRow.Cells(1).Value.ToString
        'and so on, making sure you put values in any ...
        '... fields/columns that do not allow null
        Me._CAD_050107DataSet.Tables("assignedvehicles").Rows.Add(dr)