11 Replies - 1735 Views - Last Post: 15 March 2013 - 04:42 AM Rate Topic: -----

#1 fabiocrj   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 25
  • Joined: 09-February 11

Vb.net new prefilled item in databinding

Posted 08 March 2013 - 12:12 PM

Hi all,
I have many bounded items in a Windows Form. When user fill the fields (textboxes, checkboxes, etc) and save then, i want the user to have the option to "save as new item". So, he only need to change the fields that are different from the last item.
Here is an example:
The form have 4 itens: id, model, serial number and description. If the model and description are the same, only the filed "serial number" would have to be changed.
When i use ListaBindingSource.AddNew(), all fields will be blanked. I want to copy all the items when i add a new item in the bindingsource
How can i do it?

thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Vb.net new prefilled item in databinding

#2 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Re: Vb.net new prefilled item in databinding

Posted 09 March 2013 - 06:00 AM

The simplest solution would be to create an object for each of the fields to hold their value
When the save button is clicked, before the .EndEdit() store the current controls values into each of the properties

In the add button set the controls to the values in the variables.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6829
  • View blog
  • Posts: 28,317
  • Joined: 12-December 12

Re: Vb.net new prefilled item in databinding

Posted 09 March 2013 - 11:53 AM

You might investigate DataTable.ImportRow which would copy the row. The row would be inserted at the end (as a new row) and you might use MoveLast() as in the following sample:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim dt = CType(bsCustomers.DataSource, DataTable)
    dt.ImportRow(dt.Select("Identifier = " & CType(bsCustomers.Current, DataRowView).Item("Identifier").ToString)(0))
    bsCustomers.MoveLast()
End Sub

Sample from this page.

I assume it is possible to use ImportRow(Current.Row..) somehow.
Was This Post Helpful? 0
  • +
  • -

#4 fabiocrj   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 25
  • Joined: 09-February 11

Re: Vb.net new prefilled item in databinding

Posted 11 March 2013 - 07:00 AM

Thank you for your answer. I really liked the approach, but i am having some problems:
My binding source is not a DataDable. It is a DataSet, and the DataMember is the DataTable... So i get the following error:
"Unable to cast object of type 'WindowsApplication1.dsDataSet' to type 'System.Data.DataTable'."
Any Ideas?
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6829
  • View blog
  • Posts: 28,317
  • Joined: 12-December 12

Re: Vb.net new prefilled item in databinding

Posted 11 March 2013 - 07:37 AM

My immediate thought is that, if there is only one table, you might use YourDataSet.DataTables(0), but I haven't looked in detail at the moment. Added: Well, I can't anyway without seeing your code.

If not, you should study the documentation.

This post has been edited by andrewsw: 11 March 2013 - 07:40 AM

Was This Post Helpful? 0
  • +
  • -

#6 fabiocrj   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 25
  • Joined: 09-February 11

Re: Vb.net new prefilled item in databinding

Posted 11 March 2013 - 09:11 AM

Ok, i am making some progress here :D
When i get some results i'll post again
Was This Post Helpful? 0
  • +
  • -

#7 fabiocrj   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 25
  • Joined: 09-February 11

Re: Vb.net new prefilled item in databinding

Posted 11 March 2013 - 09:33 AM

Ok, i´ve done what you said:
Dim dt = CType(MyDataSet.MyDataTable, DataTable)
Dim ID = CInt(CType(MyBindingSource.Current, DataRowView).Item("id"))
Dim Row = dt.Select("id = " & ID.ToString)(0)

everything goes fine until:
dt.ImportRow(Row)

And gives the error: "Column 'id' is constrained to be unique. Value '107' is already present.", which is ok, since i already have the id '107'... But it gives the same error when i do:
Row.Item("id") = 3851 '(Or any Number)

"Column 'id' is constrained to be unique. Value '107' is already present"
When I stop Debuggind and Start Debugging again, i get the message: "Column 'id' is constrained to be unique. Value '3851' is already present"
I just cant understand what is going on...

This post has been edited by fabiocrj: 11 March 2013 - 09:42 AM

Was This Post Helpful? 0
  • +
  • -

#8 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Re: Vb.net new prefilled item in databinding

Posted 11 March 2013 - 09:47 AM

It appears you're trying to enter a duplicate primary key. (most likely ID is an autonumber field)
Was This Post Helpful? 0
  • +
  • -

#9 fabiocrj   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 25
  • Joined: 09-February 11

Re: Vb.net new prefilled item in databinding

Posted 11 March 2013 - 09:58 AM

View PostCharlieMay, on 11 March 2013 - 09:47 AM, said:

It appears you're trying to enter a duplicate primary key. (most likely ID is an autonumber field)

Thatīs right, but when i try to force a different id, it gives the same error message... It doesnt matter if i do
Row.Item("id") = 3851 'or
Row.Item("id") = 7154551 'or
Row.Item("id") = 3808451 


Was This Post Helpful? 0
  • +
  • -

#10 C.Andrews   User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 169
  • Joined: 18-October 12

Re: Vb.net new prefilled item in databinding

Posted 11 March 2013 - 12:38 PM

View Postfabiocrj, on 11 March 2013 - 09:58 AM, said:

View PostCharlieMay, on 11 March 2013 - 09:47 AM, said:

It appears you're trying to enter a duplicate primary key. (most likely ID is an autonumber field)

Thatīs right, but when i try to force a different id, it gives the same error message... It doesnt matter if i do
Row.Item("id") = 3851 'or
Row.Item("id") = 7154551 'or
Row.Item("id") = 3808451 



If ID is set to autonumber, you're not allowed to change the value of the field, since that would mess up the autonumbering. Set the ID field to a numeric data type and see how that goes?
Was This Post Helpful? 0
  • +
  • -

#11 fabiocrj   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 25
  • Joined: 09-February 11

Re: Vb.net new prefilled item in databinding

Posted 11 March 2013 - 04:56 PM

View PostC.Andrews, on 11 March 2013 - 12:38 PM, said:

View Postfabiocrj, on 11 March 2013 - 09:58 AM, said:

View PostCharlieMay, on 11 March 2013 - 09:47 AM, said:

It appears you're trying to enter a duplicate primary key. (most likely ID is an autonumber field)

Thatīs right, but when i try to force a different id, it gives the same error message... It doesnt matter if i do
Row.Item("id") = 3851 'or
Row.Item("id") = 7154551 'or
Row.Item("id") = 3808451 



If ID is set to autonumber, you're not allowed to change the value of the field, since that would mess up the autonumbering. Set the ID field to a numeric data type and see how that goes?


Ok, Good idea.. Iīll check this and post a feedback here!
Thank You!!
Was This Post Helpful? 0
  • +
  • -

#12 fabiocrj   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 25
  • Joined: 09-February 11

Re: Vb.net new prefilled item in databinding

Posted 15 March 2013 - 04:42 AM

Hi all,

Iīve found the solution. I donīt know if itīs the best approach, but it works:
Me.MyBindingSource.AddNew()
For Each c As DataColumn In MyDataSet.MyDatabase.Columns
      If Not c.Unique Then Me.MyBindingSource.Item(newRow)(c.ColumnName) = Me.MyBindingSource.Item(newRow - 1)(c.ColumnName)
Next

What do you think about it? Any idea to improve it?

Thank You
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1