Solved

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

Posted on 2014-02-20
6
6,355 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 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

752 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