Change Datasource RRS feed

  • Question

  • This is probably going to be a very simple question. Trying to do this in VB.NET.

    I have a simple Gridview "Gridview3" bound to a DataSource with ID "NonAutoDS". Works perfectly.

    I created another DataSource with ID "AutoDS". The SQL Query is the same except for a minor change and will not affect any of the columns.

    I have a Button and all I want it to do is OnClick to change the DS from NonAutoDS to AutoDS and refresh the Gridview 3 with the new data.

     Protected Sub AutoB_Click(sender As Object, e As EventArgs) Handles AutoB.Click
            GridView3.DataSourceID = "AutoDS"
    End Sub

    Also tried

        Protected Sub AutoB_Click(sender As Object, e As EventArgs) Handles AutoB.Click
            GridView3.DataSource = AutoDS
        End Sub

    It's been a while since I coded so I'm a newbie again so please excuse the simple dumb question, but what am I doing wrong?

    I know AutoDS works because if I bind it to the GV as the default, I get the right info.

    Thanks in advance.


    Friday, November 15, 2019 7:41 PM

All replies

  • Hi

    I think you may be using VB in a WPF project of which I know nothing.

    However, here is a working example in VB.Forms whichmay work in WPF (though I believe WPF has much more extensive data handling)

    ' Form1 with a DataGridView1
    ' and Button1 (.Text = "NonAutoDS")
    Option Strict Off
    Option Explicit On
    Public Class Form1
    	Dim autoDS As New DataTable("Freddy")
    	Dim NonAutoDS As New DataTable("Freddy2")
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		With autoDS
    			For i As Integer = 1 To 9
    				.Rows.Add(i, i * 3)
    		End With
    		With NonAutoDS
    			For i As Integer = 1 To 9
    				.Rows.Add(i, i * 3, i / 3)
    		End With
    		GridView3.DataSource = autoDS
    	End Sub
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    		Select Case Button1.Text
    			Case "NonAutoDS"
    				GridView3.DataSource = NonAutoDS
    				Button1.Text = "AutoDS"
    			Case Else
    				GridView3.DataSource = autoDS
    				Button1.Text = "NonAutoDS"
    		End Select
    	End Sub
    End Class

    Regards Les, Livingston, Scotland

    Friday, November 15, 2019 8:06 PM
  • Hello,

    The following is devoid of project type. For WPF the appropriate path would be in a classes implementing MVVM where using conventional data access via connection and command you can change the table as shown in ChangeDatabaseExample and for changing the initial catalog ChangeInitialCatalogExample.

    Imports System.Data.SqlClient
    Public Class Class1
        Private ConnectionString As String = "Data Source=KARENS-PC;Initial Catalog=NorthWindAzure;" &
                                             "Integrated Security=True"
        Public Sub ChangeInitialCatalogExample(ByVal catalog As String)
            Dim builder = New SqlConnectionStringBuilder()
            builder.ConnectionString = ConnectionString
            builder.InitialCatalog = catalog
            Using cn As New SqlConnection With {.ConnectionString = builder.ConnectionString}
            End Using
        End Sub
        Public Sub ChangeDatabaseExample(ByVal database As String)
            Using cn As New SqlConnection With {.ConnectionString = ConnectionString}
                Using cmd As New SqlCommand With {.Connection = cn}
                    cmd.CommandText = $"SELECT FirstName, LastName FROM {database}"
                End Using
            End Using
        End Sub
    End Class

    To change the server from a connection

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Friday, November 15, 2019 8:11 PM
  • Asp.Net is stateless. 

    Therefore you have to have saved the datasource in any way (for instance a session.item) or you have to get again the data before you can bind it to the GridView again


    Saturday, November 16, 2019 9:48 AM