将DataRow逐个添加到DataTable

时间:2016-06-04 17:25:16

标签: vb.net datatable telerik datarow

我有一个用于将发票信息输入数据库的网络表单。

我有3个文本框,要求提供产品数量,产品和价格。

我决定创建一个DataTable来临时存储数量,productId,价格,以便用户可以添加N个产品,以便稍后将DataTable上的数据(显示在网格上)保存到数据库。

我不知道我做错了什么但是无法存储第二个产品。将1行保存到DataTable后,第二行未添加,但会覆盖第一行。我知道这对我来说是非常愚蠢的事情,但未能弄清楚我做错了什么,而且还是一个初学者。

Public Class IngresoFacturas
    Inherits System.Web.UI.Page
    Dim miDataTable = New DataTable

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        miDataTable.Columns.Add("dCantidad", GetType(System.Int32))
        miDataTable.Columns.Add("idProducto", GetType(System.Int32))
        miDataTable.Columns.Add("mTotal", GetType(System.Decimal))

    End Sub

    Protected Sub RadButton1_Click(sender As Object, e As EventArgs) Handles RadButton1.Click

        miDataTable.Rows.Add(RadNumericTextBox2.Text, Int32.Parse(RadAutoCompleteBox2.Entries.Item(0).Value), RadNumericTextBox3.Text)
        RadGrid1.Rebind() 'Forces rebind to update Grid
        RadAutoCompleteBox2.Entries.Clear()
        RadNumericTextBox2.Text = ""
        RadNumericTextBox3.Text = ""

    End Sub

    Protected Sub RadGrid1_NeedDataSource(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource

        RadGrid1.DataSource = miDataTable

    End Sub
End Class

2 个答案:

答案 0 :(得分:0)

试过安德鲁的解决方案,但它没有工作所以我做到了这一点:

你们有什么事情可以警告我使用ViewState来解决这个问题。它正在运作....但不知道它是否是最好的方法

    Public Class IngresoFacturas
Inherits System.Web.UI.Page
Dim miDataTable As DataTable

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        miDataTable = New DataTable
        miDataTable.Columns.Add("dCantidad", GetType(System.Int32))
        miDataTable.Columns.Add("idProducto", GetType(System.Int32))
        miDataTable.Columns.Add("mTotal", GetType(System.Decimal))
        ViewState("tabla") = miDataTable
    End If

End Sub

Protected Sub RadButton1_Click(sender As Object, e As EventArgs) Handles RadButton1.Click
    miDataTable = ViewState("tabla")
    miDataTable.Rows.Add(RadNumericTextBox2.Text, Int32.Parse(RadAutoCompleteBox2.Entries.Item(0).Value), RadNumericTextBox3.Text)
    RadGrid1.Rebind() 'Forces rebind to update Grid
    ViewState("tabla") = miDataTable
    RadAutoCompleteBox2.Entries.Clear()
    RadNumericTextBox2.Text = ""
    RadNumericTextBox3.Text = ""

End Sub

Protected Sub RadGrid1_NeedDataSource(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource

    RadGrid1.DataSource = miDataTable

End Sub

结束班

答案 1 :(得分:-1)

这样的事情可能有所帮助:

Dim miDataTable As DataTable

Sub PrepareDT()
    miDataTable = New DataTable
    miDataTable.Columns.Add("dCantidad", GetType(System.Int32))
    miDataTable.Columns.Add("idProducto", GetType(System.Int32))
    miDataTable.Columns.Add("mTotal", GetType(System.Decimal))

End Sub

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
    If Not Page.IsPostBack Then
        PrepareDT()
    End If

End Sub

(并从Page_Load方法中删除代码),以便您只在第一次访问页面时“重置”miDataTable。