Go Premium for a chance to win a PS4. Enter to Win

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,923 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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 …
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

916 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