Add a new row on a modal form

Arg81

Well-known member
Joined
Mar 11, 2005
Messages
949
Location
Midlands, UK
Programming Experience
1-3
Think I may be having a blonde moment, but for the life of me I cannot get the following to work;

Form with Grid. Grid displays some simple data for child table.

To add a new row to the child table, want to use a popup form instead of the grid (certain fields aren't shown in the grid)

EDIT - Also need to bear in mind that sometimes there will not be any data in the child table, so will be adding a "first row" as such.

Now, how would I do this?

Would I need to open form2 and fill a datatable of the Child table, then call Bindingsource.AddNew() to add the new row?


At the moment I am using the following code on a button_click;

VB.NET:
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DsDevelopment.DWR_Revision.Rows.Count = 0 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]varDWRNumber = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].lblTSDWR.Text
varRevNumber = 1
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] frm [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] frmRevResponse
frm.ShowDialog()
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]varDWRNumber = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].lblTSDWR.Text
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].grdRevision.MoveFirst()
varRevNumber = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].grdRevision.Columns([/SIZE][SIZE=2][COLOR=#a31515]"RevisionNumber"[/COLOR][/SIZE][SIZE=2]).Value + 1
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] frm [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] frmRevResponse
frm.ShowDialog()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE]

Then on form2 load:

VB.NET:
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DWR_RevisionTableAdapter.FillByDWRNumber([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DsDevelopment.DWR_Revision, varDWRNumber)
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DWR_RevisionBindingSource.AddNew()
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].lblDWRNo.Text = varDWRNumber
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].lblRevisionNo.Text = varRevNumber
[/SIZE]

Now, if a row already exists in the child table (or at least 1 appears in the grid), it works fine. I get an error though if the child table is empty and this is the first row being added to the child table for the Parent ID.


Think it's one of those days.

Thanks,
 
Last edited:
Something like:

bindingsource.AddNew();
bindingsource.EndEdit();
myForm2.ShowDataOf(bindingSource);


Public Sub ShowDataOf(ByVal bs as BindingSource)
form2BindingSource.DataSource = bs
Me.ShowDialog
End Sub
 
Back
Top