user1080533
user1080533

Reputation: 865

GridView - "enter" multiple rows into edit mode using checkboxes from codebehind (on button click)

I need some help again.

I have a GridView with CheckBoxes in first column. If a user checks multiple CheckBoxes and clicks a button I need to display selected rows in edit mode. How do I do that? Any tips?

Some sample code, how I imagine it.

Here is my Gridview MyGV:

<asp:GridView ID="MyGV" runat="server" AutoGenerateColumns="False" DataKeyNames="ID">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox ID="ChkBox" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="ID" HeaderText="ID" />
        <asp:TemplateField HeaderText="Column1">
            <EditItemTemplate>
                <asp:TextBox ID="tbColumn1" runat="server" Text='<%# Bind("column1") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="labColumn1" runat="server" Text='<%# Bind("column1") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Column2">
            <EditItemTemplate>
                <asp:TextBox ID="tbColumn2" runat="server" Text='<%# Bind("column2") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="labColumn2" runat="server" Text='<%# Bind("column2") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
   </Columns>
</asp:GridView>
<asp:Button ID="btnEdit" runat="server" Text="EDIT" OnClick="btnEdit_Click" />

In code behind I bind that GridView to display data from SQL table (no problem there). Now I need it to enter selected rows into edit mode (ONLY selected rows) when I click the EDIT button.

protected void btnEdit_Click(object sender, EventArgs e)
{
    foreach (GridViewRow row in MyGV.Rows)
    {
        CheckBox checkbox = (CheckBox)row.FindControl("ChkBox");
        if (checkbox.Checked)
        {
            // ENTER EDIT MODE - Help needed here!! :)
        }
    }
    BindGridView();
}

Upvotes: 0

Views: 2951

Answers (1)

Michiel van Oosterhout
Michiel van Oosterhout

Reputation: 23084

In a GridView only one row can be in true edit mode at the same time. See question below for a possible workaround:

Put multiple rows of a gridview into edit mode

Upvotes: 1

Related Questions