New row in datagrid: Old values are overwritten instead of a new row appears

First post from a not so experienced VS.NET/VB programmer Embarrassed

I am writing a short piece of code that allows users of the program to enter some data in textboxes and then by clicking on a button, allow them to add the information they put in the textboxes to a datagrid. The first time they press this button, it shows perfectly in the datagrid on the form, but the 2nd time it only updates the first row instead of adding a new row.

By doing some searches on Google, I found out that I am not using the right code, but I can't find how to do it correctly. I hope to get an answer here.

Below you see the code I am using currently, I wanted to attach an image to show the screen of the application, but obviously as a new user I am not allowed to do that.


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim table1
table1 = New DataTable("Payments")

rem adding the column to table
Dim EMail As DataColumn = New DataColumn("EMail")
EMail.DataType = System.Type.GetType("System.String")
table1.Columns.Add(EMail)
Dim Amount As DataColumn = New DataColumn("Amount")
Amount.DataType = System.Type.GetType("System.String")
table1.Columns.Add(Amount)
Dim Currency As DataColumn = New DataColumn("Currency")
Currency.DataType = System.Type.GetType("System.String")
table1.Columns.Add(Currency)
Dim Code As DataColumn = New DataColumn("Code")
Code.DataType = System.Type.GetType("System.String")
table1.Columns.Add(Code)
Dim Comment As DataColumn = New DataColumn("Comment")
Comment.DataType = System.Type.GetType("System.String")
table1.Columns.Add(Comment)

Dim Row As DataRow
Row = table1.NewRow()
REM declaring a new row
Row.Item("EMail") = tbEMail.Text
Row.Item("Amount") = tbAmount.Text
Row.Item("Currency") = cbCurrency.Text
Row.Item("Code") = tbCode.Text
Row.Item("Comment") = tbComment.Text
table1.Rows.Add(Row)

Dim ds As New DataSet
ds = New DataSet
REM creating a dataset
ds.Tables.Add(table1)
REM adding the table to dataset
DataGrid1.SetDataBinding(ds, "Payments")
REM binding the table to datagrid

Looking forward to hear from any of you!

Thank you in advance!

JD 

0
jandirks
10/13/2006 8:49:45 AM
asp.net.web-forms 93655 articles. 6 followers. Follow

4 Replies
754 Views

Similar Articles

[PageSpeed] 27

Hello jandirks, and welcome to the community!

It seems you are rebuilding the table from scratch on each invocation, so it will always hold a single row.

An approach to solve this is create the table and columns upon first load of the page (Page_Load, If Not IsPostBack) and put the table in Session. Then in handlers get the table back from session variable, and manage rows as needed.

A note: Session is a collection of Objects, so you have to cast back your reference to DataTable before using it:

   Dim Table1 As DataTable = CType(Session("Table1"), DataTable)

Btw, I'd strongly suggest you set Option Strict On and Option Explicit On in VB: http://support.microsoft.com/default.aspx?kbid=311329

Hope this helps. -LV


Julio P. Di Egidio
Software Analyst Programmer
=BUSINESS AND SCIENTIFIC=
=SOFTWARE DEVELOPMENT=
http://julio.diegidio.name

(Peace X Love] = [++1)
0
LudovicoVan
10/15/2006 3:07:55 PM

I posted my message in the VB.NET forums... Don't know why it was moved here...

 

LV, your answer is probably not of help for me... Page_load, etc... is not used in vb.net applications...

 

Thanks anyway...

 

Moderator, can this thread be moved back to the VB.NET forums ?

0
jandirks
10/16/2006 10:19:48 AM

Hello jandirks:

Yours is a question about making a form run, not about the language itself, so it has been moved to begin with.

This said, I am not sure I understand "not used in vb.net applications", because VB.NET is a language, not a platform. If yours is a win forms application, then you are completely in the wrong place, as these are the Asp.Net forums, devoted to web forms. For win forms support, check this MSDN forum group: http://forums.microsoft.com/MSDN/default.aspx?ForumGroupID=2&SiteID=1

Best luck. -LV


Julio P. Di Egidio
Software Analyst Programmer
=BUSINESS AND SCIENTIFIC=
=SOFTWARE DEVELOPMENT=
http://julio.diegidio.name

(Peace X Love] = [++1)
0
LudovicoVan
10/16/2006 4:48:42 PM
Sorry for my misposting! Thanks for forwarding me to the right forum!
0
jandirks
10/19/2006 11:01:15 AM
Reply:

Similar Artilces:

DataGrid: Insert a new row value in footer based on the selected row value
Hi guys         I need immediate help with the datagrid taht I am making. I have about 9 rows and with edit template item and a footer template for the new row that can be inserted in the db.   When someone hits the insert link in a row in the datagrid say its row 2 with OrgId = 10, I was the new row that comes as a footer row for the insert to populate orgId 10, leving the rest of the colums emtry for the user to fill in. But one column of this new row I want it to be populated with the value that was selected upon htting insert. I hope I didnt c...

How to add new rows in the form of rows
Hi, I can able to insert,delete and update record in gridview but cannot able to insert any new rows in the gird. In windows application we can insert new records(using DataGridView) into the database but not in web page. How to insert the records in gridview like control. Please tell me how to insert records in database in the form of rows. I mean i want to enter 20 records at one time.   Thanks.   Regards ArshadPlease Mark as Answer if the post is helpful to you. hi, Have you set the property AllowUserToAddRows=True; for the datagridview?????  If you are us...

Entering data in old aspx form only after saving data in new web form opened in new window
Dear all, i am facing a problem. please solve it. i have created two .aspx pages, say, page1.aspx and page2.aspx. with the help of client-side javascript i am opening page2.aspx from page1.aspx in a new window. So, page1.aspx and page2.aspx remains open at the same time. what i want is, i don't want the user to able to activate or enter data in page1.aspx without saving data in page2.aspx. regards subhajit use window.showModalDialog() instead of window.open() when the child window is closed, in this case, program execution continues on the next line after the showModalDialog...

select the row of the datagrid whose value is same as one of the value of row of datagrid
hellow all,  I have a datagrid which is bound to the ordertable,so all the orderdetails are shown on the page load. but my requirement is that,i have a textbox and a search button,when the user enter the order number in the text box and click on the search button the datagrid should show that record whose order number is entered by the user n most imp that the record may be in the 65th or 34th row of the datagrid,i wants to put it in the first row. how to do it. If there's a reason you can't filter down to just the one row, you'll have to come up with some paging algorithm that wil...

PB2.5: how to add a new row on DW and setfocus on specificated field of the new row.
I need to add a row on a datawindow when user press the 'tab' on the last field of the previous row. using dw_1.insertrow(0) in ItemChanged event does'nt work because the focus rest on the "old" line. Someone can tell me instructions to do this ? TIA roberto Roberto; The "tab" on the last field will fire the IC event if data has changed. If you are doing the InsertRow ( ) then, you are almost there in your problem. You may just need to tweak the UI as follows: DW Control IC event ... Long ll_row ll_row = InsertRow (0) ...

NEW NEW NEW
hi I have a huge form to build on an asp.net page with multiple fields  that come from and also connect to (postback=true) 10 different tables{database is access for now } .which controls would be helpful please suggest. Also how about usign infopath to build forms and hosting it on iis is it feasible..please advice. Thanks in advance Environment: Visual studio 2005 ,iis webserver,ms access,Thanks to all the PROS who are helping other developers.. Hi, For ASP.NET 2.0 Hosting purpose I am currently using GoDaddy.com. And satisfied with the service.RegardsKuldeep Deokule&nbs...

How to insert new row between the existing row in the gridview dynamically and how to insert value in it
hi,       I have a grid view i populate the grid with student records, i have a button called sub_Average when i click the button, the rows should reorder(groped) depend on department, and the average of each department should come under the end of each department (ie) a new row should be inserted for display Average of each department. Each new row should insert below the end of each department. Now my problem is i populate the records directly from dataset, so i dn't know how to insert a new row for average in the grid dynamically for each depar...

Adding a new row between rows
Hi to all. I'm trying to do a sort of SubReport. At this moment I have This Code made. It verifies if the column is equal it will erase it's text. What I would like to do now is if the Cell is not Equal then it will create a row with a label that says Sub Report. I tried some code but it inserts the row at the end. Can any one help me? protected void gridDefectReport_RowDataBound(object sender, GridViewRowEventArgs e)    {         if (e.Row.RowIndex > 0)        {     &nb...

Gridview
Hello, I currently have two Gridviews ... one with 2 Radiobuttons per row and the other that displays user selection from the RadioButtons ... Its pushing the data over using SqlDataSource2.InsertParameters but is taking all the selected values and putting into on row ... Example RadioButton1 Value RadioButton2 Value Etc... code below .... what I'm trying to do is have it return each RadioButton selected to an individual row... meaning RadioButton Selection is inserted to a row then the next RadioButon Selection starts a new row with a new ID...

add new row in datagrid, save all rows with just one trip to the server
I used a datagrid in my web app that can add a row dynamically. I added textboxes and radiobutton in the footer as the entry for new rows. Now, I want this new row to be added without posting back to the server then save them in the database with just one trip to the server. How will I do that? Hi, what do you exactly want? When I look at this sentence: this new row to be added without posting back to the server then save them in the database with just one trip to the server it becomes quite mystified to me. You can't add something to the database without a postback (or roundt...

how do you put new row at top of datagrid instead of bottom
Im adding a new row to a datagrid in edit mode: if the following puts new line at bottom of datagrid: datagrid.EditItemIndex = datagrid.Rows.Count - 1; What puts new line at top: When I try 0 it starts editing the existing data at row 0. thanks larry ...

Cannot get new blank row to appear at bottom of Datagrid!!!
Hi All,   I have created a button outside a datagrid that when pressed a blank row appears at the end of the datagrid for user to type in new data. However with the code I have, when I press the button the new row appears at the top! Please help, below I have highlighted the line of code in Red where the execution breaks down.     Private Sub btnAddRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddRow.Click Dim dr As DataRow = DataSet11.CustomerDetails.NewCustomerDetailsRow dr = DataSet11.CustomerDetails.NewCustomerDetailsRow Dim ...

How to Pass data to pop up form, allow user to add new value, pass new value back to form to do Insert
I have a grid view that shows the user the last meter readings (hours) for several generators. The meter readings need to be added daily.  I would like the user to be able to select the meter record from the grid view and have a pop up javascript form open that would allow the user to enter a new meter reading. Then after I do validation, do an insert of the new reading to the database. Is that a reasonable way of doing what I want? If not, can you give me a suggestion as to how to do it differently. Thanks. Hi..   Instead of opening window to ente...

Get column values of selected row in datagrid on a new page...
I have a datagrid, but its column has columns from multiple tables...(i.e. it has UNION in the sql statements and some columns are created by concatenating multiple columns together)One of the column has a hyperlink.  When the hyperlink of a particular row is clicked, a new form is loaded.  Is it possible to populate the fields on this new form with the column values of the selected row - i.e. without doing another SQL lookup? (i.e. basically reading all the column values of the selected row and then displaying it on a new form) Hi,You look like back to the question of passi...

Web resources about - New row in datagrid: Old values are overwritten instead of a new row appears - asp.net.web-forms

Missing Malaysia Airlines plane: onboard flight computer manually overwritten
WASHINGTON - The first turn to the west that diverted the missing Malaysia Airlines plane from its planned flight path from Kuala Lumpur to Beijing ...

‘Crimson Peak’ Reviews: Jessica Chastain Is a ‘Fabulous Monster’ in an ‘Overwritten’ Horror Film
... Creepy Haunted Mansion Thrills But some reviews note that while the film boasts stellar sets, it features “overfamiliar elements,” is “overwritten” ...

Missing Plane MH370 Computer Was 'Manually Overwritten' 17
The flight computer on missing Malaysian Airlines flight MH370 was "manually overwritten", investigators have said. The plane carrying 239 passengers ...

Garbage collection (computer science) - Wikipedia, the free encyclopedia
In computer science , garbage collection ( GC ) is a form of automatic memory management . The garbage collector , or just collector , attempts ...

Version Comparison: Apple Support Communities
Apple's iTunes software is, in the main, designed to transfer data to their devices but not in the other direction. In general if you try to ...

Inside AirPrint: How to add iOS printer sharing back to Mac OS X
Experimenters have discovered the configuration files Apple removed from the public release of Mac OS X 10.6.5; adding them back restores the ...

301 Redirecting WordPress Posts
I just upgraded my WordPress installation to 2.2.1 – partly because a friend warned me of a fairly dangerous exploit in the previous version. ...

Baselux
Your hard disk is damaged, and there are important files in the disk. What would you do? Of course, it’s best to send your hard disk drive to ...

10. What is system bucket and what is its use in Amazon S3 operations?
System bucket is provided to backup all deleted file(s) and older version of overwritten file(s) on Amazon S3 in same structure.

The Dreaded Kernel Panic on Mac OS X
Apple's Mac OS X operating system is a flavor of Linux. So us Mac users have the privilege of rarely experiencing what are called "kernel panics." ...

Resources last updated: 11/23/2015 12:04:48 AM