.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> VB.Net >> Post New QuestionBookmark and Share Subscribe to Forum

Delete Row in Gridview using checkbox

Posted By: Catherine     Posted Date: April 05, 2011    Points:5   Category :VB.Net
Hi I am using vb.net. I am trying to delete row in gridview using checkbox but when i debug it does not go into if condition.
here is my vb.net code:
Dim id As Integer
For Each row As GridViewRow In GridView1.Rows
Dim chkselect As CheckBox = row.FindControl("chkselect")
If chkselect.Checked Then
id = Convert.ToInt32(GridView1.DataKeys(row.RowIndex).Value)
Dim connection As OleDbConnection = New OleDbConnection(connectionString)
Dim cmd As New OleDbCommand("delete from stu_tbl where id=" & id & "", connection)
connection.Open()
cmd.ExecuteNonQuery()
MsgBox("Record deleted")
connection.Close()
End If
Next
bind()

Here is my source code:
<asp:BoundField DataField="id" SortExpression="id" HeaderText ="ID" />
<asp:BoundField DataField="name" SortExpression="name" HeaderText ="NAME"/>
<asp:BoundField DataField="mark" SortExpression="mark" HeaderText ="MARK"/>
<asp:TemplateField HeaderText="chk">
<ItemTemplate>
<asp:CheckBox ID="chkselect" runat="server"/>
</ItemTemplate>

</asp:TemplateField>
</Columns>

Responses
Author: Mahesh             
Posted Date: April 05, 2011     Points: 20   

Hi,
i think you need to delete rows from grid which are checked on the button click event which is on form
then use following code, you did not cast the control to checkbox try this,

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

For Each row As GridViewRow In GridView1.Rows
Dim CheckBox2 As CheckBox = DirectCast(row.FindControl("CheckBox2"), CheckBox)

If CheckBox2.Checked Then
' your code
End If

Next

End Sub

Thanks & Regards
Mahesh

Author: Catherine             
Posted Date: April 05, 2011     Points: 20   

thank you for your reply. again same problem

Author: Catherine             
Posted Date: April 05, 2011     Points: 20   

Thank you
I have written code in page_load as
If Not Page.IsPostBack Then
bind()
End If

now its working

Author: Maverick             
Posted Date: April 06, 2011     Points: 20   

Hello,

You can use commandargument and commandname property og grid to delete the rows.

Thanks,
Maverick

Thanks,
Maverick


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend