简体   繁体   中英

how to delete a row from gridview

The data is getting propagated into my gridview. But, i am messing it up when i need to delete the data.

I have a delete button within my gridview as template field.

<asp:TemplateField HeaderText="Delete" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
                                          <asp:Button ID="btnDelete"  runat="server" OnClick="btnDelete_Click" OnClientClick="return confirm('Are you sure you want to delete this Inventory SN?');" />
                                      </ItemTemplate>
                                  </asp:TemplateField>

And on the click event i have

 protected void btnDelete_Click(object sender, EventArgs e)
    {
        try
        {
            Button btn = (Button)sender;
            GridViewRow row = (GridViewRow)btn.NamingContainer;
            if (row != null)
            { 
            //what shall i write next??
            }

        }
        catch (Exception ex)
        {

            throw ex;
        }
    }

The data which is getting populated is not coming from one table. So, i just need to make it vanish.

Any kind of help will be gracefully accepted.

Why not deleting your selected row?

When you click your Delete button you also select the row.

Just call yourgridview.SelectedItem to get it. Use yourgridview.Remove(yourgridview.SelectedItem); to remove it

if (dataGrid.SelectedItem != null)
{
    dataGrid.Items.Remove(dataGrid.SelectedItem);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM