How to code a button to get data from a table in a database and display in a datagrid view?

I'm trying to code a button that has a SELECT statement to get information from a single table, but I want the information to be displayed as a data grid.

In a data grid view, this data will be stored in a different table in the same database.

I used to use a list box to display information, but I was unable to save it to the database.

This is the code I used for the list:

listbox.items.add
{while mydatareader.read
{add.("item_name")

      

Is there a way to show this in a data grid like I did in the list?

Im using datagrid textbox column column.

0


a source to share


2 answers


I'm sure there are many ways to do this, but try this. Let's say you have a database with a table named "TestSample" which has three columns named "Column1", "Column2", "Column3". Then add a ListView control to your form and name it "ListDisplay" and maybe two buttons for testing. Then you need to create columns, you can do it with code like this

Private Sub CreateColumns()
    'ListDisplay is the name of the ListView Control'
    ListDisplay.View = View.Details
    ListDisplay.FullRowSelect = True

    'Create Columns'
    ListDisplay.Columns.Add("Column1", 200, HorizontalAlignment.Left)
    ListDisplay.Columns.Add("Column2", 200, HorizontalAlignment.Left)
    ListDisplay.Columns.Add("Column3", 200, HorizontalAlignment.Left)
End Sub

      

this code can be called from the "Load Form" event, for example

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Setup Colums'
    CreateColumns()
End Sub

      

The next is to collect data from the database and insert back into the database .. these two functions can help

 Private Sub FillTable(ByVal connString As String, ByVal QueryString As String)
    'Clear Populated Items'
    ListDisplay.Items.Clear()

    'Setup Connection and query'
    Dim conn As SqlConnection = New SqlConnection(connString)
    Dim cmd As SqlCommand = New SqlCommand(QueryString, conn)
    Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)

    'Setup Dataset'
    Dim ds As New DataSet

    'Populate Dataset'
    conn.Open()
    da.Fill(ds, "TestSample")
    conn.Close()

    'Free up memory'
    cmd.Dispose()
    conn.Dispose()

    'Populate ListView Control'
    For Each dr As DataRow In ds.Tables("TestSample").Rows
        Dim items As New ListViewItem
        items.Text = dr.Item(0).ToString()
        items.SubItems.Add(dr.Item(1).ToString())
        items.SubItems.Add(dr.Item(2).ToString())

        ListDisplay.Items.Add(items)
    Next

End Sub

Private Sub InsertBack(ByVal connString As String, ByVal QueryString As String, ByVal ParameterOne As String, ByVal ParameterTwo As String, ByVal ParameterThree As String)
    'Setup Connection'
    Dim conn As SqlConnection = New SqlConnection(connString)
    Dim cmd As SqlCommand = New SqlCommand(QueryString, conn)

    'Setup Parameters'
    cmd.Parameters.Add("@Column1", SqlDbType.Text).Value = ParameterOne
    cmd.Parameters.Add("@Column2", SqlDbType.Text).Value = ParameterTwo
    cmd.Parameters.Add("@Column3", SqlDbType.Text).Value = ParameterThree

    'Process data: Insert to database'
    conn.Open()
    cmd.ExecuteNonQuery()
    conn.Close()

    'Free up memory'
    cmd.Dispose()
    conn.Dispose()

End Sub

      



with the "Click Event" button on the form you can call fill the ListView like this

 'Get data from database and populate ListView: Parameter requires connection string'
    FillTable("Data Source=SERVER-PC;Initial Catalog=Database;Persist Security Info=True;User ID=UsernameIfRequired;Password=PasswordIfRequired", "SELECT * FROM TestSample")

      

the connection string is a sample, you will need to provide your own, you will also need to change the query string according to your database. Finally, to insert data into the database, say from your ListView items, if it has changed, you can do it like ...

  'Loop through all Items in the ListView and send to database'
    For Each item As ListViewItem In ListDisplay.Items
        'Add data to the database from ListView Items: Parameter requres connection string and the 3 input items (could be as many as you want)'
        InsertBack("Data Source=SERVER-PC;Initial Catalog=Database;Persist Security Info=True;User ID=UsernameIfRequired;Password=PasswordIfRequired", "INSERT INTO TestSample (Column1,Column2,Column3) values (@Column1,@Column2,@Column3)", item.Text, item.SubItems(1).Text, item.SubItems(2).Text)

        'Add data to the database from ListView Items: Parameter requres connection string and the 3 input items (could be as many as you want)'
        InsertBack("Data Source=SERVER-PC;Initial Catalog=Database;Persist Security Info=True;User ID=UsernameIfRequired;Password=PasswordIfRequired", "INSERT INTO AnotherSample (Column1,Column2,Column3) values (@Column1,@Column2,@Column3)", item.Text, item.SubItems(1).Text, item.SubItems(2).Text)
    Next

      

As you can see, data can be sent to two different tables.

+2


a source


Why not just copy the first table to the second table and then bind the gridview to the second table instead of trying to bind the gridview to both?



0


a source







All Articles