Click here to Skip to main content
15,887,256 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Dear

How to can i delete a row from gridview
which is dynamically added i.e on button click
now i added more rows which i dont want

now that i want be delete, how to delete that row?

Please any one can help me.

Thank You
Posted
Comments
Karthik Harve 1-Jan-12 23:32pm    
How you are adding a row to gridview..?? show that code..
Jagadeesh Bollabathini 2-Jan-12 6:52am    
Dim RowIndex As Integer = 0
Dim i As Integer
If (ViewState("CurrentTable")) IsNot Nothing Then
dt = DirectCast(ViewState("CurrentTable"), DataTable)
Dim dtCurrentRow As DataRow = Nothing

If dt.Rows.Count > 0 Then
For i = 1 To dt.Rows.Count
Dim box1 As TextBox = DirectCast(dgv.Rows(RowIndex).Cells(1).FindControl("txtDescrip"), TextBox)
Dim box2 As TextBox = DirectCast(dgv.Rows(RowIndex).Cells(2).FindControl("txtDuration"), TextBox)
Dim box3 As TextBox = DirectCast(dgv.Rows(RowIndex).Cells(3).FindControl("txtPredesessor"), TextBox)
Dim box4 As TextBox = DirectCast(dgv.Rows(RowIndex).Cells(4).FindControl("txtType"), TextBox)
Dim box5 As TextBox = DirectCast(dgv.Rows(RowIndex).Cells(5).FindControl("txtLag"), TextBox)
Dim box6 As TextBox = DirectCast(dgv.Rows(RowIndex).Cells(6).FindControl("txtFlow_Steps_ID"), TextBox)
Dim box7 As TextBox = DirectCast(dgv.Rows(RowIndex).Cells(7).FindControl("txtUnique_ID"), TextBox)
dtCurrentRow = dt.NewRow()
dtCurrentRow("SRNO") = i + 1
dt.Rows(i - 1)("DESCRIPTION") = box1.Text
dt.Rows(i - 1)("DURATION") = box2.Text
dt.Rows(i - 1)("PREDESESSOR") = box3.Text
dt.Rows(i - 1)("TYPE") = box4.Text
dt.Rows(i - 1)("LAG") = box5.Text
dt.Rows(i - 1)("FLOW_STEPS_ID") = box6.Text
dt.Rows(i - 1)("UNIQUE_ID") = box7.Text
RowIndex = RowIndex + 1
Next i
dt.Rows.Add(dtCurrentRow)
ViewState("CurrentTable") = dt
dgv.DataSource = dt
dgv.DataBind()
Session("DS") = dt
End If
Else
Response.Write("ViewState is nothing")
End If
Nigam Patel 2-Jan-12 0:10am    
your question is not clear

CommandName Value

"Delete" - Deletes the current record. Raises the RowDeleting and RowDeleted events.



SEE ALSO:

Dynamically removing rows from a grid view[^]
 
Share this answer
 
v2
if you are using DataTable as DataSource for a GridView then try this..

Delete a row in DataTable First Then Bind the gridView again.

C#
DataTable dt = (DataTable)(GridView1.DataSource);
dt.Rows.RemoveAt(GridView1.Rows.Count - 1);
dt.AcceptChanges();
GridView1.DataSource = dt;
GridView1.DataBind();


this code will delete the new row added at the last of a GirdView. If you want to delete row of any other index then replace GridView1.Rows.Count - 1 with your index value.

hope it helps..
 
Share this answer
 
C#
Dim RowIndex As Integer = gvRow.RowIndex
        If ViewState("CurrentTable") IsNot Nothing Then
            dt = DirectCast(ViewState("CurrentTable"), DataTable)
            If dt.Rows.Count > 1 Then
                If gvRow.RowIndex < dt.Rows.Count - 1 Then
                    'Remove the Selected Row data
                    dt.Rows.Remove(dt.Rows(RowIndex))
                End If
            End If
            'store the current data in ViewState for future reference                
            ViewState("CurrentTable") = dt
            Session("DS") = dt
            'rebind the GridView for the updated data
            dgv.DataSource = dt
            dgv.DataBind()
            For i = 0 To dgv.Rows.Count - 1
                dgv.Rows(i).Cells(0).Text = i + 1
            Next i
        End If
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900