Results 1 to 3 of 3

Thread: Need a help on Gridview

  1. #1

    Thread Starter
    Registered User
    Join Date
    Apr 2018
    Posts
    1

    Exclamation Need a help on Gridview

    I'm working on a asp.net vb web page. i have created a grid-view which will be adding data on a button click event. but in the 1st button click event it is added two row to the grid-view and after the 1st button click situation is normal. Can some one help me to avoid this duplication in the 1st event?

    here I attached the code and a screen print. I need to avoid the row with null values(plz see attcahement).

    Code:
    Name:  Capture.jpg
    Views: 232
    Size:  15.7 KB
    
    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Collections
    
    Partial Class add_stock
        Inherits System.Web.UI.Page
    
        Private numOfColumns As Integer = 1
        Private ctr As Integer = 0
        Private tablepanel As Table = Nothing
    
    
        Dim row As New TableRow()
        Dim cell As New TableCell()
        Dim tb9 As New Button
        Dim panel2 As New Panel
        Dim DT As New DataTable
    
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
            If Not IsPostBack Then
                tablepanel = New Table()
                tablepanel.ID = "tableBuild"
                Session("table") = tablepanel
                ViewState("ctr") = ctr
                'BindGridview()
            End If
            ctr = CType(ViewState("ctr"), Int32)
            tablepanel = DirectCast(Session("table"), Table)
            Panel1.Controls.Add(tablepanel)
    
            DT.Columns.Add("ID")
            DT.Columns.Add("Name")
            DT.Columns(0).AutoIncrement = True
    
        End Sub
        
    
        Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim drCurrentRow As DataRow = Nothing
            If GridView1.Rows.Count > 1 Then
                AddNewRow()
            ElseIf GridView1.Rows.Count = 1 Then
                AddNewRow()
            Else
                BindGridview()
                AddNewRow()
            End If
            'Place the table with textboxes on the page
            '-------- numOfColumns = 1
            '-------- GenerateTable(numOfColumns)
            'End of the table display with textboxes 
    
            ' Add row to grid view
        End Sub
    
        Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs)
            ClientScript.RegisterStartupScript(Me.GetType, "Button_Alert", "alert('Button clicked!');", True)
            numOfColumns = 1
            'Generate the Table based from the inputs
            GenerateTable(numOfColumns)
        End Sub
        Protected Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            GridView1.ShowFooter = True
        End Sub
    
        Protected Sub BindGridview()
            Dim dt As New DataTable()
            'dt.Columns.Add("rowid", GetType(Integer))
            dt.Columns.Add("Item Number", GetType(String))
            dt.Columns.Add("Item Category", GetType(String))
            dt.Columns.Add("Lead Time", GetType(String))
            dt.Columns.Add("Supplier", GetType(String))
            dt.Columns.Add("Item Quantity", GetType(String))
            dt.Columns.Add("Re-order Level", GetType(String))
            dt.Columns.Add("Unit Price", GetType(String))
            dt.Columns.Add("Retails Price", GetType(String))
            dt.Columns.Add("Item Details", GetType(String))
            Dim dr As DataRow = dt.NewRow()
            dr("Item Number") = String.Empty
            dr("Item Category") = String.Empty
            dr("Lead Time") = String.Empty
            dr("Supplier") = String.Empty
            dr("Item Quantity") = String.Empty
            dr("Re-order Level") = String.Empty
            dr("Unit Price") = String.Empty
            dr("Retails Price") = String.Empty
            dr("Item Details") = String.Empty
            dt.Rows.Add(dr)
            ViewState("Curtbl") = dt
            GridView1.DataSource = dt
            'GridView1.DataBind()
        End Sub
    
        Private Sub AddNewRow()
    
            Dim rowIndex As Integer
    
            If ViewState("Curtbl") IsNot Nothing Then
                Dim dt As DataTable = DirectCast(ViewState("Curtbl"), DataTable)
                Dim drCurrentRow As DataRow = Nothing
                If dt.Rows.Count > 1 Then
                    Dim rowcount As Integer = dt.Rows.Count
                    drCurrentRow = dt.NewRow()
                    '   drCurrentRow("rowid") = rowcount + 1
                    dt.Rows(rowcount - 1)("Item Number") = TextBox21.Text
                    dt.Rows(rowcount - 1)("Item Category") = DropDownList4.Text
                    dt.Rows(rowcount - 1)("Lead Time") = TextBox19.Text
                    dt.Rows(rowcount - 1)("Supplier") = DropDownList3.Text
                    dt.Rows(rowcount - 1)("Item Quantity") = TextBox22.Text
                    dt.Rows(rowcount - 1)("Re-order Level") = TextBox17.Text
                    dt.Rows(rowcount - 1)("Unit Price") = TextBox23.Text
                    dt.Rows(rowcount - 1)("Retails Price") = TextBox20.Text
                    dt.Rows(rowcount - 1)("Item Details") = TextBox18.Text
                    dt.Rows.Add()
                    rowIndex += 1
                    ViewState("Curtbl") = dt
                    GridView1.DataSource = dt
                    GridView1.DataBind()
                    MsgBox("1st")
    
                ElseIf dt.Rows.Count = 1 Then
                    Dim rowcount As Integer = dt.Rows.Count
                    'drCurrentRow = dt.NewRow()
                    '   drCurrentRow("rowid") = rowcount + 1
                    MsgBox(rowcount + 1)
                    dt.Rows(rowcount - 1)("Item Number") = TextBox21.Text
                    dt.Rows(rowcount - 1)("Item Category") = DropDownList4.Text
                    dt.Rows(rowcount - 1)("Lead Time") = TextBox19.Text
                    dt.Rows(rowcount - 1)("Supplier") = DropDownList3.Text
                    dt.Rows(rowcount - 1)("Item Quantity") = TextBox22.Text
                    dt.Rows(rowcount - 1)("Re-order Level") = TextBox17.Text
                    dt.Rows(rowcount - 1)("Unit Price") = TextBox23.Text
                    dt.Rows(rowcount - 1)("Retails Price") = TextBox20.Text
                    dt.Rows(rowcount - 1)("Item Details") = TextBox18.Text
                    dt.Rows.Add()
                    rowIndex += 1
    
                    ViewState("Curtbl") = dt
                    GridView1.DataSource = dt
                    GridView1.DataBind()
                    MsgBox("2nd")
                Else
                    Dim rowcount As Integer = dt.Rows.Count
                    drCurrentRow = dt.NewRow()
                    '  drCurrentRow("rowid") = rowcount + 1
                    dt.Rows(1)("Item Number") = TextBox21.Text
                    dt.Rows(1)("Item Category") = DropDownList4.Text
                    dt.Rows(1)("Lead Time") = TextBox19.Text
                    dt.Rows(1)("Supplier") = DropDownList3.Text
                    dt.Rows(1)("Item Quantity") = TextBox22.Text
                    dt.Rows(1)("Re-order Level") = TextBox17.Text
                    dt.Rows(1)("Unit Price") = TextBox23.Text
                    dt.Rows(1)("Retails Price") = TextBox20.Text
                    dt.Rows(1)("Item Details") = TextBox18.Text
                    dt.Rows.Add()
                    rowIndex += 1
                    ViewState("Curtbl") = dt
                    GridView1.DataSource = dt
                    GridView1.DataBind()
                    MsgBox("3rd")
                End If
            Else
                Response.Write("ViewState Value is Null")
            End If
    
        End Sub
    
    
        Protected Sub OnRowDeleting(sender As Object, e As GridViewDeleteEventArgs)
    
            Dim dt As DataTable = TryCast(ViewState("Curtbl"), DataTable)
            Dim RowToDelete As Integer = e.RowIndex
    
            If dt.Rows.Count > 0 Then
                dt.Rows.RemoveAt(RowToDelete)
                dt.AcceptChanges()
                GridView1.DataSource = dt
            End If
            GridView1.DataBind()
        End Sub
    
        Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs)
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim item As String = e.Row.Cells(0).Text
                For Each button As Button In e.Row.Cells(2).Controls.OfType(Of Button)()
                    If button.CommandName = "Delete" Then
                        button.Attributes("onclick") = "if(!confirm('Do you want to delete " + item + "?')){ return false; };"
                    End If
                Next
            End If
        End Sub
    
    End Class
    Attached Files Attached Files

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Need a help on Gridview

    Welcome to VBForums

    Thread moved from the 'ASP, VB Script' forum to the 'ASP.Net' forum

  3. #3
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: Need a help on Gridview

    Check if the datatable that is bound to the GridView control has an empty row, if yes then add logic to remove that row.

    - kgc
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width