Solved

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

Posted on 2014-02-20
6
5,665 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
Comment Utility
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
Comment Utility
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
Comment Utility
Let me know if you need help with the connectionstring.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:printmedia
Comment Utility
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
Comment Utility
Super :)
0
 

Author Closing Comment

by:printmedia
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now