I am trying to delete Rows from my Gridview
using a CommandName
but its not working. I am using get RowIndex
to do this.
I do not get any errors, it just doesn't do anything when I click on the ImageButton
.
Here is my code:
<asp:GridView ID="GridView1" runat="server" Width="538px" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical" onselectedindexchanged="DropDownList5_SelectedIndexChanged" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt" Font-Size="Small" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" Height="16px" ImageUrl="~/images/delete.png" Width="16px" CommandName="DeleteRow" />
</ItemTemplate>
<HeaderStyle Width="30px" />
<ItemStyle Height="10px" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>
Here is cs code:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (Page.IsPostBack)
{
if (e.CommandName.Equals("DeleteRow"))
{
GridViewRow oItem = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
int RowIndex = oItem.RowIndex;
GridView1.DeleteRow(RowIndex);
DataBind();
}
}
}