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 November 29th, 2007, 02:15 PM
Authorized User
 
Join Date: Jun 2006
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem with Adding a new row to an existing datag

Hello,

The existing data table binded to a grid works When I enter the number of line items I need and click on a button that say make grid. The problem is when click on Add Rows the extra row does not appear. I think I am having a problem with the databind, because if I use, gvParts.DataBind(), the existing datagrid goes away.


Thank you in advance,

Existing Code used to create the data table to add records (WORKS)
************************************************** ********
    Protected Sub btnMkGrid_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnMkGrid.Click
        Dim dt As New Data.DataTable
        'Dim tblReceiving As Table
        dt.Columns.Add(New Data.DataColumn("LineNO"))
        For i As Integer = 1 To tbItems.Text
            Dim dr As Data.DataRow
            dr = dt.NewRow
            dr("LineNO") = i
            dt.Rows.Add(dr)
        Next
        'Bind the DataTable to the DataGrid
        gvParts.DataSource = dt
        gvParts.DataBind()

    End Sub

************************************************** **
Does Not Work
Protected Sub AddRow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddRow.Click

        'Add the number of rows specified by "RowCount" to gvParts
        If (IsNumeric(RowCount.Text)) Then
            Dim i As Integer
            For i = 1 To CInt(RowCount.Text)

                'Adding a blank record to the dataset.
                Dim dt As New Data.DataTable
                dt.Columns.Add(New Data.DataColumn("LineNO"))
                Dim dr As Data.DataRow
                dr = dt.NewRow
                dr("LineNO") = i
                dt.Rows.Add(dr)

            Next
        End If
        'Rebind the grid to show the newly added row(s).
        'gvParts.DataBind()
        'DataBind()

    End Sub


 
Old November 29th, 2007, 02:36 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Your logic is all messed up. First, if your for loop executes more than once, the data from precceding loop is lost since your recreate the DataTable with every pass.

Second, you never specifiy a datasource for your grid. Your code should be something like:
Code:
        Dim dt As New Data.DataTable
        dt.Columns.Add(New Data.DataColumn("LineNO"))

        If (IsNumeric(RowCount.Text)) Then
            Dim i As Integer
            For i = 1 To CInt(RowCount.Text)
                Dim dr As Data.DataRow
                dr = dt.NewRow
                dr("LineNO") = i
                dt.Rows.Add(dr)
            Next
        End If
        'Rebind the grid to show the newly added row(s).
        gvParts.DataSource = dt
        gvParts.DataBind()
Issues: if the for never executes (or the IF statement for that matter), an empty DataTable will be bound to your Grid and erase all of your data. You could of course move the DataTable declaration into the IF as well as the code to specifiy the DataSource and DataBind.

Your code claims to add the table to a dataset, but I do not see a Dataset anywhere.



================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor :.
Wrox Books 24 x 7
================================================== =========
 
Old November 29th, 2007, 04:00 PM
Authorized User
 
Join Date: Jun 2006
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am working on the logic, because when I click on Add Row it recreates a new grid instead of add extra rows to the bottom of the grid.

 
Old December 1st, 2007, 10:31 AM
Authorized User
 
Join Date: Jun 2006
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello, I updated my logic, but it still seems like I am just recreating a new table. Please tell me where you think I am going wrong this time around. Thank you in advbance:

Protected Sub AddRow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddRow.Click

        Dim dt As New partdb.tblReceivingDataTable()
        dt.Columns.Add(New Data.DataColumn("LineNO"))

        If (IsNumeric(RowCount.Text)) Then
            Dim i As Integer
            For i = 1 To CInt(RowCount.Text)

                'Dim ds As DataSet = New DataSet(): Donot need using existing dataset partdb.tblReceivingDataTable
                'Adding a row to the dataset datatable assigned to the grid.datasource
                Dim dr As Data.DataRow
                dr = dt.NewRow()
                dr("CustomerID") = ""
                dr("BA") = "False"
                dr("Barcode") = ""
                dr("RP") = "False"
                dr("RD") = ""
                dr("CL") = "False"
                dr("LocationID") = ""
                dr("Part_SerialNumber") = ""
                dr("Part_number") = ""
                dr("UPC") = ""
                dr("unit_price") = "0"
                dr("PropertyTypeID") = ""
                dr("CC") = ""
                dr("RC") = ""
                dr("Received_Qty") = "0"
                dr("UIID") = ""
                dr("CCID") = ""
                dr("Time_stamp_Received") = Date.Now
                dr("Remarks") = ""
                dr("PFlag") = "False"
                dr("Archive") = "False"
                'dt.Rows.Add("", "", "", "", "", "", "", "", "", "", 0, "", "", "", 0, "", "", "", "")
                dt.Rows.Add(dr)
                dt.AcceptChanges()

                'Bind & Show New row(s) in grid
                gvParts.DataSource = dt
                gvParts.DataBind()
            Next
        End If


    End Sub


 
Old December 4th, 2007, 09:41 AM
Authorized User
 
Join Date: Jun 2006
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I added dt = (DataTable)gvParts.DataSource and received error: DataTable is a type and cannot be used as an expression. and end of statement expected. I have tried the statement different ways and no luck.


 
Old December 6th, 2007, 02:37 AM
Authorized User
 
Join Date: Jun 2006
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Helllo,

I updated the my logic and added page load code to bind to a blank data table with all the columns I need to add a blank row when using the Add Row Click but now I receive the error: Column 'CustomerID' does not belong to table. Please tell me what I am doing wrong?

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim DT As New DataTable
        If (Not Page.IsPostBack) OrElse (Not (TypeOf Session("DT") Is DataTable)) Then

            'Create Empty Datatable
            'DT = CType(gvParts.DataSource, DataTable)
            DT = New DataTable("AddExtraRow")
            DT.Columns.Add("LN", Type.GetType("System.String"))
            DT.Columns.Add("CustomerID", Type.GetType("System.String"))
            DT.Columns.Add("BarcodeAttached", Type.GetType("System.String"))
            DT.Columns.Add("Barcode", Type.GetType("System.String"))
            DT.Columns.Add("RevisionPart", Type.GetType("System.String"))
            DT.Columns.Add("RevisionDescription", Type.GetType("System.String"))
            DT.Columns.Add("ComponentLoaction", Type.GetType("System.String"))
            DT.Columns.Add("LocationID", Type.GetType("System.String"))
            DT.Columns.Add("Part_SerialNumber", Type.GetType("System.String"))
            DT.Columns.Add("Part_number", Type.GetType("System.String"))
            DT.Columns.Add("UPC", Type.GetType("System.String"))
            DT.Columns.Add("unit_price", Type.GetType("System.String"))
            DT.Columns.Add("PropertyTypeID", Type.GetType("System.String"))
            DT.Columns.Add("ComponentClassification", Type.GetType("System.String"))
            DT.Columns.Add("RecvCondition", Type.GetType("System.String"))
            DT.Columns.Add("Received_Qty", Type.GetType("System.String"))
            DT.Columns.Add("UOI", Type.GetType("System.String"))
            DT.Columns.Add("ConditionCodeID", Type.GetType("System.String"))
            DT.Columns.Add("Time_stamp_Received", Type.GetType("System.String"))
            DT.Columns.Add("Remarks", Type.GetType("System.String"))

            'Store the table in the Session variable
            Session("DT") = DT

            ' Bind the DataGrid to an empty table
            'gvParts.DataSource = DT
           ' gvParts.DataBind()

        Else
            ' Retrieve the table from the session variable
            DT = CType(Session("DT"), DataTable)
        End If

    End Sub

    Protected Sub AddRow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddRow.Click

        If (IsNumeric(RowCount.Text)) Then
            Dim i As Integer
            For i = 1 To CInt(RowCount.Text)
                Dim DT As New DataTable
                Dim DR As DataRow = DT.NewRow()
                DR("CustomerID") = RowCount.Text
                DT.Rows.Add(DR)

                'Bind & Show New row(s) in grid
                gvParts.DataSource = DT
                gvParts.DataBind()
            Next
        End If

    End Sub






Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamically add a new row to a existing GridView srkvellanki ASP.NET 2.0 Professional 1 September 21st, 2008 09:15 PM
Adding existing Console/project ? chiefarchon BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4 0 September 19th, 2008 07:51 AM
Adding existing and non-existing attributes spencer.clark XSLT 5 July 27th, 2005 04:02 PM
Please Help Print a column from a row from a datag macupryk Javascript 1 October 31st, 2004 06:52 PM
insert new row and merge with existing merged cell funkyProgrammer Excel VBA 1 February 9th, 2004 10:58 AM





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