[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?
Re: Add a new row in a GridView
Is this the GridView as in ASP.NET or the DataGridView as in Windows Forms?
Re: Add a new row in a GridView
Quote:
Originally Posted by
mendhak
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).
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.