all groups > dotnet windows forms databinding > december 2005 >
You're in the

dotnet windows forms databinding

group:

binding source addnewitem question



binding source addnewitem question Shannon Ramirez
12/13/2005 2:24:47 PM
dotnet windows forms databinding: I have a datasource that is bound to a class i created. I changed the
datasource so that it woudl be using the detail view, not the datagrid and
drug it over.. below is what I'm using to bind the class to the
bindingsource

With oCampers

..LoadFromDB()

oCampers = .Load("") 'load all

End With

Me.CampersBindingSource.DataSource = oCampers



that brings back the data. if I create a command button and do the
following, it adds a new row and I'm able to set teh detail information to
the new row so the data can be filled in.

Me.CampersBindingSource.AddNew()

Debug.WriteLine(Me.CampersBindingSource.CurrencyManager.Position.ToString)

Debug.WriteLine(Me.CampersBindingSource.Count)

Dim count1 As Integer = Me.CampersBindingSource.Count + 1

Me.CampersBindingSource.CurrencyManager.Position = count1



when I put that same code into the BindingNavigatorAddNewItem_Click event, a
new row is added but the row it sets itself to goes back to 1 when it exits
the event.



can somone help me understand what it is i'm not understanding.

thanks

shannon

Re: binding source addnewitem question Shannon Ramirez
12/14/2005 1:00:20 PM
Hey Bart.. was hoping you'd respond.

I was trying to write some code because without doing anything other then
the drag and drop and adding the datasource, when I clicked the plus sign it
did add a record, but did not navigate to that last record.. so I figured it
was something that I had to code for.

oCampers is a custom class that i'm working on.

hope that give you what you need to educate me..
thanks
shannnon



[quoted text, click to view]

Re: binding source addnewitem question Bart Mermuys
12/14/2005 1:56:34 PM
Hi,

[quoted text, click to view]

I'm not sure why you need this code ? First of all when you call AddNew the
position should automatically change to the new record. And when you use a
BindingNavigator that's connected to a BindingSource then a new record
should be added when you push the Add button without having to add any code.
So maybe you could explain why you're doing this. What kind of object is
oCampers ?

Also notice that CampersBindingSource.Position is a shortcurt for
CampersBindingSource.CurrencyManager.Position.

HTH,
Greetings

[quoted text, click to view]

Re: binding source addnewitem question Shannon Ramirez
12/14/2005 2:32:15 PM
Hey.. i created a new form and again.. when I click on the add button it
adds a row, but the display shows row 1.

Here is the Campers class... thanks for taking a look. sorry about the
formatting.. is there a better way to post this kind of stuff so the
formatting is there and would make it easer to read
Option Explicit On

Imports System

Imports System.Data

Imports System.Data.SqlClient 'Have to have a reference to System.Data from
Project/Add Reference

'had to set a reference to System.XML to have the DataSet

Imports System.ComponentModel

Namespace LE

Public Class Campers

Inherits Component

Implements IList

Private mds As New DataSet

' protected property List

Private m_List As New ArrayList

'-------------------------------------

' public members

'-------------------------------------

Public Function Add(ByVal item As Camper) As Camper

Me.List.Add(item)

Return item

End Function

Default Public Property Item(ByVal index As Integer) As Camper

Get

Return DirectCast(Me.List(index), Camper)

End Get

Set(ByVal Value As Camper)

Me.List(index) = Value

End Set

End Property

Public Sub Remove(ByVal item As Camper)

Me.List.Remove(item)

End Sub

'-------------------------------------

' protected members

'-------------------------------------

Protected ReadOnly Property List() As ArrayList

Get

Return m_List

End Get

End Property

'-------------------------------------

' IList public members

'-------------------------------------

Public Sub CopyTo(ByVal array As System.Array, ByVal index As Integer)
Implements System.Collections.ICollection.CopyTo

Me.List.CopyTo(array, index)

End Sub

Public ReadOnly Property Count() As Integer Implements
System.Collections.ICollection.Count

Get

Return Me.List.Count

End Get

End Property

Public ReadOnly Property IsSynchronized() As Boolean Implements
System.Collections.ICollection.IsSynchronized

Get

Return Me.List.IsSynchronized

End Get

End Property

Public ReadOnly Property SyncRoot() As Object Implements
System.Collections.ICollection.SyncRoot

Get

Return Me.List.SyncRoot

End Get

End Property

Public Function GetEnumerator() As System.Collections.IEnumerator Implements
System.Collections.IEnumerable.GetEnumerator

Return Me.List.GetEnumerator()

End Function

Public Sub Clear() Implements System.Collections.IList.Clear

Me.List.Clear()

End Sub

Public ReadOnly Property IsFixedSize() As Boolean Implements
System.Collections.IList.IsFixedSize

Get

Return Me.List.IsFixedSize

End Get

End Property

Public ReadOnly Property IsReadOnly() As Boolean Implements
System.Collections.IList.IsReadOnly

Get

Return Me.List.IsReadOnly

End Get

End Property

Public Sub RemoveAt(ByVal index As Integer) Implements
System.Collections.IList.RemoveAt

Me.List.RemoveAt(index)

End Sub

'-------------------------------------

' private members

'-------------------------------------

Private Function Add(ByVal value As Object) As Integer Implements
System.Collections.IList.Add

Me.List.Add(value)

End Function

Private Function Contains(ByVal value As Object) As Boolean Implements
System.Collections.IList.Contains

Return Me.List.Contains(value)

End Function

Private Function IndexOf(ByVal value As Object) As Integer Implements
System.Collections.IList.IndexOf

Return Me.List.IndexOf(value)

End Function

Private Sub Insert(ByVal index As Integer, ByVal value As Object) Implements
System.Collections.IList.Insert

Me.List.Insert(index, value)

End Sub

Private Property ListItem(ByVal index As Integer) As Object Implements
System.Collections.IList.Item

Get

If index >= 0 AndAlso index < Me.List.Count Then

Return Me.List(index)

Else

Return Nothing

End If

End Get

Set(ByVal Value As Object)

Me.List(index) = Value

End Set

End Property

Private Sub Remove(ByVal value As Object) Implements
System.Collections.IList.Remove

Me.List.Remove(value)

End Sub

Public Sub LoadFromDB()

Dim cn As New SqlConnection

Dim cm As New SqlCommand

Dim ds As New DataSet

Dim da As New SqlDataAdapter

Dim dbcon As New LE.DbInfo

Try

cn = New SqlConnection(dbcon.Monitor)

'cn = New
SqlConnection("Pwd=sidney;UID=sa;server=127.0.0.1;database=dbLittleEden")

cm = New SqlCommand("spCamper_List", cn)

cm.CommandType = Data.CommandType.StoredProcedure

cm.Parameters.Add("@intTblCamperID", Data.SqlDbType.Int).Value = 0

'cm.Parameters.Add("@intDeptID", Data.SqlDbType.Int).Value = mintOrgID

da.SelectCommand = cm

cn.Open()

cm.ExecuteNonQuery()

da.Fill(ds)

mds = ds

Catch ex As Exception

Debug.WriteLine("LoadFromDb error: " & ex.Message.ToString)

Finally

cn.Dispose()

cm.Dispose()

da.Dispose()

ds.Dispose()

End Try

End Sub

Public Function Load(ByVal pSelect As String) As Campers

Dim drow As DataRow

Dim oCamper As Camper

Dim oCampers As New Campers

Dim rows



Dim dt As DataTable = mds.Tables(0)

If pSelect.Length = 0 Then

'Dim drows = dt.Select

rows = dt.Rows

Else

rows = dt.Select(pSelect)

End If

For Each drow In rows

oCamper = New Camper

With oCamper

..Id = drow("intTblCamperID")

..LegacyCode = drow("intLegacyCode")

..FirstName = drow("vcFirstName")

..LastName = drow("vcLastName")

..Spouse = drow("vcSpouseName")

..WinterName = drow("vcWinterName")

..WinterContact = drow("vcWinterContactName")

..Address1 = drow("vcAddress1")

..Address2 = drow("vcAddress2")

..City = drow("vcCity")

..State = drow("vcState")

..Zip = drow("vcZip")

..Phone1 = drow("vcPhone1")

..Phone2 = drow("vcPhone2")

..Phone1Desc = drow("vcPhone1Desc")

..Phone2Desc = drow("vcPhone2Desc")

..Notes = drow("vcNotes")

..OtherDeposits = drow("monDeposits")

..Deceased = drow("bDeceased")

End With

oCampers.Add(oCamper)

oCamper = Nothing

Next

Return oCampers

oCampers = Nothing

dt.Dispose()

End Function

Public Function Load4OrgDS(ByVal pSelect As String) As Campers

Dim drow As DataRow

Dim oCamper As Camper

Dim oCampers As New Campers

Dim rows



Dim dt As DataTable = mds.Tables(0)

If pSelect.Length = 0 Then

'Dim drows = dt.Select

rows = dt.Rows

Else

rows = dt.Select(pSelect)

End If

For Each drow In rows

oCamper = New Camper

With oCamper

..ID = drow("intTblCamperId")

..LegacyCode = drow("intCode")

..FirstName = drow("vcFName")

..LastName = drow("vcLName")

..Spouse = drow("vcSpouse")

..WinterName = drow("vcWName")

End With

oCampers.Add(oCamper)

oCamper = Nothing

Next

Return oCampers

oCampers = Nothing

dt.Dispose()

End Function

End Class

End Namespace

Re: binding source addnewitem question Shannon Ramirez
12/14/2005 2:39:00 PM
was wondering if anyone had a couple of good links to using your own custom
classes as a data source. I don't want to access sql tables directly.. i'd
like to use sp's. seems to me that creating a custom class adds a business
layer to all this and would be a good way to go. but doing the saves,
deletes and updates i'm not understanding yet. All the info I'm finding is
on going direct to the database using table adapters. Hoping someone has
some links so that I can learn. I'm a sql guy trying to learn vb.

thanks
shannon
[quoted text, click to view]

Re: binding source addnewitem question Shannon Ramirez
12/14/2005 2:50:25 PM
sorry to post again.. Bart.. didn't answer your question. I'm using the
released version of vs 2005 pro.

Microsoft Visual Studio 2005
Version 8.0.50727.42 (RTM.050727-4200)
Microsoft .NET Framework
Version 2.0.50727

Installed Edition: Professional

Microsoft Visual Basic 2005 77626-009-0000007-41834
Microsoft Visual Basic 2005


thanks shannon
[quoted text, click to view]

Re: binding source addnewitem question Bart Mermuys
12/14/2005 9:03:38 PM
Hi Shannon,

[quoted text, click to view]

I'm afraid it's really not enough information. The thing is that it
normally does work when you drag a Data Source on the Form. So before
trying any work-arounds, i would first try to figure out why it doesn't
work. Unfortunately without seeing any source code this is virtually
impossible for me.

The first thing i would do is drag the Data Source on a new Form and check
if the same problem happens.

If it does then i would check the custom class, what is oCampers exactly ?

If it is a Custom Collection (list) then there might be a small chance
something is wrongly implemented (Add) and then i would ask you to post the
code for this custom collection.

What version of VS2005 are you using, beta or final release ?

HTH,
Greetings


[quoted text, click to view]

Re: binding source addnewitem question Bart Mermuys
12/14/2005 10:11:40 PM
Hi,

[quoted text, click to view]

There are probely tools for this, but i don't mind the wrong formatting.

Here is the problem (part of your Campers class):

Private Function Add(ByVal value As Object) As Integer Implements
System.Collections.IList.Add

Me.List.Add(value)

End Function

Notice that the function should return an Integer while you don't return
anything and vb not complaining about it makes it even harder, the function
should infact return the index of the newly added record, fortunatelly
"Me.List.Add" returns that index, so all you need to do is add "Return":

Private Function Add(ByVal value As Object) As Integer Implements
System.Collections.IList.Add

Return Me.List.Add(value)

End Function

The index is used by the BindingSource to position it to the last record.


-----------

You could simplify things a lot if you base your Custom Collection on a
generic BindingList(System.ComponentModel), it's a generic class that
provides typed methods and basic support for DataBinding:

Public Class Campers
Inherits System.ComponentModel.BindingList(Of Camper)

' Typed Methods are already implemented
' all you need to do is add additional stuff

End Class


HTH
Greetings


[quoted text, click to view]
Re: binding source addnewitem question Shannon Ramirez
12/15/2005 8:23:08 AM
Wow.. how cool is that.. i did the inherits and fixed a few things and
boom.. so cool...

I've done a lot of web digging and hadn't seen that yet.. I'll have to read
up on it on msdn..
thanks for the help..

[quoted text, click to view]
AddThis Social Bookmark Button