まずその中の画像ボタンでテンプレート列を追加:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Srl"
DataSourceID="EntityDataSource1" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/Left.gif" CommandName="Add" />
</ItemTemplate>
</asp:TemplateField>
<%--Other columns--%>
</Columns>
</asp:GridView>
そしてGridView1_RowDataBoundイベントハンドラでは、インデックスを行にボタンCommandArgumentを設定:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton button = (ImageButton)e.Row.FindControl("ImageButton1");
button.CommandArgument = e.Row.RowIndex.ToString();
}
}
最後GridView1_RowCommandイベントにハンドラはImageButton imageUrlとCommandNameを切り替え、行の追加と削除に必要な操作を行います。
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow selectedRow = GridView1.Rows[index];
ImageButton button = (ImageButton)e.CommandSource;
switch (e.CommandName)
{
case "Add":
// Use selectedRow to add your rows
button.ImageUrl = "~/images/down.gif";
button.CommandName = "Remove";
break;
case "Remove":
// Use selectedRow to remove your rows
button.ImageUrl = "~/images/left.gif";
button.CommandName = "Add";
break;
}
}