From: Mike on
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.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(a)unibw.de> wrote in message
news:911D5625-4F8F-4381-A99B-20BB82978CDA(a)microsoft.com...

Previous Posts In This Thread:

On Monday, April 21, 2008 1:22 PM
Andreas Hartje wrote:

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
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

On Tuesday, April 22, 2008 11:54 AM
Mike Urquiola wrote:

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


"Andreas Hartje" <andreas.hartje(a)unibw.de> wrote in message
news:911D5625-4F8F-4381-A99B-20BB82978CDA(a)microsoft.com...

On Tuesday, April 22, 2008 1:51 PM
Chris Shepherd wrote:

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.

On Wednesday, April 23, 2008 4:31 AM
AndreasHartj wrote:

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


"Mike Urquiola" wrote:

On Wednesday, April 23, 2008 10:03 AM
Mike Urquiola wrote:

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

On Wednesday, April 23, 2008 1:11 PM
Andreas Hartje wrote:

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

"Mike Urquiola" <mike(a)urquiola.org> schrieb im Newsbeitrag


Submitted via EggHeadCafe - Software Developer Portal of Choice
Sending SMTP email from within BizTalk Orchestration
http://www.eggheadcafe.com/tutorials/aspnet/9dd0f346-baf9-4674-a50f-1716445b26bc/sending-smtp-email-from-w.aspx