Solved

How to dynamically populate a datagridview control in vb.net in visual studio 2010

Posted on 2014-02-20
6
6,101 Views
Last Modified: 2014-02-20
Hi all.

I want to populate a datagridview control based on the value entered in one of my windows form's textbox. So I created a Leave event for the textbox:

    Private Sub txtMasterItemNo_Leave(sender As System.Object, e As System.EventArgs) Handles txtMasterItemNo.Leave
        DataGridView2.DataSource = GetDataTable()

    End Sub

Open in new window


It calls the GetDataTable() function which is where I'm going to grab the data to populate the datagrid.

Private Function GetDataTable() As DataTable
        '
        ' This Function needs to build the data table.
        '
        Dim con As New SqlConnection
        Dim cmd As New SqlCommand
       

        con.ConnectionString = "Data Source=myserver;Initial Catalog=mydb;Integrated Security=True"

        con.Open()

        cmd.Connection = con

        'cmd.CommandText = "INSERT INTO ItemQuote( MasterItemNumber, Description, SLXID, QuoteDate, UnitCost, QuotedCost, Notes) VALUES ('" & txtMasterItemNo.Text & "', '" & txtDescription.Text & "', '" & txtSalesLogixID.Text & "', '" & txtQuoteDate.Text & "', " & txtUnitCost.Text & ", " & txtQuotedCost.Text & ", '" & NotevalueToPutInDatabase & "')"
        cmd.CommandText = "SELECT  MasterItemNumber, Description, SLXID, QuoteDate, UnitCost, QuotedCost, Notes FROM ItemQuote WHERE MasterItemNumber = @MasterItemNo"

        cmd.Parameters.AddWithValue("@MasterItemNo", txtMasterItemNo.Text)

        cmd.ExecuteNonQuery()

        Return New DataTable()

        con.Close()
    End Function

Open in new window


But now I'm stuck, how do I create the column headers and bring in the data?

I simply dragged and dropped the DataGridView control onto my windows form and did not select a data source because the data would be based on what is entered in the textbox.

Thank you in advance!
0
Comment
Question by:printmedia
  • 3
  • 2
6 Comments
 
LVL 24

Assisted Solution

by:mankowitz
mankowitz earned 250 total points
ID: 39873787
This is fairly complicated, but I can give you some brush strokes.

First, you create a DataSet and add DataAdapters with your SQL commands. Next you use the dataadapter's fill command to populate the dataset. When you bind the datagridview to the dataset's datatable, it will automagically work.

Here are some examples:
http://www.daniweb.com/software-development/vbnet/threads/277433/tutorial-request-dataadapter-commands-to-update-your-database-from-a-datagridview
0
 
LVL 15

Accepted Solution

by:
David L. Hansen earned 250 total points
ID: 39873859
Personally, I avoid full datasets in my code.  I create a single datatable and fill it with an SQL statement, then attach it to the datagridview.
Dim dtTable As DataTable 
Dim sqlConn As New SqlConneciton 
sqlConn.ConnectionString = "ConnectionStringGoesHere;" 

Dim sqlCmd As New SqlCommand 
sqlCmd.CommandText = "SELECT * FROM yourTables..etc." 
sqlCmd.CommandType = CommandType.Text 
sqlCmd.Connection = sqlConn 

Dim sqlAdp As New SqlDataAdapter 
sqlAdp.SelectCommand = sqlCmd 

'Now fill that datatable:
dtTable = New DataTable
sqlAdp.Fill(dtTable) 

'Attach it to the grid:
myDataGridView.DataSource = dtTable 
myDataGridView.DataBind

Open in new window

Nothing wrong at all with using DataSets.  I just reserve using them for when I have lots of tables to manage.  It keeps simple things simple.
0
 
LVL 15

Expert Comment

by:David L. Hansen
ID: 39873867
Let me know if you need help with the connectionstring.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:printmedia
ID: 39873883
Thank you both!

I used both your suggestions and adjusted mine a bit, and it works.
0
 
LVL 15

Expert Comment

by:David L. Hansen
ID: 39873890
Super :)
0
 

Author Closing Comment

by:printmedia
ID: 39873891
I'll post another question shortly because now I want to join my table with another table that resides on a different server and database. Thanks again!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Convert datetime to time string 10 28
VB.NET 2008 Winforms Signing 13 31
How to seperate number and letters from a string 3 22
Groupbox Control ? 2 17
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question