Hi there,
sorry for crossposting, it's getting late in the evening here in Europe :-(

I have a little problem binding a generic list to a GridView. The List gets
filled
with data from textboxes (via properties in class Person, see below) and I
want the grid to show each new set of textbox strings to show up as a new
row in the grid. i mahaged to do this by definig a little sessionmanagement
but the really problem now is that each time I post the strings to the
server, the previus set of data/strings in my grid (and also in the List)
gets overritten with the last input of strings. In the end i get thre or
four or more rows in my dataset with the strings of the textbox input.
Any idea about this?
Thanks very much, Andi

CODE:
Public Class Person
Public lpers As New List(Of Person)
Private vorname As String
Private nachname As String
Private alter As String
Private geburtsort As String

Public Property GetPersVorname() As String
Get
Return vorname
End Get
Set(ByVal value As String)
vorname = value
End Set
End Property
Public Property GetPersNachname() As String
Get
Return nachname
End Get
Set(ByVal value As String)
nachname = value
End Set
End Property
Public Property GetPersAlter() As String
Get
Return alter
End Get
Set(ByVal value As String)
alter = value
End Set
End Property
Public Property GetPersGeburtsort() As String
Get
Return geburtsort
End Get
Set(ByVal value As String)
geburtsort = value
End Set
End Property

End Class

Dim p As New Person

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
If IsNothing(Session("Person")) Then
Session("Person") = New Person
End If
p = Session("Person")
End Sub

Public Sub arrFill()
p.GetPersVorname = TextBox1.Text
p.GetPersNachname = TextBox2.Text
p.GetPersAlter = TextBox3.Text
p.GetPersGeburtsort = TextBox4.Text

p.lpers.Add(p)
GridView1.DataSource = p.lpers
GridView1.DataBind()
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
arrFill()

End Sub

Re: Binding a generic List to GridView by Mike

Mike
Tue Apr 22 10:54:04 CDT 2008

The problem is you only have one person object, and you keep overwriting the
properties. Try this.

Public Sub arrFill()
p = new Person(); -Create new person object here.
p.GetPersVorname = TextBox1.Text
p.GetPersNachname = TextBox2.Text
p.GetPersAlter = TextBox3.Text
p.GetPersGeburtsort = TextBox4.Text

p.lpers.Add(p)
GridView1.DataSource = p.lpers
GridView1.DataBind()
End Sub


"Andreas Hartje" <andreas.hartje@unibw.de> wrote in message
news:911D5625-4F8F-4381-A99B-20BB82978CDA@microsoft.com...
> Hi there,
> sorry for crossposting, it's getting late in the evening here in Europe
> :-(
>
> I have a little problem binding a generic list to a GridView. The List
> gets filled
> with data from textboxes (via properties in class Person, see below) and I
> want the grid to show each new set of textbox strings to show up as a new
> row in the grid. i mahaged to do this by definig a little
> sessionmanagement
> but the really problem now is that each time I post the strings to the
> server, the previus set of data/strings in my grid (and also in the List)
> gets overritten with the last input of strings. In the end i get thre or
> four or more rows in my dataset with the strings of the textbox input.
> Any idea about this?
> Thanks very much, Andi
>
> CODE:
> Public Class Person
> Public lpers As New List(Of Person)
> Private vorname As String
> Private nachname As String
> Private alter As String
> Private geburtsort As String
>
> Public Property GetPersVorname() As String
> Get
> Return vorname
> End Get
> Set(ByVal value As String)
> vorname = value
> End Set
> End Property
> Public Property GetPersNachname() As String
> Get
> Return nachname
> End Get
> Set(ByVal value As String)
> nachname = value
> End Set
> End Property
> Public Property GetPersAlter() As String
> Get
> Return alter
> End Get
> Set(ByVal value As String)
> alter = value
> End Set
> End Property
> Public Property GetPersGeburtsort() As String
> Get
> Return geburtsort
> End Get
> Set(ByVal value As String)
> geburtsort = value
> End Set
> End Property
>
> End Class
>
> Dim p As New Person
>
> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles Me.Load
> If IsNothing(Session("Person")) Then
> Session("Person") = New Person
> End If
> p = Session("Person")
> End Sub
>
> Public Sub arrFill()
> p.GetPersVorname = TextBox1.Text
> p.GetPersNachname = TextBox2.Text
> p.GetPersAlter = TextBox3.Text
> p.GetPersGeburtsort = TextBox4.Text
>
> p.lpers.Add(p)
> GridView1.DataSource = p.lpers
> GridView1.DataBind()
> End Sub
>
> Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles Button1.Click
> arrFill()
>
> End Sub
>



Re: Binding a generic List to GridView by Chris

Chris
Tue Apr 22 12:51:24 CDT 2008

Mike Urquiola wrote:
> The problem is you only have one person object, and you keep overwriting the
> properties. Try this.
>
> Public Sub arrFill()
> p = new Person(); -Create new person object here.
> p.GetPersVorname = TextBox1.Text
> p.GetPersNachname = TextBox2.Text
> p.GetPersAlter = TextBox3.Text
> p.GetPersGeburtsort = TextBox4.Text
>
> p.lpers.Add(p)
> GridView1.DataSource = p.lpers
> GridView1.DataBind()
> End Sub

Also, you should be using BindingList instead of List since BindingList will
publish changes while List will not.

Chris.

Re: Binding a generic List to GridView by AndreasHartje

AndreasHartje
Wed Apr 23 03:31:01 CDT 2008

Hi Mike,
thanks for your answer, I added your "p = new Person" to my Sub arrFill(),
but unfortunately this does not make any difference, at least not the one you
intended.
No new row ist added after a postback, instead the only row gets updated
withe the changes made in the textboxes. I already use the BindList intead of
List as Chris (see follow up post to your post) ... no difference at all.
What else can do about this?
Thanks in advance for all your suggestions ....
Andi

Code now lokks like:
Public Class Person
...
End Class

Dim p As New Person()

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
If IsNothing(Session("Person")) Then
Session("Person") = New Person()
Else
p = Session("Person")
End If
End Sub

Public Sub arrFill()
p = New Person()
p.Vorname = TextBox1.Text
p.Nachname = TextBox2.Text
p.Alter = TextBox3.Text
p.Geburtsort = TextBox4.Text
p.list_pers.Add(p)

GridView1.DataSource = p.list_pers
Me.DataBind()
End Sub


"Mike Urquiola" wrote:

> The problem is you only have one person object, and you keep overwriting the
> properties. Try this.
>
> Public Sub arrFill()
> p = new Person(); -Create new person object here.
> p.GetPersVorname = TextBox1.Text
> p.GetPersNachname = TextBox2.Text
> p.GetPersAlter = TextBox3.Text
> p.GetPersGeburtsort = TextBox4.Text
>
> p.lpers.Add(p)
> GridView1.DataSource = p.lpers
> GridView1.DataBind()
> End Sub
>
>
> "Andreas Hartje" <andreas.hartje@unibw.de> wrote in message
> news:911D5625-4F8F-4381-A99B-20BB82978CDA@microsoft.com...
> > Hi there,
> > sorry for crossposting, it's getting late in the evening here in Europe
> > :-(
> >
> > I have a little problem binding a generic list to a GridView. The List
> > gets filled
> > with data from textboxes (via properties in class Person, see below) and I
> > want the grid to show each new set of textbox strings to show up as a new
> > row in the grid. i mahaged to do this by definig a little
> > sessionmanagement
> > but the really problem now is that each time I post the strings to the
> > server, the previus set of data/strings in my grid (and also in the List)
> > gets overritten with the last input of strings. In the end i get thre or
> > four or more rows in my dataset with the strings of the textbox input.
> > Any idea about this?
> > Thanks very much, Andi
> >
> > CODE:
> > Public Class Person
> > Public lpers As New List(Of Person)
> > Private vorname As String
> > Private nachname As String
> > Private alter As String
> > Private geburtsort As String
> >
> > Public Property GetPersVorname() As String
> > Get
> > Return vorname
> > End Get
> > Set(ByVal value As String)
> > vorname = value
> > End Set
> > End Property
> > Public Property GetPersNachname() As String
> > Get
> > Return nachname
> > End Get
> > Set(ByVal value As String)
> > nachname = value
> > End Set
> > End Property
> > Public Property GetPersAlter() As String
> > Get
> > Return alter
> > End Get
> > Set(ByVal value As String)
> > alter = value
> > End Set
> > End Property
> > Public Property GetPersGeburtsort() As String
> > Get
> > Return geburtsort
> > End Get
> > Set(ByVal value As String)
> > geburtsort = value
> > End Set
> > End Property
> >
> > End Class
> >
> > Dim p As New Person
> >
> > Protected Sub Page_Load(ByVal sender As Object, ByVal e As
> > System.EventArgs) Handles Me.Load
> > If IsNothing(Session("Person")) Then
> > Session("Person") = New Person
> > End If
> > p = Session("Person")
> > End Sub
> >
> > Public Sub arrFill()
> > p.GetPersVorname = TextBox1.Text
> > p.GetPersNachname = TextBox2.Text
> > p.GetPersAlter = TextBox3.Text
> > p.GetPersGeburtsort = TextBox4.Text
> >
> > p.lpers.Add(p)
> > GridView1.DataSource = p.lpers
> > GridView1.DataBind()
> > End Sub
> >
> > Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
> > System.EventArgs) Handles Button1.Click
> > arrFill()
> >
> > End Sub
> >
>
>
>

Re: Binding a generic List to GridView by Mike

Mike
Wed Apr 23 09:03:40 CDT 2008

Store the BindingList in session, rather than the person.


"Andreas Hartje" <AndreasHartje@discussions.microsoft.com> wrote in message
news:5EB07BF5-5737-4CAD-B968-FDFD22479C47@microsoft.com...
> Hi Mike,
> thanks for your answer, I added your "p = new Person" to my Sub arrFill(),
> but unfortunately this does not make any difference, at least not the one
> you
> intended.
> No new row ist added after a postback, instead the only row gets updated
> withe the changes made in the textboxes. I already use the BindList intead
> of
> List as Chris (see follow up post to your post) ... no difference at all.
> What else can do about this?
> Thanks in advance for all your suggestions ....
> Andi
>
> Code now lokks like:
> Public Class Person
> ...
> End Class
>
> Dim p As New Person()
>
> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles Me.Load
> If IsNothing(Session("Person")) Then
> Session("Person") = New Person()
> Else
> p = Session("Person")
> End If
> End Sub
>
> Public Sub arrFill()
> p = New Person()
> p.Vorname = TextBox1.Text
> p.Nachname = TextBox2.Text
> p.Alter = TextBox3.Text
> p.Geburtsort = TextBox4.Text
> p.list_pers.Add(p)
>
> GridView1.DataSource = p.list_pers
> Me.DataBind()
> End Sub
>
>


Re: Binding a generic List to GridView by Andreas

Andreas
Wed Apr 23 12:11:48 CDT 2008

THAT'S IT!!!
Thanks very much, Mike ... you saved my evening ... and more!
Andi

"Mike Urquiola" <mike@urquiola.org> schrieb im Newsbeitrag
news:OGFcWrUpIHA.1580@TK2MSFTNGP06.phx.gbl...
> Store the BindingList in session, rather than the person.
>
>
> "Andreas Hartje" <AndreasHartje@discussions.microsoft.com> wrote in
> message news:5EB07BF5-5737-4CAD-B968-FDFD22479C47@microsoft.com...
>> Hi Mike,
>> thanks for your answer, I added your "p = new Person" to my Sub
>> arrFill(),
>> but unfortunately this does not make any difference, at least not the one
>> you
>> intended.
>> No new row ist added after a postback, instead the only row gets updated
>> withe the changes made in the textboxes. I already use the BindList
>> intead of
>> List as Chris (see follow up post to your post) ... no difference at all.
>> What else can do about this?
>> Thanks in advance for all your suggestions ....
>> Andi
>>
>> Code now lokks like:
>> Public Class Person
>> ...
>> End Class
>>
>> Dim p As New Person()
>>
>> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
>> System.EventArgs) Handles Me.Load
>> If IsNothing(Session("Person")) Then
>> Session("Person") = New Person()
>> Else
>> p = Session("Person")
>> End If
>> End Sub
>>
>> Public Sub arrFill()
>> p = New Person()
>> p.Vorname = TextBox1.Text
>> p.Nachname = TextBox2.Text
>> p.Alter = TextBox3.Text
>> p.Geburtsort = TextBox4.Text
>> p.list_pers.Add(p)
>>
>> GridView1.DataSource = p.list_pers
>> Me.DataBind()
>> End Sub
>>
>>
>