Link to home
Start Free TrialLog in
Avatar of adminender
adminender

asked on

Asp .net gridview adding new row

Hi, there is 2 textboxes and a button in my page. and i want to add the values of textboxes to a grdview by programmatically. how can i do?
Avatar of sameer_goyal
sameer_goyal
Flag of India image

What language are you using? vb or c#
Avatar of adminender
adminender

ASKER

vb
You can do the following if you want to bind the grid only with the 2 textboxes

On Button Click:

Dim dsData As DataTable = New DataTable("GridData")
dsData.Columns.Add("Column1", typeOf(string))
dsData.Columns.Add("Column2", typeof(string))

Dim drRow As DataRow = dsData.NewRow()
drRow[0] = txtBox1.Text
drRow[1] = txtBox2.Text

dsData.Rows.Add(drRow)

GridView1.DataSource = dsData
GridView1.DataBind()

Hope it helps. Let me know if you need any other help on this one.
Just one edit:

In place of [ ] brackets, use ( ).
thanks sameer goyal i try it. it works. but i create the datatables columns on the load event of the page. i check the postback event but every click of add button it removes the previous entries and takes the last entry. how i can solve this?
here is the code:
LOAD PART

        If (Not IsPostBack) Then

            newtable.Columns.Add("Marka", System.Type.GetType("System.String"))

            grd_urun_listesi.DataSource = newtable
            grd_urun_listesi.DataBind()

        End If


ADD BUTTON CLICK PART

        Dim newrow As Data.DataRow
        newrow = newtable.NewRow
        newrow.Item(0) = txt_marka.Text
        newtable.Rows.Add(newrow)
        grd_urun_listesi.DataBind()



when click add button it couldn't find column
ASKER CERTIFIED SOLUTION
Avatar of sameer_goyal
sameer_goyal
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks a lot.