Click here to Skip to main content
15,911,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Page.IsPostBack Then
            Generate_Datatable()
            geneate()

           
        End If
     

    End Sub
    Private Sub Generate_Datatable()
        Dim _InvDt As DataTable = New DataTable
        Dim _Dr As DataRow
        _InvDt.Columns.Add("PartNo")
        _InvDt.Columns.Add("PartDesc")
        _InvDt.Columns.Add("UOM")
        _InvDt.Columns.Add("Qty")
        _InvDt.Columns.Add("Price")
        _InvDt.Columns.Add("Amount")
        _Dr = _InvDt.NewRow
        _InvDt.Rows.Add(_Dr)

        GridView1.DataSource = _InvDt
        GridView1.DataBind()

        ViewState("InvDt") = _InvDt
    End Sub
    Private Sub Generate_Row(ByRef InvDt As DataTable)

        Dim _Dr As DataRow
        _Dr = InvDt.NewRow
        InvDt.Rows.Add(_Dr)
        GridView1.DataSource = InvDt
        GridView1.DataBind()
    End Sub
    Private Sub Delete_Row(ByRef pInvDt As DataTable)
        txttotal.Text = ""
        Dim i As Integer = GridView1.Rows.Count
        Dim _Dr As DataRow = pInvDt.Rows.Item(i - 1)
        pInvDt.Rows.Remove(_Dr)
        GridView1.DataSource = pInvDt
        GridView1.DataBind()
        ViewState("InvDt") = pInvDt
    End Sub
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnaddrow.Click

        Dim InvDt As DataTable = CType(ViewState("InvDt"), DataTable)
        If GridView1.Rows.Count < 2 Then
            Generate_Row(InvDt)
        End If
    End Sub
    Protected Sub btndelrow_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btndelrow.Click
        txttotal.Text = 0
        Dim _InvDt As DataTable = CType(ViewState("InvDt"), DataTable)
        If GridView1.Rows.Count <= 0 Then
            ScriptManager.RegisterStartupScript(Me, GetType(String), "Message", "alert('No Rows are Exist');", True)
        Else
            Delete_Row(_InvDt)
        End If
    End Sub
Posted
Updated 2-Oct-11 20:42pm
v2

Or a list of objects, that's the way I prefer, I dislike DataTable very much.
See Bind List to GridView[^]
 
Share this answer
 
That is probably the best way you have in your code, you can however create a XML file as an template and load that in your dataset, but the current way is better as you see what you have done in your code and don't have to wade through your solution project to track down the dataset schema.
 
Share this answer
 
Comments
Simon Bang Terkildsen 3-Oct-11 2:52am    
I want to give you a 5, but the fact that you do not throw some hate at DataTable and my great hate of it, means I can only give a 4 :)
Also a List is also an option.
Mehdi Gholam 3-Oct-11 3:02am    
A List is good too.

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