Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have used the following code to display data from Text Boxes and combo boxes to a Grid View without saving the data to the database. When I enter the data in the Text Boxes, and click on the Button, those values will be displayed in the Grid view. It is working as expected without any errors.
But I want to tweak the code a bit so that the Grid View should be able to add new data from the Text boxes by retaining the old data as it is in the Grid view (multiple rows should be displayed in the Grid view instead of single rows).

It is an desktop application, I'm using visual basics and visual studio 2012. My code so far:

VB
Public Sub DataTabel_TblDetali()

        Dim TblDetali As New DataTable()

        'Se kreira dataTabel so oderdeniKoloni
        TblDetali.Columns.Add("BrojBaranje", GetType(String))
        TblDetali.Columns.Add("RedBroj", GetType(Integer))
        TblDetali.Columns.Add("Apoen", GetType(Decimal))
        TblDetali.Columns.Add("Evidenciski_Broj", GetType(String))
        TblDetali.Columns.Add("InventarenBroj", GetType(String))
        TblDetali.Columns.Add("OznakaSerija", GetType(String))
        TblDetali.Columns.Add("VidSeriskiBroj", GetType(String))
        TblDetali.Columns.Add("GodinaIzdavanje", GetType(String))
        TblDetali.Columns.Add("BrojParcinja", GetType(Integer))
        TblDetali.Columns.Add("SeriskiBroj1", GetType(String))
        TblDetali.Columns.Add("SeriskiBroj2", GetType(String))
        TblDetali.Columns.Add("BrojIndikativ", GetType(String))
        TblDetali.Columns.Add("TipKlasa", GetType(String))
        TblDetali.Columns.Add("TipReprodukcija", GetType(String))
        TblDetali.Columns.Add("BrojNalog", GetType(String))



        Dim RedenBroj As Integer = 0
        TblDetali.Rows.Add(BrojBaranje, RedenBroj + 1, Apoen, EvidenciskiBroj, InventarenBroj, OznakaZaSerija, VidSeriskibroj, GodinaIzdavanje, BrojParcinja, SeriskiBroj1, SeriskiBroj2, BrojIndikativ, TipKlasa, TipReprodukcija, "")

        grdDetaliVnos.DataSource = TblDetali
        RedenBroj = RedenBroj + 1
Posted
Updated 3-Oct-13 2:39am
v2

1 solution

Hey there,

You could save the DataTable in the Session after adding row to it.
This way when you want to add another row, just get the DataTable back from Session, add a row to the table, bind it to the GridView and save it back to the same Session

Here:
Use this statement:
VB
If Session("MyTable") IsNot Nothing Then
TblDetali = ctype(Session("MyTable"), DataTable)  End If

after:
VB
Dim TblDetali As New DataTable()

and
this line:
VB
Session("MyTable") = TblDetali 
before this line:
VB
grdDetaliVnos.DataSource = TblDetali



Hope it helps.

Azee
 
Share this answer
 
v4
Comments
Voley 3-Oct-13 9:18am    
When I do that I get this error: 'Session' is not declared. It may be inaccessible due to its protection level.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900