Wednesday, 2 October 2013

Bind,Save,Edit,Update,Cancel,Delete,Paging example in GridView in ASP.NET using VB.NET

HI friends, in this article I will explain about Bind,Save,Edit,Update,Cancel,Delete,Paging example in GridView in ASP.NET using C# or VB.NET.
VB.NET Code:
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Drawing

Partial Public Class EditGrid
    Inherits System.Web.UI.Page
    Private con As New SqlConnection(ConfigurationManager.ConnectionStrings("con").ConnectionString)
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        If Not IsPostBack Then
            bindData()
        End If
    End Sub
    Protected Sub bindData()

        Dim cmd As New SqlCommand("select * from user_data", con)
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet()
        da.Fill(ds)
        If ds.Tables(0).Rows.Count > 0 Then
            grd.DataSource = ds
            grd.DataBind()
        Else
            ds.Tables(0).Rows.Add(ds.Tables(0).NewRow())
            grd.DataSource = ds
            grd.DataBind()
            Dim columncount As Integer = grd.Rows(0).Cells.Count
            grd.Rows(0).Cells.Clear()
            grd.Rows(0).Cells.Add(New TableCell())
            grd.Rows(0).Cells(0).ColumnSpan = columncount
            grd.Rows(0).Cells(0).Text = "No Records Found"
        End If
    End Sub
    Protected Sub grd_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
        grd.PageIndex = e.NewPageIndex
        bindData()
    End Sub
    Protected Sub grd_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
        grd.EditIndex = e.NewEditIndex
        bindData()
    End Sub
    Protected Sub grd_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
        grd.EditIndex = -1
        bindData()
    End Sub
    Protected Sub grd_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
        grd.EditIndex = -1
        Dim id As Integer = Convert.ToInt32(DirectCast(grd.Rows(e.RowIndex).FindControl("txtUid"), TextBox).Text)
        Dim username As String = DirectCast(grd.Rows(e.RowIndex).FindControl("txtUname"), TextBox).Text
        Dim lastname As String = DirectCast(grd.Rows(e.RowIndex).FindControl("txtLname"), TextBox).Text
        Dim cmd As New SqlCommand("update user_data set  username= '" & username & "' ,lastname='" & lastname & "' where userid= ' " & id & " ' ", con)
        con.Open()
        cmd.ExecuteNonQuery()
        con.Close()
        bindData()
    End Sub
    Protected Sub grd_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
        Dim id As Integer = Convert.ToInt32(DirectCast(grd.Rows(e.RowIndex).FindControl("lblUID"), Label).Text)
        Dim cmd As New SqlCommand("delete user_data where userid= ' " & id & " ' ", con)
        con.Open()
        cmd.ExecuteNonQuery()
        con.Close()
        bindData()
    End Sub
End Class





ASP.NET Code
C# code
Click here ASP.NET code

Click here C# code

No comments:

Post a Comment

© 2012-2014 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.