简体   繁体   中英

ASP.NET with VB.NET adding rows to data table don't remain after postback

I am using ASP.NET with VB.NET and I want the user add several row to data table.

I am trying to add multi row into data table in run time by (textboxes, rad combo box and check box). I add first row second and third row. But when I present the data table its show only the last row I enter so the table contain only one row.

How do I keep all row in data table not only one row? I tried everything please help.

This is my code:

Dim dcItemID = New DataColumn("ItemID", GetType(Int32))
Dim dcUnit = New DataColumn("UnitID", GetType(Int32))
Dim ItemPrice = New DataColumn("ItemPrice", GetType(Int32))
Dim sellprice = New DataColumn("SellPrice", GetType(Int32))

dt.Columns.Add(dcItemID)
dt.Columns.Add(dcUnit)
dt.Columns.Add(ItemPrice)
dt.Columns.Add(sellprice)

dt.Rows.Add(Convert.ToInt32(rcItemName.SelectedValue), Convert.ToInt32(rcUnitID.SelectedValue), tbItemPrice.Text, tbSellprice.Text)

thank u all i found the answer

if any one interested this is my solution :

Public Class WebForm1 Inherits System.Web.UI.Page

Private dt As DataTable = New DataTable()

Private dr As DataRow = Nothing
Protected Sub btnSaved_Click(sender As Object, e As EventArgs) Handles btnSaved.Click


    If ViewState("table") IsNot Nothing Then
        dt = CType(ViewState("table"), DataTable)
    End If
    dr = dt.NewRow()
    dr("ItemPrice") = tbItemPrice.Text
    dr("SellPrice") = tbSellprice.Text
    ' dr("Product_Amount") = txtAmount.Text
    dt.Rows.Add(dr)
    ViewState("table") = dt
    GridView1.DataSource = dt
    GridView1.DataBind()
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    dt.Columns.Add("ItemPrice")
    dt.Columns.Add("SellPrice")


End Sub

End Class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM