Programming for Everybody: Update insert and delete gridview without database asp.net (vb.net)

Update insert and delete gridview without database asp.net (vb.net)

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Name    <asp:TextBox ID="TxtName" runat="server" ></asp:TextBox>
       Age<asp:TextBox ID="txtAge" runat="server" ></asp:TextBox>
        <asp:Button ID="btnInsert" Text="Save"  runat="server" OnClick="btnInsert_Click"  />
      <%--  <asp:Button ID="btnUpdate" Text="Update" runat="server" OnClick="btnUpdate_Click" />--%>
    <asp:GridView ID="grdView" runat="server" OnRowCommand="grdView_RowCommand" OnRowDeleting="grdView_RowDeleting"
>
        <Columns>
            <asp:TemplateField>

                <ItemTemplate>
                    <asp:Button ID="btn" runat="server" Text="Delete"  CommandName="delete" CommandArgument='<%# Container.DataItemIndex %>' />
            </ItemTemplate>

         <%--   <ItemTemplate>
                <asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" />
            </ItemTemplate>--%>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button runat="server" CommandName="Change" ID="btnEdit" Text="Edit" CommandArgument='<%# Container.DataItemIndex %>'  />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>

        </asp:TemplateField>
    </Columns>
</asp:GridView>
</div>
    </form>
</body>
</html>

ــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ
Imports System.Data.OleDb
Imports System.IO
Imports System.Configuration
Imports System.Data
Imports System.Data.DataTable
Partial Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    End Sub

    Public Shared dt As Datatable
    Public Shared i As Integer = 1

    Protected Sub btnInsert_Click(ByVal sender As Object, ByVal e As EventArgs)
        If Convert.ToString(ViewState("Row")) <> "" Then
            dt.Rows(Convert.ToInt32(ViewState("Row")))("Column1") = TxtName.Text
            dt.Rows(Convert.ToInt32(ViewState("Row")))("Column2") = txtAge.Text
            grdView.DataSource = dt
            grdView.DataBind()
            ViewState("Row") = ""
        ElseIf Convert.ToString(ViewState("Row")) = "" Then
            Session("datatable") = dt
            dt = New DataTable()
            Dim dr As DataRow = Nothing
            dt.Columns.Add(New DataColumn("RowNumber", GetType(Integer)))
            dt.Columns.Add(New DataColumn("Column1", GetType(String)))
            dt.Columns.Add(New DataColumn("Column2", GetType(String)))

            If Session("datatable") IsNot Nothing Then
                dt = CType(Session("datatable"), DataTable)
                dr = dt.NewRow()
                dr("RowNumber") = i
                dr("Column1") = TxtName.Text
                dr("Column2") = txtAge.Text
                dt.Rows.Add(dr)
            Else
                dr = dt.NewRow()
                dr("RowNumber") = i
                dr("Column1") = TxtName.Text
                dr("Column2") = txtAge.Text
                dt.Rows.Add(dr)
            End If

            grdView.DataSource = dt
            grdView.DataBind()
            i += 1
        End If
    End Sub

    Protected Sub grdView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
        If e.CommandName = "delete" Then
            dt = CType(Session("datatable"), DataTable)

            If dt.Rows.Count >= 0 Then
                dt.Rows.RemoveAt(Convert.ToInt16(e.CommandArgument))
                grdView.DataSource = dt
                grdView.DataBind()
            End If
        ElseIf e.CommandName = "Change" Then
            dt = CType(Session("datatable"), DataTable)
            TxtName.Text = grdView.Rows(Convert.ToInt32(e.CommandArgument)).Cells(4).Text
            txtAge.Text = grdView.Rows(Convert.ToInt32(e.CommandArgument)).Cells(5).Text
            ViewState("Row") = e.CommandArgument

            If dt.Rows.Count >= 0 Then
                grdView.DataSource = dt
                grdView.DataBind()
            End If
        End If
    End Sub

    Protected Sub grdView_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
        grdView.DataSource = dt
        grdView.DataBind()
    End Sub

    Protected Sub grdView_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        TxtName.Text = grdView.SelectedRow.Cells(2).Text
    End Sub
End Class


No comments:

Post a Comment