Dim bookid As Integer = CType(bookidlabel.Text, Integer) If Not (bookidlist.Contains(bookid)) Then bookidlist.Add(bookid) End If End Sub
// C# protected void RowChanged( object sender, System.EventArgs e) { DataGridItem dgi = (DataGridItem)(((Control)sender).NamingContainer); Label bookidlabel = (Label) dgi.Cells[0].Controls[1]; int bookid = int.Parse(bookidlabel.Text); if (!bookidlist.Contains(bookid)) { bookidlist.Add(bookid); } } Note The method cannot be private, or you will not be able to bind to it later. It is helpful to understand that change events do not, by default, post the page back to the server. Instead, the event is raised only when the page is posted some other way (usually via a Click event). During page processing, the page and its controls are initialized, and then all change events are raised. Only when the change event's handlers have finished is the Click event raised for the control that caused the post.
On to the RowChanged method illustrated above. The code needs to get the book ID out of the current item. The event does not pass the item to you (as it does for many DataGrid events, for example), so you have to work backwards. From the sender argument of the event, get the NamingContainer property, which will be the grid item. From there, you can drill back down to get the value of the Label control that displays the book ID.
You need to check that the book ID is not already in the array. Each control in the row raises the event individually, so if there has been a change in more than one control, you could potentially end up adding the book ID to the array more than once.
The change events for controls are always raised and handled before click events. Therefore, you can build the array list in the change event and know that it will be available when the event handler runs for the button click that posted the form (in this example, the btnUpdate_Click handler).
Now that you have the array list, you can make a minor modification to the handler that manages the update. In the btnUpdate_Click, when you iterate through the data grid items, add a test to see if the current book ID is in the array list; if so, make the update.
' Visual Basic Private Sub btnUpdate_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnUpdate.Click Dim i As Integer Dim dgi As DataGridItem 'Rest of declarations here