Question Clearing selected rows in DataGrid

gchq

Well-known member
Joined
Dec 14, 2007
Messages
168
Programming Experience
10+
The code below works fine the first time it is loaded, but if the navigate back button is used the selected row remains the same as the last input - I have tried a myriad ways to clear it, but sadly nothing seems to work....


VB.NET:
 Try
            Mouse.OverrideCursor = Cursors.Wait
            WebPagesCanvas.Children.Clear()
            Dim DGV As New DataGrid
            With DGV
                .Name = "WebPages_DGV"
                .HorizontalAlignment = Windows.HorizontalAlignment.Stretch
                .VerticalAlignment = Windows.VerticalAlignment.Stretch
                .CanUserAddRows = False
                .CanUserDeleteRows = False
                .IsReadOnly = True
                .SetBinding(ItemsControl.ItemsSourceProperty, New Binding With {.Source = Nothing})
            End With



            AddHandler DGV.MouseDoubleClick, AddressOf WebPages_SelectPage
            Dim vO As Object = WebPagesCanvas.FindName("WebPages_DGV")
            If vO Is Nothing Then
                Me.RegisterName("WebPages_DGV", DGV)

            End If
            WebPagesCanvas.Children.Add(DGV)


            vService = New ServiceReference1.Service1Client
            strSQL = "SELECT Page_ID as ID, Page_Name as Name, Menu_Sub_MainID as Sub, Menu_Position as Position FROM HOA3_Pages"
            Dim DS As System.Data.DataSet = vService.ReturnDataSet(strSQL, 2)
            Dim DT As System.Data.DataTable = DS.Tables(0).Copy
            DGV.SetBinding(ItemsControl.ItemsSourceProperty, New Binding With {.Source = DT})
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            Mouse.OverrideCursor = Nothing
        End Try


VB.NET:
 Private Sub WebPages_SelectPage(ByVal sender As Object, ByVal e As EventArgs)
        Try
            Dim DGV As DataGrid = WebPagesCanvas.FindName("WebPages_DGV")
            If DGV.SelectedItems.Count = 1 Then
                Dim row As System.Data.DataRowView = DGV.SelectedItems(0)
                WebPage_ID = row("ID")
                MessageBox.Show(WebPage_ID)
                Dim vWebpage As New WebPage
                NewRecord = False
                Me.NavigationService.Navigate(vWebpage)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        
    End Sub
 
This was resolved by moving the code from Loaded to Initialised :) - This is more like writing asp.NET
 
Back
Top