Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-02-20
6
Medium Priority
?
6,798 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 24

Assisted Solution

by:mankowitz
mankowitz earned 1000 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 1000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

705 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