Psst! Did you know DevelopmentNow is a mobile web site design agency?

Contact us for help mobilizing your site, or to sign up for our beta Mobile Web SDK!
all groups > dotnet framework > april 2008 >

dotnet framework : Binding a generic List to GridView


Andreas Hartje
4/21/2008 7:22:41 PM
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
Mike Urquiola
4/22/2008 11:54:04 AM
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


[quoted text, click to view]

Chris Shepherd
4/22/2008 1:51:24 PM
[quoted text, click to view]

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

Andreas Hartje
4/23/2008 1:31:01 AM
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


[quoted text, click to view]
Mike Urquiola
4/23/2008 10:03:40 AM
Store the BindingList in session, rather than the person.


[quoted text, click to view]
Andreas Hartje
4/23/2008 7:11:48 PM
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...
[quoted text, click to view]
AddThis Social Bookmark Button