Solved

DataGridView data binding (ADO.NET 2.0)

Posted on 2006-06-12
5
2,410 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 500 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

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

13 Experts available now in Live!

Get 1:1 Help Now