[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

DataGridView data binding (ADO.NET 2.0)

Posted on 2006-06-12
5
Medium Priority
?
2,423 Views
Last Modified: 2008-02-01
I would like to use the results of a stored procedure to fill a datagridview. I am not using the Data Set IDE and created the Stored Procedure objects manually. I added a blank datagridview control to the form named dgPartInfo but don't know where to go from here:

        Dim cmdGetPartInfo As New SqlCommand("SW_GET_PART_BASICS", dbConn) ' gets header info from part table
        cmdGetPartInfo.CommandType = CommandType.StoredProcedure
        cmdGetPartInfo.Parameters.AddWithValue("@PART_ID", strPartID)
        Dim PartReader As SqlDataReader = cmdGetPartInfo.ExecuteReader
        dgPartInfo.DataSource = PartReader

I am moving from mostly VB6 experience to .NET 2.0 so am not very familiar with the datagrid and datagrid view controls or the difference between them. All the documantation I can find only shows how to poulate a grid by dragging items from the Data Set IDE.

Thanks,
0
Comment
Question by:SEANWYATT
  • 2
  • 2
5 Comments
 
LVL 10

Accepted Solution

by:
vadim63 earned 1500 total points
ID: 16889283
A good example:

' Assumes that connection is a valid SqlConnection object.
Dim adapter As SqlDataAdapter = New SqlDataAdapter( _
  "SELECT CategoryID, CategoryName FROM dbo.Categories", connection)

adapter.InsertCommand = New SqlCommand("InsertCategory", connection)
adapter.InsertCommand.CommandType = CommandType.StoredProcedure

adapter.InsertCommand.Parameters.Add( _
  "@CategoryName", SqlDbType.NChar, 15, "CategoryName")

Dim parameter As SqlParameter = adapter.InsertCommand.Parameters.Add( _
  "@Identity", SqlDbType.Int, 0, "CategoryID")
parameter.Direction = ParameterDirection.Output

connection.Open()

Dim categories As DataSet = New DataSet
adapter.Fill(categories, "Categories")

Dim newRow As DataRow = categories.Tables("Categories").NewRow()
newRow("CategoryName") = "New Category"
categories.Tables("Categories").Rows.Add(newRow)

adapter.Update(categories, "Categories")

connection.Close()
0
 

Author Comment

by:SEANWYATT
ID: 16889352
OK but how does this data get into the datagridview control?
0
 
LVL 10

Expert Comment

by:vadim63
ID: 16889428
DataGridView1.DataSource = categories.Tables("Categories")
0
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 16889441
the datasource for your datagridview needs to be a datatable/dataview not a datareader.

stealing from vadim's example:

dgpartinfo.datasource = categories.tables("categories")
0
 

Author Comment

by:SEANWYATT
ID: 16889531
OK Thanks. I was trying to do it without the table defiined but now it makes sense.

I appreciate the quick response.
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

834 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