没有数据库的asp.net gridview

时间:2013-05-09 07:39:48

标签: asp.net gridview postback

大家好我有一个问题。有没有办法在不使用数据库的情况下在asp.net中填充gridview?我正在使用gridview来显示信息,但我已经看到每当我尝试向网格中插入更多行时,最后一行会更改为新的并覆盖数据

我正在使用以下代码:

Dim dtsetinform As New DataSet
Dim datatableinfo As New DataTable("fill")

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

   dtsetinform.Tables.Add(datatableinfo ) 
   gridfactura.DataSource = datatableinfo 
   datatableinfo.Columns.Add("Name") '
   datatableinfo.Columns.Add("Quantity")
   datatableinfo.Columns.Add("Price")

   Session("fill") = datatableinfo 

End Sub

Public Sub agregarfilas(ByVal total As Integer)

   datatableinfo = Session("fill")

   Dim row As DataRow = datatableinfo .NewRow
   row("Name") = ddserviciotxt.Text 
   row("Quantity") = cantidadtxt.Text
   row("Price") = total
   datatableinfo .Rows.Add(row)

   ViewState("tablainViewState") = datatableinfo 
   datatableinfo .AcceptChanges()
   gridfactura.DataSource = datatableinfo 

   gridfactura.DataBind()

End Sub

Protected Sub btnagregar_Click(sender As Object, e As EventArgs) Handles btnagregar.Click
Dim result As Integer
Dim price As Integer = preciotxt.Text

Dim quantity As Integer = cantidadtxt.Text

result = price * quantity 
agregarfilas(result)

End Sub

1 个答案:

答案 0 :(得分:1)

您需要将代码放入页面加载

if not Page.IsPostBack then
your code
end if