Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I'm back again and have another question:

I have a simple GridView ona webpage with several names in it:

- Alex
- Thomas
- Kate
- George

I want to add the names in a list after I select a name.

I use the SelectedIndexChanged event.

I have this code:
Public Class StudentNames
Inherits System.Web.UI.Page
Public List As New Generic.List(Of String)


VB
Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged

Name = GridView1.SelectedRow.Cells(0).Text

List.Add(Name)


I click on the name Alex in the GridView and Alex is added to the list. But when I click on Thomas in the GridView, it looks like the name Alex is gone from the list.

I have a Textbox1 where I count the selected names.

Textbox1.text = List.count


List.count = 1. After the add of the name Alex, I expect two items in list and not one.

What I'm doing wrong?

Sorry for my bad English.

Thanks in advance.

What I have tried:

Search on Microsoft website and Google
Posted
Updated 9-Sep-20 23:01pm
v15

I think you should harness the GridView event for 'Selected' / 'Clicked' - Im on my Mac so I can't easily tell you what that is, but if you're in Forms Design mode and select the GridView there's a way of seeing the events - so select the event and define a handler for it .. actually, this DataGridView.SelectionChanged Event (System.Windows.Forms) | Microsoft Docs[^] may help - there's even an event for Rows Added for example ... have a look at SelectionChanged

Knowing that, if you're still stuck you can Google 'VB.Net Gridview SelectionChanged example' and that should help you along
 
Share this answer
 
Comments
Coder4EU 6-Sep-20 6:31am    
Thanks for the reply. I use the SelectedIndexChanged event for my code, but this won't work correctly. It looks like the list is overwritten when I add a new Name (the old name is gone).
Garth J Lancaster 6-Sep-20 6:36am    
"but it won't work correctly.' not really helpful - why don't you use Improve question and add the SelectionIndexChanged event handler code you have, try to describe as clearly as possible why it doesn't work, and we'll see what we can do/suggest .. don't fret too much about the 'bad English' bit, we are quite tolerant unless the meaning is obscured, then we can always ask you
Coder4EU 6-Sep-20 6:43am    
Thank you for the reply and sorry for my unclear question. I improved my question. I hope my question is more clear.
Garth J Lancaster 6-Sep-20 6:52am    
sorry, without seeing the code all I can do is shrug - I can't set up a test from here either .. maybe someone else will understand from your description
Coder4EU 6-Sep-20 7:07am    
Thanks for the replies. I appreciate your efforts
Because every time that event fires you create a new list which will contain just one name. Your list should be declared and initialised outside of the event handler, so each event will add the name to the existing list.
 
Share this answer
 
Comments
Coder4EU 6-Sep-20 9:03am    
Thank you for the reply. I declared my list outside of the event handler, but the List.Count is still 1 after selection of another name in the GridView.
Richard MacCutchan 6-Sep-20 9:12am    
Yes, but you are still creating a new temporary one in the event handler.
Coder4EU 6-Sep-20 9:15am    
Thank you for the reply. Maybe my reply was a bit unclear.

I removed the list from the event handler and placed it outside the event handler. This result also in list.count is 1. I updated my question with my recent code.
Richard MacCutchan 6-Sep-20 10:06am    
Something else must be happening. You need to put a breakpoint on that code and use the debugger to trace through it to find out what.
Coder4EU 7-Sep-20 13:04pm    
Thank you for the reply. I will insert a breakpoint and analyze what is going wrong.
Quote:
VB.NET
Public Class StudentNames
Inherits System.Web.UI.Page
    Public List As New Generic.List(Of String)
Every request to your page, including a post-back, will create a new instance of your page class to process the request.

Anything stored in a field within that class will not be persisted between requests.

You'll need to store your list in ViewState for the changes to persist between requests.
VB.NET
Public Property List As Generic.List(Of String)
    Get
        Dim value As String() = DirectCast(ViewState("List"), String())
        If value Is Nothing Then Return New Generic.List(Of String)
        Return New Generic.List(Of String)(value)
    End Get
    Set(ByVal value As Generic.List(Of String))
        If value Is Nothing Then Throw New ArgumentNullException()
        ViewState("List") = value.ToArray()
    End Set
End Property
ASP.NET State Management Overview | Microsoft Docs[^]
 
Share this answer
 
v2
Comments
Coder4EU 10-Sep-20 5:13am    
Thank you for the response. In my SelectedIndexChanged event I also need to refer to the public property?
Richard Deeming 10-Sep-20 5:20am    
Anywhere you're currently referencing the field, you'd use the property instead.
Coder4EU 10-Sep-20 5:21am    
Thank you very much for the answer. My code is working well.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900