This program will show how to deleting selected rows from gridview using checkbox in vb.net
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Public temptable As DataTable
Public ds As DataSet
Public editrow As DataRow
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
temptable = Session("cart")
editrow = temptable.NewRow
editrow("Item_Name") = TextBox1.Text
temptable.Rows.Add(editrow)
Session("cart") = temptable
GridView1.DataSource = temptable
GridView1.DataBind()
End Sub
'function for filling datatable
Public Function makecart()
Try
temptable = New DataTable("cart")
temptable.Columns.Add("id", GetType(Integer))
temptable.Columns("id").AutoIncrement = True
temptable.Columns("id").AutoIncrementSeed = 1
temptable.Columns.Add("Item_Name", GetType(String))
'put table in session
Session("cart") = temptable
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Function
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
makecart()
End If
End Sub
'delete selected items
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
temptable = Session("cart")
For i As Integer = 0 To temptable.Rows.Count - 1
If CType(GridView1.Rows(i).FindControl("CheckBox1"), CheckBox).Checked = True Then
temptable.Rows(i).Delete()
Else
Dim a As Integer = 0
End If
Next
Session("cart") = temptable
GridView1.DataSource = temptable
GridView1.DataBind()
End Sub
'selecting all at a time this handel check box in header row
Protected Sub CheckBox2_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.RowCommand
If CType(GridView1.HeaderRow.FindControl("CheckBox2"), CheckBox).Checked = True Then
For i As Integer = 0 To GridView1.Rows.Count - 1
CType(GridView1.Rows(i).FindControl("CheckBox1"), CheckBox).Checked = True
Next
Else
For i As Integer = 0 To GridView1.Rows.Count - 1
CType(GridView1.Rows(i).FindControl("CheckBox1"), CheckBox).Checked = False
Next
End If
End Sub
End Class
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Public temptable As DataTable
Public ds As DataSet
Public editrow As DataRow
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
temptable = Session("cart")
editrow = temptable.NewRow
editrow("Item_Name") = TextBox1.Text
temptable.Rows.Add(editrow)
Session("cart") = temptable
GridView1.DataSource = temptable
GridView1.DataBind()
End Sub
'function for filling datatable
Public Function makecart()
Try
temptable = New DataTable("cart")
temptable.Columns.Add("id", GetType(Integer))
temptable.Columns("id").AutoIncrement = True
temptable.Columns("id").AutoIncrementSeed = 1
temptable.Columns.Add("Item_Name", GetType(String))
'put table in session
Session("cart") = temptable
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Function
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
makecart()
End If
End Sub
'delete selected items
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
temptable = Session("cart")
For i As Integer = 0 To temptable.Rows.Count - 1
If CType(GridView1.Rows(i).FindControl("CheckBox1"), CheckBox).Checked = True Then
temptable.Rows(i).Delete()
Else
Dim a As Integer = 0
End If
Next
Session("cart") = temptable
GridView1.DataSource = temptable
GridView1.DataBind()
End Sub
'selecting all at a time this handel check box in header row
Protected Sub CheckBox2_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.RowCommand
If CType(GridView1.HeaderRow.FindControl("CheckBox2"), CheckBox).Checked = True Then
For i As Integer = 0 To GridView1.Rows.Count - 1
CType(GridView1.Rows(i).FindControl("CheckBox1"), CheckBox).Checked = True
Next
Else
For i As Integer = 0 To GridView1.Rows.Count - 1
CType(GridView1.Rows(i).FindControl("CheckBox1"), CheckBox).Checked = False
Next
End If
End Sub
End Class
1 comment:
very fine
Post a Comment