You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ASP.NET C#中点击单个编辑按钮使RadGrid所有行单元格可编辑

实现RadGrid一键批量编辑所有行单元格

Got it, let's solve this problem where you want to switch all rows (including nested detail tables) in your RadGrid to editable mode with a single button click—no per-row edit buttons needed. Here's a step-by-step solution tailored to your existing code:

Step 1: Update RadGrid Configuration

First, we need to adjust your RadGrid settings to enable multi-row in-place editing, and update each template column to include both display (ItemTemplate) and editable (EditItemTemplate) controls.

Modified RadGrid Snippet

I'll focus on updating the key parts—you can integrate these changes into your existing grid:

<radG:RadGrid ID="grdAction" runat="server" 
    AllowPaging="True" Width="100%" 
    EnableAJAXLoadingTemplate="true" 
    AllowSorting="True" ShowHeader="false" 
    OnNeedDataSource="grd_View_NeedDataSource" 
    OnDetailTableDataBind="grd_View_DetailTableDataBind" 
    PageSize="11" 
    MasterTableView-GroupsDefaultExpanded="true" 
    OnPreRender="grd_View_PreRender" 
    AllowMultiRowSelection="true"
    AllowMultiRowEdit="true" <!-- Enable multi-row editing -->
    Skin="Glassy">
    <MasterTableView AutoGenerateColumns="False" 
        HierarchyDefaultExpanded="true"
        EditMode="InPlace"> <!-- Set in-place editing mode -->
        <!-- ... existing MasterTableView columns and settings ... -->
        <DetailTables>
            <radG:GridTableView Width="100%" runat="server" 
                AutoGenerateColumns="False" 
                ShowHeader="True" 
                HierarchyLoadMode="ServerBind"
                EditMode="InPlace" <!-- Apply to detail tables too -->
                AllowMultiRowEdit="true"> <!-- Allow multi-row edit in details -->
                <Columns>
                    <!-- Example: Updated Template Column with EditItemTemplate -->
                    <radG:GridTemplateColumn HeaderText="Header1" SortExpression="Header1">
                        <ItemTemplate>
                            <asp:Label ID="lblItem1" runat="server" 
                                Text='<%# Eval("RootCause").ToString()+ "&nbsp" %>'>
                            </asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <!-- Use TextBox for text-based data -->
                            <asp:TextBox ID="txtRootCause" runat="server" 
                                Text='<%# Eval("RootCause") %>'
                                Width="95%">
                            </asp:TextBox>
                        </EditItemTemplate>
                        <ItemStyle VerticalAlign="Top" />
                        <HeaderStyle Width="22%" Wrap="True" />
                    </radG:GridTemplateColumn>

                    <!-- Another example: Boolean/Yes-No field with CheckBox -->
                    <radG:GridTemplateColumn HeaderText="header2" SortExpression="" ItemStyle-HorizontalAlign="Center">
                        <ItemTemplate>
                            <asp:Label ID="lblAgree" runat="server" 
                                CssClass='<%# Eval("lblitem2").ToString()=="N"?"AgreeStyles": ""%>' 
                                Text='<%# Eval("lblitem2").ToString()+ "&nbsp" %>'>
                            </asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:CheckBox ID="chkAgree" runat="server" 
                                Checked='<%# Eval("lblitem2").ToString() == "Y" %>' />
                        </EditItemTemplate>
                        <ItemStyle VerticalAlign="Top" />
                        <HeaderStyle Width="3%" Wrap="True" />
                    </radG:GridTemplateColumn>

                    <!-- Multiline text field with TextBox (TextMode="MultiLine") -->
                    <radG:GridTemplateColumn HeaderText="header3" SortExpression="What">
                        <ItemTemplate>
                            <asp:Label ID="lblitem3" runat="server" 
                                Text='<%# UIHelper.TextArea2HTML(Eval("lblitem3").ToString())+ "&nbsp" %>'>
                            </asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtItem3" runat="server" 
                                Text='<%# Eval("lblitem3") %>'
                                TextMode="MultiLine"
                                Rows="3"
                                Width="95%">
                            </asp:TextBox>
                        </EditItemTemplate>
                        <ItemStyle VerticalAlign="Top" />
                        <HeaderStyle Width="15%" Wrap="True" />
                    </radG:GridTemplateColumn>
                </Columns>
                <!-- ... existing DetailTable settings ... -->
            </radG:GridTableView>
        </DetailTables>
        <!-- ... remaining MasterTableView settings ... -->
    </MasterTableView>
    <!-- ... existing RadGrid settings ... -->
</radG:RadGrid>

Step 2: Implement the btnEdit_Click Event

In your code-behind, write logic to switch every row in the main table and all detail tables to edit mode. We'll use a recursive method to handle nested detail tables:

protected void btnEdit_Click(object sender, ImageClickEventArgs e)
{
    // Switch main table rows to edit mode
    foreach (GridDataItem item in grdAction.MasterTableView.Items)
    {
        item.Edit = true;
        // Expand detail tables to ensure their rows are accessible
        item.Expanded = true;
    }

    // Recursively switch detail table rows to edit mode
    SwitchDetailTablesToEditMode(grdAction.MasterTableView.DetailTables);

    // Rebind the grid to apply changes
    grdAction.Rebind();
}

private void SwitchDetailTablesToEditMode(GridTableViewCollection detailTables)
{
    foreach (GridTableView tableView in detailTables)
    {
        foreach (GridDataItem item in tableView.Items)
        {
            item.Edit = true;
            // If there are nested detail tables, recurse further
            if (item.HasChildItems)
            {
                SwitchDetailTablesToEditMode(item.ChildItem.NestedTableViews);
            }
        }
    }
}

Step 3: (Optional) Handle Saving Edits

If you need to save the changes, add a save button and write code to iterate through all editable rows and update your data source:

protected void btnSave_Click(object sender, ImageClickEventArgs e)
{
    // Process main table rows
    foreach (GridDataItem item in grdAction.MasterTableView.Items)
    {
        if (item.IsInEditMode)
        {
            // Get edited values from controls
            string rootCause = ((TextBox)item.FindControl("txtRootCause")).Text;
            bool isAgreed = ((CheckBox)item.FindControl("chkAgree")).Checked;
            string item3 = ((TextBox)item.FindControl("txtItem3")).Text;

            // Update your database with these values (use your data access logic)
            // Example: UpdateRecord(item.GetDataKeyValue("YourPrimaryKey").ToString(), rootCause, isAgreed, item3);
        }
    }

    // Process detail tables recursively
    SaveDetailTableEdits(grdAction.MasterTableView.DetailTables);

    // Switch back to display mode
    foreach (GridDataItem item in grdAction.MasterTableView.Items)
    {
        item.Edit = false;
    }
    SwitchDetailTablesToDisplayMode(grdAction.MasterTableView.DetailTables);

    grdAction.Rebind();
}

private void SaveDetailTableEdits(GridTableViewCollection detailTables)
{
    foreach (GridTableView tableView in detailTables)
    {
        foreach (GridDataItem item in tableView.Items)
        {
            if (item.IsInEditMode)
            {
                // Retrieve values and update database
                string rootCause = ((TextBox)item.FindControl("txtRootCause")).Text;
                // ... other fields ...
            }

            if (item.HasChildItems)
            {
                SaveDetailTableEdits(item.ChildItem.NestedTableViews);
            }
        }
    }
}

private void SwitchDetailTablesToDisplayMode(GridTableViewCollection detailTables)
{
    foreach (GridTableView tableView in detailTables)
    {
        foreach (GridDataItem item in tableView.Items)
        {
            item.Edit = false;
            if (item.HasChildItems)
            {
                SwitchDetailTablesToDisplayMode(item.ChildItem.NestedTableViews);
            }
        }
    }
}

Key Notes

  • EditItemTemplate: Make sure every column you want editable has an EditItemTemplate with the appropriate control (TextBox, CheckBox, DropDownList, etc.) matching the data type.
  • Recursive Logic: Since your grid has nested detail tables, we use recursion to ensure all levels are switched to edit mode.
  • Rebind: Always call grdAction.Rebind() after changing row edit states to refresh the grid UI.

内容的提问来源于stack exchange,提问作者Sadanand Sonar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 23:27:40