Results 1 to 4 of 4

Thread: [RESOLVED] Add a new row in a GridView

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2007
    Posts
    8

    Resolved [RESOLVED] Add a new row in a GridView

    EDIT: forgot to mention, so better state this before anything else... I'm running VS 2005 with Framework 2.0.

    Hello all,

    I have a GridView (gdvN) with 7 columns (N1, N2, N3, N4, Edit command e um Delete command). In the same aspx, 4 textbox (that would be a new line with new N1, N2, N3 and N4 values.).

    After filling the textboxes and pressing a button, it was supposed to insert a Row in the gdvN.

    The code I wrote is below:

    Code:
            Dim tbl As Table
            Dim newindex as Int32 = 0
            table = DirectCast(gdvN.Controls(0), Table)
    
            For Each _item In gdvN.Rows
                     newindex += 1
            Next
    
            Dim _newitem As New TableRow
    
            _newitem = New GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Insert)
    
            _newitem.Cells(1).Text = txtN1.text
            _newitem.Cells(2).Text = txtN2.text
            _newitem.Cells(3).Text = txtN3.text
            _newitem.Cells(4).Text = txtN4.text
    
            tbl.Rows.AddAt(newindex, _newitem)
    When i try to run this, I notice that _newitem is created but has Cells.Count = 0. I thought it was a matter of "copying" a similar row, so I added:

    Code:
           _newitem = gdvN.Rows(0)
    Unfortunately that didn't solved, as it edits an existing row instead of adding one. As I can see, that may be two paths for this to be solved:

    Is there a way to make _newitem be created with the same structure as the gdvN rows. I didn't found how.

    Or there's another logic (or a mistake in the coding) I'm not looking?
    Last edited by garavello; Apr 7th, 2009 at 03:59 PM. Reason: Adding Vs and Framework versions

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Add a new row in a GridView

    Is this the GridView as in ASP.NET or the DataGridView as in Windows Forms?

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2007
    Posts
    8

    Re: Add a new row in a GridView

    Quote Originally Posted by mendhak View Post
    Is this the GridView as in ASP.NET or the DataGridView as in Windows Forms?
    ASP.NET

    Just more info: I know the easiest way would be changing the DataSource (if it was SQL or Orcale, adding a new record to a table and then binding the gdvN again would work), but the data comes from a SAP plataform, so the setting here is to bind the gdvN with info from SP, let people add rows to the gdv and only after work is done to update the SAP tables.

    That's why i tried creating a "tbl" variable to get the info from the gdvN. what I would need is to add a new row to this "tbl", and then bind the gdvN with it. But I am facing problems with the _newitem row, as it isn't created with the proper cells (when i debug, after creation _newitem has a 0 count of cells).

  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 2007
    Posts
    8

    Re: Add a new row in a GridView

    Found a way. I created a new table, same as the original datasource. I read the GridView, adding the rows to the new datasource, then I add a new line (with the textboxes' info) to datasource and then binding the gridview with the new datasource.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width