Binding a generic List to GridView

  • Thread starter Thread starter Andreas Hartje
  • Start date Start date
A

Andreas Hartje

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
 
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
 
Mike said:
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.
 
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
 
How do you assign the properties in the collection to the DataKeyNames of the gridview?

GridView1.DataSource = p.lpers
GridView1.DataBind()

If I was binding to a SqlDataReader it might say:

GridView1.DataSource = myDataReader;
GridView1.DataKeyNames = new string[] { "FirstName", "Surname", "etc" };
GridView1.DataBind();

How do you populate the DataKeyNames when binding to a Generic List?

Thanks for any helpl.



Mike Urquiola wrote:

The problem is you only have one person object, and you keep overwriting the
22-Apr-08

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.Tex
p.GetPersNachname = TextBox2.Tex
p.GetPersAlter = TextBox3.Tex
p.GetPersGeburtsort = TextBox4.Tex

p.lpers.Add(p
GridView1.DataSource = p.lper
GridView1.DataBind(
End Su


Previous Posts In This Thread:

Binding a generic List to GridView
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
fille
with data from textboxes (via properties in class Person, see below) and
want the grid to show each new set of textbox strings to show up as a ne
row in the grid. i mahaged to do this by definig a little sessionmanagemen
but the really problem now is that each time I post the strings to th
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 o
four or more rows in my dataset with the strings of the textbox input
Any idea about this
Thanks very much, And

CODE
Public Class Perso
Public lpers As New List(Of Person
Private vorname As Strin
Private nachname As Strin
Private alter As Strin
Private geburtsort As Strin

Public Property GetPersVorname() As Strin
Ge
Return vornam
End Ge
Set(ByVal value As String
vorname = valu
End Se
End Propert
Public Property GetPersNachname() As Strin
Ge
Return nachnam
End Ge
Set(ByVal value As String
nachname = valu
End Se
End Propert
Public Property GetPersAlter() As Strin
Ge
Return alte
End Ge
Set(ByVal value As String
alter = valu
End Se
End Propert
Public Property GetPersGeburtsort() As Strin
Ge
Return geburtsor
End Ge
Set(ByVal value As String
geburtsort = valu
End Se
End Propert

End Clas

Dim p As New Perso

Protected Sub Page_Load(ByVal sender As Object, ByVal e A
System.EventArgs) Handles Me.Loa
If IsNothing(Session("Person")) The
Session("Person") = New Perso
End I
p = Session("Person"
End Su

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

p.lpers.Add(p
GridView1.DataSource = p.lper
GridView1.DataBind(
End Su

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

End Sub

The problem is you only have one person object, and you keep overwriting the
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



Re: Binding a generic List to GridView
Mike Urquiola wrote:

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

Chris.

Hi Mike,thanks for your answer, I added your "p = new Person" to my Sub
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


:

Store the BindingList in session, rather than the person.
Store the BindingList in session, rather than the person.

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



Submitted via EggHeadCafe - Software Developer Portal of Choice
Sending SMTP email from within BizTalk Orchestration
http://www.eggheadcafe.com/tutorial...f-1716445b26bc/sending-smtp-email-from-w.aspx
 
Back
Top