This seems like a repeated question but i’m not able to get my answer.
I have a grid view and I need to delete a particular row, when I click on a button outside the gridview.
protected void btnDelete_Click(object sender, EventArgs e)
{
dtable = (DataTable)Session["data"];
DataRow row = dtable.Rows[DataGV1.SelectedIndex];
dtable.Rows.Remove(row);
DataGV1.DataSource = dtable;
DataGV1.DataBind();
Session["data"] = dtable;
}
The session variable has the previous state of datatable.
protected void DataGV1_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridView _gridView = (GridView)sender;
// Get the selected index
_selectedIndex = int.Parse(e.CommandArgument.ToString());
}
Gridview controls
onselectedindexchanged="DataGV1_SelectedIndexChanged"
OnRowCommand="DataGV1_RowCommand" OnRowDeleting="DataGV1_RowDeleting"
AutoGenerateSelectButton="False" DataKeyNames="Role,Last_name">
<Columns>
<asp:ButtonField DataTextField="last_name" HeaderText="Last_name" CommandName="SingleClick"
SortExpression="last_name" Text="Button" />
<asp:BoundField DataField="role" HeaderText="role" SortExpression="role" />
<asp:BoundField DataField="role" HeaderText="role" HeaderText="Frist_name"
SortExpression="first_name" Text="First_name" />
</Columns>
</asp:GridView>
This doesn’t seem to work.
Can u please tell me where I am going wrong?
If button is outside the
GridViewthen no need to handleRowCommandevent (In fact it is inappropriate).Suggestion:
You have to add a
TemplateFieldcolumn, drop the CheckBox control in ItemTemplate of TemplateField and write code in click handler of button to traverse theGridView.Rowscollection, identify the selected row by reading value ofCheckBoxcontrol and perform deletion action if thatCheckBoxis checked.Demo DataSource (
List<T>)Markup:
Code-behind (Page_Load)