Solved

DataGridView data binding (ADO.NET 2.0)

Posted on 2006-06-12
5
2,413 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
[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
  • 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

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!

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
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…

733 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