Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete row in gridview using rowdeleting event?

Tags:

c#

asp.net

This is my .cs code :

protected void Gridview1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
 Gridview1.DeleteRow(e.RowIndex);
 Gridview1.DataBind();
}

and this is markup,

<asp:gridview ID="Gridview1" runat="server" ShowFooter="true" 
                                                AutoGenerateColumns="false" OnRowDeleting="Gridview1_RowDeleting">
                    <Columns>
                    <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
                    <asp:TemplateField HeaderText="Column Name">
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>

                 <%-- <asp:TemplateField HeaderText="Header 2">
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>--%>
                    <asp:TemplateField HeaderText="Data Type">
                        <ItemTemplate>
                              <asp:DropDownList ID="ddldatatype" runat="server">
                              <asp:ListItem>varchar</asp:ListItem>
                              <asp:ListItem>int</asp:ListItem>
                              <asp:ListItem>numeric</asp:ListItem>
                              <asp:ListItem>uniqueidentifier</asp:ListItem>
                              <asp:ListItem>char</asp:ListItem>
                              </asp:DropDownList>
                        </ItemTemplate>
                        <FooterStyle HorizontalAlign="Right" />
                        <FooterTemplate>
                        <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click"/>
                         <asp:Button ID="ButtonDel" runat="server" Text="Delete Row" OnClick="ButtonDel_Click" />
                         <input type="hidden" runat="server" value="0" id="hiddencount" />
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:LinkButton ID="lnkdelete" runat="server" CommandName="Delete" >Delete</asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                    </Columns>
            </asp:gridview>

Please sugegest me. I have done this much.. but still not deleting row...

 protected void Gridview1_RowDeleting(object sender, GridViewDeleteEventArgs e)
         {


             //Gridview1.DeleteRow((int)Gridview1.DataKeys[e.RowIndex].Value);
             //Gridview1.DeleteRow(e.RowIndex);
             //Gridview1.DataBind();
             foreach(DataRow dr in dt.Rows)
             {
                 dt.Rows.Remove(dr);
                 dt.Rows[e.RowIndex].Delete();

             }
             Gridview1.DeleteRow(e.RowIndex);
            // dt = (DataTable)Gridview1.DataSource;
             Gridview1.DataSource = dt;
             Gridview1.DataBind();
       }
like image 625
Mahendra Avatar asked Nov 27 '22 12:11

Mahendra


2 Answers

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
    SqlCommand cmd = new SqlCommand("Delete From userTable (userName,age,birthPLace)");
    GridView1.DataBind();
}
like image 59
Devesh Kabdwal Avatar answered Dec 05 '22 02:12

Devesh Kabdwal


Make sure to create a static DataTable object and then use the following code:

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    dt.Rows.RemoveAt(e.RowIndex); 
    GridView1.DataSource = dt;
    GridView1.DataBind();
}
like image 33
tejas Avatar answered Dec 05 '22 01:12

tejas