Question : Add new row to datagrid for insertion vb.net

Hello Experts,
When i'm using the following code a new row is being inserted to grid but it is replacing the first data row in the grid and makes it in edit mode. can you please chk where i'm missing the steps if any. the grid is working otherwise in edit mode. Please check at btnadd_click and new row isert as well as grid ItemCommand. This is urgent please.

Public Class sample
'    Inherits System.Web.UI.Page

  Inherits inlibrary.BasePage

        Protected WithEvents idSiteMenu As inlibrary.SiteMenuControl
          Protected WithEvents idCurrentMenu As CurrentMenu
        Protected WithEvents idErrorTable As HtmlTableRow
          Protected WithEvents idErrorText As Label
          Protected WithEvents btnAdd As Button
        Protected WithEvents idMessageTable As HtmlTableRow
        Protected WithEvents idMessageText As Label
      Protected WithEvents idInfoGrid As DataGrid
       Protected idSiteHeader As SiteHeader

          Private _dt As DataTable
          Private _mode As Modes = Modes.Standard

Public Enum Modes
        Standard
        ConfirmAdd
    End Enum

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    erStepThrough()> Private Sub InitializeComponent()

    End Sub

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
        SecurityEnabled = False
    End Sub

#End Region


 
    Private Sub idInfoGrid_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles idInfoGrid.ItemCommand


        Dim cn As SqlClient.SqlConnection
        Dim cmd As SqlClient.SqlCommand
        Dim st As SqlClient.SqlTransaction
        Dim temp_date As System.DateTime
        Dim strMessage As String, strFlg As String, strStatus As String
        Dim result As Integer

        idInfoGrid.EditItemIndex = -1

        Select Case e.CommandName
            Case "Insert"
                'Insert code here
                'idInfoGrid.DataBind()

            Case "Edit"
                idInfoGrid.EditItemIndex = e.Item.ItemIndex
                idInfoGrid.Columns(6).Visible = False
                idInfoGrid.Columns(7).Visible = False
                idInfoGrid.DataBind()

            Case "Cancel"
                'idInfoGrid.EditItemIndex = -1
                idInfoGrid.Columns(6).Visible = True
                idInfoGrid.Columns(7).Visible = True
                Dim ecc As WebLibrary.EditCommandColumn = DirectCast(idInfoGrid.Columns(0), WebLibrary.EditCommandColumn)
                ecc.UpdateText = "Update"
                idInfoGrid.DataBind()

            Case "Update"
                ' Validate the entered info, and log which entries were changed

                    idInfoGrid.EditItemIndex = -1
                    DataBind()
                    idInfoGrid.Columns(6).Visible = True
                    idInfoGrid.Columns(7).Visible = True

        End Select
    End Sub


    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        '   idSiteHeader.Title = idSiteMenu.MenuDescription
        If Not IsPostBack Then
            idMessageTable.Visible = False
            idErrorTable.Visible = False
        End If
        DataBind()

    End Sub

    Public Overrides Sub DataBind()

        SetUpGrid()
        FillGrid()

    End Sub

Private Sub SetUpGrid()
        ' Create the columns for the datagrid if they don't already exist.

        Dim ec As WebLibrary.EditCommandColumn
        Dim bc As BoundColumn
        Dim lb As WebLibrary.LinkButtonColumn

        If idInfoGrid.Columns.Count = 0 Then
            ec = New WebLibrary.EditCommandColumn
            ec.HeaderText = " Function "
            ec.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
            ec.HeaderStyle.Font.Bold = True
            ec.HeaderStyle.BackColor = Color.Blue
            ec.ButtonType = ButtonColumnType.LinkButton
            ec.ItemStyle.CssClass = "submenu"
            ec.EditText = "Edit"
            ec.CancelText = "Cancel"
            ec.UpdateText = "Update"
            idInfoGrid.Columns.Add(ec)


            bc = New BoundColumn
            bc.HeaderText = " Account "
            bc.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
            bc.HeaderStyle.Font.Bold = True
            bc.HeaderStyle.BackColor = Color.Blue
            'bc.ItemStyle.CssClass = "submenu"
            bc.ItemStyle.HorizontalAlign = HorizontalAlign.Center
            'bc.ItemStyle.Width = New Unit("100px")
            bc.DataField = "Account"
            idInfoGrid.Columns.Add(bc)

            bc = New BoundColumn
            bc.HeaderText = " Account Name "
            bc.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
            bc.HeaderStyle.Font.Bold = True
            bc.HeaderStyle.BackColor = Color.Blue
            'bc.ItemStyle.CssClass = "submenu"
            bc.ItemStyle.HorizontalAlign = HorizontalAlign.Center
            'bc.ItemStyle.Width = New Unit("100px")
            bc.ReadOnly = True
            bc.DataField = "AccountName"
            idInfoGrid.Columns.Add(bc)

            bc = New BoundColumn
            bc.HeaderText = " Billed "
            bc.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
            bc.HeaderStyle.Font.Bold = True
            bc.HeaderStyle.BackColor = Color.Blue
            'bc.ItemStyle.CssClass = "submenu"
            bc.ItemStyle.HorizontalAlign = HorizontalAlign.Center
            bc.DataField = "DisplayStatus"
            idInfoGrid.Columns.Add(bc)

            bc = New BoundColumn
            bc.HeaderText = " Label "
            bc.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
            bc.HeaderStyle.Font.Bold = True
            bc.HeaderStyle.BackColor = Color.Blue
            'bc.ItemStyle.CssClass = "submenu"
            bc.ItemStyle.HorizontalAlign = HorizontalAlign.Center
            bc.DataField = "DisplayActiveFlag"
            idInfoGrid.Columns.Add(bc)

            bc = New BoundColumn
            bc.HeaderText = " Price "
            bc.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
            bc.HeaderStyle.Font.Bold = True
            bc.HeaderStyle.BackColor = Color.Blue
            'bc.ItemStyle.CssClass = "submenu"
            bc.ItemStyle.HorizontalAlign = HorizontalAlign.Center
            bc.DataField = "Price"
            bc.DataFormatString = "{0:c}"
            idInfoGrid.Columns.Add(bc)

            bc = New BoundColumn
            bc.HeaderText = " Created "
            bc.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
            bc.HeaderStyle.Font.Bold = True
            bc.HeaderStyle.BackColor = Color.Blue
            'bc.ItemStyle.CssClass = "submenu"
            bc.ItemStyle.HorizontalAlign = HorizontalAlign.Center
            bc.DataField = "DisplayCreated"
            idInfoGrid.Columns.Add(bc)

            bc = New BoundColumn
            bc.HeaderText = " Modified "
            bc.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
            bc.HeaderStyle.Font.Bold = True
            bc.HeaderStyle.BackColor = Color.Blue
            'bc.ItemStyle.CssClass = "submenu"
            bc.ItemStyle.HorizontalAlign = HorizontalAlign.Center
            bc.DataField = "DisplayModified"
            idInfoGrid.Columns.Add(bc)

        End If

        If idInfoGrid.EditItemIndex > -1 Then
            idInfoGrid.Columns(6).Visible = False
            idInfoGrid.Columns(7).Visible = False

        End If

    End Sub

 
    Private Sub FillGrid()
        ' populate the datagrid

        Dim cn As SqlClient.SqlConnection
        Dim cmd As SqlClient.SqlCommand
        Dim sda As SqlClient.SqlDataAdapter
        Dim temp_date As System.DateTime, temp_date2 As System.DateTime

        cn = CType(Page, WebLibrary.BasePage).Connection("AEMisc")

        Try

            If _dt Is Nothing Then
                _dt = New DataTable
            Else
                _dt.Clear()
            End If

            cmd = cn.CreateCommand

            If _mode = Modes.Standard Then

                ' ElseIf _mode = Modes.ConfirmDelete Then
            End If

            cmd.CommandText = "SELECT * FROM 888888"
            If _filterText.Length > 0 Then
                cmd.CommandText += " Where Account " + _filterText
            End If

            If Len(cmd.CommandText) > 0 Then

                sda = New SqlClient.SqlDataAdapter(cmd)
                sda.Fill(_dt)

                FormatDateField()
                FormatStatus()

            End If
        Finally
            If Not sda Is Nothing Then sda.Dispose()
            If Not cn Is Nothing Then cn.Dispose()
        End Try

    End Sub

    Private Sub InsertEmpty()
        _dt.Rows.InsertAt(_dt.NewRow(), 0)
    End Sub

    Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        'inserting()
        idInfoGrid.EditItemIndex = 0

        ' modify text
        Dim ecc As WebLibrary.EditCommandColumn = DirectCast(idInfoGrid.Columns(0), WebLibrary.EditCommandColumn)
        ecc.UpdateText = "Insert"


            ' fill table, insert empty row, bind to datagrid
            FillGrid()
            InsertEmpty()

            idInfoGrid.DataSource = _dt
            If _mode = Modes.Standard Then      ' no paging needed for delete, can only delete one at a time.
                idInfoGrid.AllowPaging = True
                idInfoGrid.PageSize = 19
                idInfoGrid.PagerStyle.Mode = PagerMode.NumericPages
                idInfoGrid.PagerStyle.CssClass = "a"
            Else
                idInfoGrid.AllowPaging = False
            End If
        idInfoGrid.DataBind()
    End Sub
End Class

Answer : Add new row to datagrid for insertion vb.net

Tried another approach through one of our existing code and it is working fine now.
Thanks for your help.
Random Solutions  
 
programming4us programming4us