Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 19th, 2007, 12:10 PM
Authorized User
 
Join Date: Jul 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to fill a dataset from a gridview

Sorry if this topic has been posted before.

How do I populate a dataset or datatable with the contents of a gridview control?

Basically, I want to add a new row to the gridview without destroying the exisitng contents of the gridview.

To preserve the data between callbacks, I want to get the existing gridview data and add one row to it and then rebind the gridview to reflect the latest addition.

Any ideas?

 
Old July 19th, 2007, 01:13 PM
Friend of Wrox
 
Join Date: Jun 2007
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
Default

how are you changing the datagrid?

I'm a junior coder myself, but datagrid is just a display device. It has to get it's information from your datareader or dataset. So by the time you see your information populated in the datagrid, everything is finished. All you have to do is update your dataset back to the database so that the changes are saved.

-------------------------

Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe

When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper

Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.
 
Old July 19th, 2007, 01:40 PM
Authorized User
 
Join Date: Jul 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am not using a datagrid I am using two gridviews. The first gridview is populated by a sqldatasource object.

When the user clicks a button, the RowCommand event fires, and I use code to add a record to the 2nd gridview.

This technique works well for the latest record.

The problem is I need to preserve the second gridview's contents and add another row to it programmatically.

So I want to create a datatable and load it with the 2nd gridview's data, then add the latest record to it and rebind the 2nd gridview with the datatable.



 
Old July 19th, 2007, 03:16 PM
Authorized User
 
Join Date: Jul 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I found a way. I get a collection of gridviewrows and then loop through them to add them to the new datatable. Then, add the new row, and bind to the gridview. My RowCommand sub for this is below (without the Try/Catch blocks).

I hope this helps someone else:
Protected Sub gridSubs_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs ) Handles gridSubs.RowCommand

If e.CommandName = "AddToCart" Then
            'first get the row that was clicked
            Dim i As Integer = Convert.ToInt32(e.CommandArgument)
            Dim dr As GridViewRow = gridSubs.Rows(i) 'gridsubs is first gridview that holds subscriptions
            Dim origrow As GridViewRow
            Dim dt As DataTable = New System.Data.DataTable("OrderTable")
            Dim ds As New DataSet
            Dim column As DataColumn
            Dim row As DataRow
            'next get the orders table so that we can add the new row to it
            Dim gvr(gridOrders.Rows.Count - 1) As GridViewRow 'gridOrders is 2nd gridview that holds new additions selected from gridSubs
            gridOrders.Rows.CopyTo(gvr, 0)

            'now add the columns to the data table
            column = New DataColumn
            column.DataType = System.Type.GetType("System.String")
            column.ColumnName = "subid"
            column.ReadOnly = True
            column.Unique = True
            dt.Columns.Add(column)

            column = New DataColumn
            column.DataType = System.Type.GetType("System.String")
            column.ColumnName = "subname"
            column.Unique = True
            column.ReadOnly = True
            dt.Columns.Add(column)

            column = New DataColumn
            column.DataType = System.Type.GetType("System.String")
            column.ColumnName = "subprice"
            column.Unique = True
            column.ReadOnly = True
            dt.Columns.Add(column)

            'set the primary key
            Dim PrimaryKeyColumns(0) As DataColumn
            PrimaryKeyColumns(0) = dt.Columns("subid")
            dt.PrimaryKey = PrimaryKeyColumns

            'now add the datatable to the dataset
            ds.Tables.Add(dt)

            'now add the previous orders back to the datatable
            For Each origrow In gvr
                row = dt.NewRow
                row("subid") = origrow.Cells(0).Text
                row("subname") = origrow.Cells(1).Text
                row("subprice") = origrow.Cells(2).Text
                dt.Rows.Add(row)
            Next

            'now add the new row
            row = dt.NewRow
            row("subid") = dr.Cells(0).Text
            row("subname") = dr.Cells(1).Text
            row("subprice") = dr.Cells(3).Text
            dt.Rows.Add(row)

            'finally, bind the updated datatable to the gridview
            gridOrders.DataSource = ds
            gridOrders.DataBind()
        End If
end sub






Similar Threads
Thread Thread Starter Forum Replies Last Post
Fill gridview from 2 tables using sqldatasource niktos ASP.NET 2.0 Basics 1 January 30th, 2007 10:45 AM
Fill(dataset) or dataset.load() salemkoten SQL Server 2005 1 November 2nd, 2006 11:04 PM
fill dataset with oracle sp p2pMember ASP.NET 1.0 and 1.1 Professional 1 October 4th, 2006 04:19 AM
How to fill stored procedure in dataset. jayaraj ADO.NET 4 August 2nd, 2004 02:40 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.