Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi everyone

ive created a gridview and 2 textboxes and i want to create a button that when i click on it and the button creates a row in the gridview that with the text of the 2 texboxes anda a cell (collum) for each textbox.text

the problem is that, i can get the the first row with the text i want, but i cant get a second row. it just replaces the 1st

VB
Protected Function FormatDataTable() As DataTable
    Dim dt As DataTable = New DataTable()
    ' Create Columns
    dt.Columns.Add("id", System.Type.GetType("System.String"))
    dt.Columns.Add("text", System.Type.GetType("System.String"))
    Return dt
End Function

Protected Function GetData1() As DataTable
    Dim dt As DataTable = FormatDataTable()
    Dim dr As DataRow

    ' Populate the datatable with your data (put this in appropriate loop)
    dr = dt.NewRow
    dr("id") = TextBox1.Text
    dr("text") = TextBox2.Text
    ' Add the row
    dt.Rows.Add(dr)

    dt.NewRow()
    Return dt
End Function


Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    GridView1.DataSource = GetData1()
    GridView1.DataBind()
End Sub
Posted
Updated 6-Jun-13 6:26am
v4

1 solution

You need to cache that data table, so it remembers how many rows there are. Instead of building a new data table with 1 row each time, do something like
dt = Session(SessionVariableNameForTable)
. Then add your new row, and cache the result again with
Session(SessionVariableNameForTable) = dt
so the changes are available the next time through.
 
Share this answer
 
Comments
dp24 7-Jun-13 4:13am    
Thanks for using your time to help me, im kinda noob at asp.net. and it says i need do declare the variable but i dont know what type i should declare


Protected Function GetData1() As DataTable
Dim dt As DataTable = FormatDataTable()
Dim dr As DataRow
Dim gridsession as . . .
dt = Session(gridsession)
' Populate the datatable with your data (put this in appropriate loop)
dr = dt.NewRow()
dr("id") = TextBox1.Text
dr("text") = TextBox2.Text
' Add the row
dt.Rows.Add(dr)
dt.AcceptChanges()
Return dt
Session(gridsession) = dt
End Function

could you help me?
Btw, thanks for all!
woopsydoozy 7-Jun-13 9:41am    
What you put in the parantheses after Session is a session variable name, and that's a string. Best to make the string a constant, so there are no fat-finger typos, and you can use it anywhere else in you class that it's needed. Note also that I've moved the assignment to the session variable before the Return statement.

Const MY_DATA As String = "SomethingUniqueToIdentifyYourDataset"

Protected Function GetData1() As DataTable
Dim dt As DataTable = Session(MY_DATA)
If dt Is Nothing then dt = FormatDataTable()
Dim dr As DataRow
' Populate the datatable with your data (put this in appropriate loop)
dr = dt.NewRow()
dr("id") = TextBox1.Text
dr("text") = TextBox2.Text
' Add the row
dt.Rows.Add(dr)
dt.AcceptChanges()
Session(MY_DATA) = dt
Return dt
End Function
dp24 7-Jun-13 9:53am    
thanks alot! it worked :)

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