• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1210
  • Last Modified:

GRIDVIEW INSERT/UPDATE

I want to do an update on GRIDVIEW IN AJAX Application with VB.Net.
When i click on the UPDATE button on GRIDVIEW, how can i select the new values and old values. Can someone give me the piece of code in VB.net at the event of ROWUPDATING.

Thanks!
0
salmanfazal
Asked:
salmanfazal
1 Solution
 
carlnorrbomCommented:
Hi,

Please review the attached code sample which does just what You're after (managing the edit / update events from code behind). To make it AJAX, just put the GridView inside and ajax update panel.

/Carl.
GridViewPage.aspx:
 
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="GridViewPage.aspx.vb" Inherits="GridViewPage" %>
 
<!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>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" 
            CellPadding="4" ForeColor="#333333" GridLines="None">
            <RowStyle BackColor="#EFF3FB" />
            <Columns>
                <asp:CommandField CausesValidation="False" ShowEditButton="True" />
                <asp:BoundField DataField="ID" HeaderText="ID:" SortExpression="ID" 
                    Visible="True" />
                <asp:TemplateField HeaderText="First Name:" SortExpression="FirstName">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Last Name:" SortExpression="LastName">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtLastName" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Company:" SortExpression="Company">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtCompany" runat="server" Text='<%# Bind("Company") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("Company") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>
 
GridViewPage.aspx.vb:
 
Imports System.Data
Imports System.Data.SqlClient
 
Partial Class GridViewPage
    Inherits System.Web.UI.Page
 
    Private dbConn As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            BindGrid(GridView1)
        End If
    End Sub
 
    Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
        GridView1.EditIndex = e.NewEditIndex
        BindGrid(GridView1)
        GridView1.Rows(e.NewEditIndex).Cells(1).Enabled = False 'Protect ID Column
    End Sub
 
    Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
        GridView1.EditIndex = -1
        BindGrid(GridView1)
    End Sub
 
    Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
        Dim strUpdateCustomers As String = "UPDATE Customers SET FirstName = @FirstName, LastName = @LastName, Company = @Company WHERE (ID = @ID)"
        Dim cmdUpdateCustomers As New SqlCommand(strUpdateCustomers, dbConn)
 
        cmdUpdateCustomers.Parameters.AddWithValue("@ID", GridView1.DataKeys(e.RowIndex).Values(0))
        cmdUpdateCustomers.Parameters.AddWithValue("@FirstName", CType(GridView1.Rows(e.RowIndex).FindControl("txtFirstName"), TextBox).Text)
        cmdUpdateCustomers.Parameters.AddWithValue("@LastName", CType(GridView1.Rows(e.RowIndex).FindControl("txtLastName"), TextBox).Text)
        cmdUpdateCustomers.Parameters.AddWithValue("@Company", CType(GridView1.Rows(e.RowIndex).FindControl("txtCompany"), TextBox).Text)
 
        Try
            If Not dbConn.State = ConnectionState.Open Then
                dbConn.Open()
            End If
            cmdUpdateCustomers.ExecuteNonQuery()
        Catch ex As Exception
            'Logic to handle exception
        End Try
        If Not dbConn.State = ConnectionState.Closed Then
            dbConn.Close()
        End If
        e.Cancel = True
        GridView1.EditIndex = -1
        BindGrid(GridView1)
    End Sub
 
    Private Sub BindGrid(ByVal gv As GridView)
        Dim strSelectCustomers As String = "SELECT * FROM Customers ORDER BY LastName"
        Dim cmdSelectCustomers As New SqlDataAdapter(strSelectCustomers, dbConn)
        Dim dtCustomers As New DataTable()
        cmdSelectCustomers.Fill(dtCustomers)
        gv.DataSource = dtCustomers.DefaultView()
        gv.DataBind()
    End Sub
 
End Class

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now