ASP.NET delete specific rows from gridview

Multi tool use
Multi tool use


ASP.NET delete specific rows from gridview



I need help with following problem.
I try to delete specific rows from gridview.
Code bellow works well, but when page is loading its show all data in database.
How can I filter data? I mean when page is loading it shows nothing, but after type some text in text box, it shows me rows with that text and I can delete some of that rows with text from text box by check checkbox and button.



This is my code. Pls help me.


Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If IsPostBack Then
GetData()
End If
BindGrid()
End Sub

Private Sub BindGrid()
Dim constr As String = ConfigurationManager _
.ConnectionStrings("conString").ConnectionString()
Dim query As String = "select * from TestCustomers"
Dim con As New SqlConnection(constr)
Dim sda As New SqlDataAdapter(query, con)
Dim dt As New DataTable()
sda.Fill(dt)
gvAll.DataSource = dt
gvAll.DataBind()
End Sub

Protected Sub OnPaging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
gvAll.PageIndex = e.NewPageIndex
gvAll.DataBind()
SetData()
End Sub

Private Sub GetData()
Dim arr As ArrayList
If ViewState("SelectedRecords") IsNot Nothing Then
arr = DirectCast(ViewState("SelectedRecords"), ArrayList)
Else
arr = New ArrayList()
End If
Dim chkAll As CheckBox = DirectCast(gvAll.HeaderRow _
.Cells(0).FindControl("chkAll"), CheckBox)
For i As Integer = 0 To gvAll.Rows.Count - 1
If chkAll.Checked Then
If Not arr.Contains(gvAll.DataKeys(i).Value) Then
arr.Add(gvAll.DataKeys(i).Value)
End If
Else
Dim chk As CheckBox = DirectCast(gvAll.Rows(i).Cells(0) _
.FindControl("chk"), CheckBox)
If chk.Checked Then
If Not arr.Contains(gvAll.DataKeys(i).Value) Then
arr.Add(gvAll.DataKeys(i).Value)
End If
Else
If arr.Contains(gvAll.DataKeys(i).Value) Then
arr.Remove(gvAll.DataKeys(i).Value)
End If
End If
End If
Next
ViewState("SelectedRecords") = arr
End Sub

Private Sub SetData()
Dim currentCount As Integer = 0
Dim chkAll As CheckBox = DirectCast(gvAll.HeaderRow _
.Cells(0).FindControl("chkAll"), CheckBox)
chkAll.Checked = True
Dim arr As ArrayList = DirectCast(ViewState("SelectedRecords") _
, ArrayList)
For i As Integer = 0 To gvAll.Rows.Count - 1
Dim chk As CheckBox = DirectCast(gvAll.Rows(i).Cells(0) _
.FindControl("chk"), CheckBox)
If chk IsNot Nothing Then
chk.Checked = arr.Contains(gvAll.DataKeys(i).Value)
If Not chk.Checked Then
chkAll.Checked = False
Else
currentCount += 1
End If
End If
Next
hfCount.Value = (arr.Count - currentCount).ToString()
End Sub

Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim count As Integer = 0
SetData()
gvAll.AllowPaging = False
gvAll.DataBind()
Dim arr As ArrayList = DirectCast(ViewState("SelectedRecords") _
, ArrayList)
count = arr.Count
For i As Integer = 0 To gvAll.Rows.Count - 1
If arr.Contains(gvAll.DataKeys(i).Value) Then
DeleteRecord(gvAll.DataKeys(i).Value.ToString())
arr.Remove(gvAll.DataKeys(i).Value)
End If
Next
ViewState("SelectedRecords") = arr
hfCount.Value = "0"
gvAll.AllowPaging = True
BindGrid()
ShowMessage(count)
End Sub

Private Sub DeleteRecord(ByVal CustomerID As String)
Dim constr As String = ConfigurationManager _
.ConnectionStrings("conString").ConnectionString
Dim query As String = "delete from TestCustomers where" & _
" CustomerID=@CustomerID"
Dim con As New SqlConnection(constr)
Dim cmd As New SqlCommand(query, con)
cmd.Parameters.AddWithValue("@CustomerID", CustomerID)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Sub

Private Sub ShowMessage(ByVal count As Integer)
Dim sb As New StringBuilder()
sb.Append("<script type = 'text/javascript'>")
sb.Append("alert('")
sb.Append(count.ToString())
sb.Append(" records deleted.');")
sb.Append("</script>")
ClientScript.RegisterStartupScript(Me.GetType(), _
"script", sb.ToString())
End Sub

<asp:GridView ID="gvAll" runat="server"
AutoGenerateColumns = "false" Font-Names = "Arial"
Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B"
HeaderStyle-BackColor = "green" AllowPaging ="true"
OnPageIndexChanging = "OnPaging" DataKeyNames = "CustomerID"
PageSize = "10" >
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" onclick = "checkAll(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" onclick = "Check_Click(this)"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ItemStyle-Width = "150px" DataField = "ContactName" HeaderText = "Contact Name"/>
<asp:BoundField ItemStyle-Width = "150px" DataField = "Country" HeaderText = "Country"/>
<asp:BoundField ItemStyle-Width = "150px" DataField = "City" HeaderText = "City"/>
</Columns>
<AlternatingRowStyle BackColor="#C2D69B" />
</asp:GridView>
<asp:HiddenField ID="hfCount" runat="server" Value = "0" />
<asp:Button ID="btnDelete" runat="server" Text="Delete Checked Records" OnClientClick = "return ConfirmDelete();" OnClick="btnDelete_Click" />




2 Answers
2



Even though your question is not clear , i will answer what i have understood.



you want to show all data on page load and on postback you want to show selected rows , right ?



if yes



try this code
If IsPostBack Then
GetData()
End If



else
BindGrid()



if this has not answered your question , please tell me what exactly you want to do.





thank you.i mean when page is loading..datagrid is empty. so not show..after input text in text box then click search button ,all record with text from text box will show up..then i simply choose specific records that i want to delete and click delete button to delete selected rows..in my code all rows show when page is loading..but i want to choose rows that i want to delete with specific text...not from all records as now..
– Andrew Punio
Jun 29 at 6:38




This will delete all rows that had been selected.


For Each row As DataGridViewRow In yourDGV.SelectedRows
yourDGV.Rows.Remove(row)
Next


Private Sub btndelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndelete.Click
If DataGridView1.SelectedRows.Count > 0 Then
DataGridView1.Rows.RemoveAt(DataGridView1.CurrentRow.Index)
Else
MessageBox.Show("You must select a row")
End If
End Sub



Use this it may help full to you


Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As EventArgs)
For Each gvrow As GridViewRow In gvDetails.Rows
'Finiding checkbox control in gridview for particular row
Dim chkdelete As CheckBox = DirectCast(gvrow.FindControl("chkSelect"), CheckBox)
'Condition to check checkbox selected or not
If chkdelete.Checked Then
'Getting UserId of particular row using datakey value
Dim usrid As Integer = Convert.ToInt32(gvDetails.DataKeys(gvrow.RowIndex).Value)
Using con As New SqlConnection("YOUR SQL DATABASE CONNECTION")
con.Open()
Dim cmd As New SqlCommand("delete from UserDetails where UserId=" & usrid, con)
cmd.ExecuteNonQuery()
con.Close()
End Using
End If
Next
BindUserDetails()
End Sub





thank you for your code...but i want to delete rows by select them by checkbix and click button to delete selected rows
– Andrew Punio
Jun 30 at 1:26





you should not bind all the rows on page load, on page loading you have to show the empty grid, write the query to search the text from SQL table and execute the query, now show the result on grid with check boxes. Now which record do you want to delete select and click on the delete button .
– developer69
Jul 2 at 6:39






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

l8153mClxoNi
jKpeLR JY62xvxNM,xgl1UdA02kG2Yukdw9OXMX2ae,2

Popular posts from this blog

Rothschild family

Cinema of Italy