Click here to Skip to main content
16,004,647 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I want to delete row in the gridview.

But i am not able to pick the id from the table on which base i am delete the row from gridview

I am using code which is show here for select the cell value form the selected row.
there some way which i am try in this give below
1) TextBox1.Text = GridView1.Rows[e.RowIndex].Cells(0).Text;
2)string nam = ((Label)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text;

3)int a = (int)GridView1.DataKeys[e.RowIndex].Value;

4) GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lbl = (Label)row.FindControl("lblid");

But i am not able to delete the any one guide me or provide me any reference code.


Thanks & Regards
Deepak
Posted
Updated 15-Jun-11 22:19pm
v3

try this:

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
       
        string textBoxID = ((TextBox)row.FindControl("IDTextBox")).Text;
        SqlTransaction st = null;
        try
        {
            conn.Open();
            st = conn.BeginTransaction();
            SqlCommand command1= new SqlCommand("Delete from tablename where MyID ='" + Convert.ToInt32(textBoxID) + "'", conn,st);
                   
            
            
            int num = command1.ExecuteNonQuery();
            
            
            if (num != 0)
            {
                
               
                   
                    st.Commit();
                    BindGrid();
               
                
            }
        }
        catch(Exception)
        {
            if (st == null)
                st.Rollback();
            
          
        }
        finally
        {
        conn.Close();
        }
        
    }

and GridView should look like this:

<pre><asp:GridView ID="GridView1"  runat="server"  AutoGenerateColumns="false" Font-Bold="True"
                Font-Size="Large" ShowFooter="true" AutoGenerateDeleteButton="True"   OnRowDeleting="GridView1_RowDeleting"  

                <Columns>

                 <asp:TemplateField HeaderText="Finding ID">
                        <ItemTemplate>
                            <asp:TextBox ID="IDTextBox" Width="198px"  runat="server" BackColor="White" BorderColor="White"
                                Font-Bold="True" Font-Size="Large"  Height="23px"  ForeColor="Black"></asp:TextBox>
                        </ItemTemplate>
                     <HeaderStyle ForeColor="Blue" />
                    </asp:TemplateField>


 
Share this answer
 
v2
This[^] may help.
 
Share this answer
 
thanks dear friend i will try now
 
Share this answer
 
karan thanks your code is working in my application thankyou
 
Share this answer
 
Comments
karan joshua 16-Jun-11 4:45am    
your welcome..
  Protected Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If Page.IsPostBack Then
            lbId = CType(GridView1.FooterRow.FindControl("lblEmpId"), Label)
            tb1 = CType(GridView1.FooterRow.FindControl("txtFName"), TextBox)
            tb2 = CType(GridView1.FooterRow.FindControl("txtLName"), TextBox)
            chk = CType(GridView1.FooterRow.FindControl("chkIsVendor"), CheckBox)

        End If
    End Sub
 


Protected Sub GridView1_RowDeleting(sender As Object, e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
        'Dim EC As Integer = GridView1.Rows(e.RowIndex).Cells(0).Text
        Dim cmd As New SqlCommand("DELETE from mstEmployee where EmpCode=" &                 lbId.Text & " ", con)
        If con.State = ConnectionState.Closed Then
            con.Open()
        End If
        cmd.ExecuteNonQuery()
        con.Close()
        DisplayData()
End Sub
 
Share this answer
 

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