如何在网格视图中大容量编辑单个列

本文关键字:大容量 编辑 单个列 视图 网格 | 更新日期: 2023-09-27 18:30:12

我正在开发ASP.net应用程序(c#)。在它中,我有一个网格视图,其中的列显示为ItemTemplates。我想编辑一列的值。

我在网格视图下面有一个更新按钮。因此,当我单击update时,新值应该会更新到数据库中。

我正在使用列表集合将数据绑定到网格视图。

我不知道如何做到这一点。

如何在网格视图中大容量编辑单个列

假设列中有TextBox,用户将在其中更新新值。您可以通过这种方式在网格视图行中进行迭代

foreach(GridViewRow row in GridView1.Rows) {
    if(row.RowType == DataControlRowType.DataRow) {
        TextBox txtbx= row.FindControl("txtbx") as TextBox;
        //using the txtbx you can get the new values and update then to the db
    }
}
foreach(GridViewRow row in GridView1.Rows) 
   {
     if(row.RowType == DataControlRowType.DataRow) 
       {        
            String str = ((txtbx)(row.Cells[2].Controls[0])).Text;
        }
    }

检查textbox的单元格值。

如果您使用的是GridView,您是否也使用了一个支持对象(某种集合)来绑定到网格行?这似乎很可能是因为您使用了ItemTemplates来显示它们。

在这种情况下,您是否可以访问此备份集合中要更改的项目?

编辑

Comment说您正在使用列表来绑定网格,那么为什么不对列表项进行更改并将其发送回数据库呢?

foreach(var listItem in MyList)
{
    listItem.ThingToChange = newValueForThisProperty;
}
SubmitChangesToDatabase(MyList);

我创建了一个完整的工作演示并测试了它:

GridBulkEdit.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridBulkEdit.aspx.cs" Inherits="GridBulkEdit" EnableViewState="true" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server"> 
       <% /*1. Enter the new value for the column to be updated in the 'NewValue' textbox
            2. Check the 'UpdateThisRow' checkbox in the gridview for all rows that need to be updated.            
            3. Click the 'Update' button */%>
       New Value: <asp:TextBox runat="server" ID="NewValue_TextBox"></asp:TextBox>        
        <asp:Button runat="server" ID="Update_Button" Text="Update Checked Rows With New Value" OnClick="Update_Button_Click" />
      <% /* Assuming grid is bound to datasource with 2 columns: 
            1. 'PrimaryKeyField' - the primary key for each row 
            2. 'CurrentValue' - the value that we want to batch update */%>
        <asp:GridView runat="server" ID="grid" AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:HiddenField runat="server" ID="PrmaryKeyForThisRow_HiddenField" Value='<%# Bind("PrimaryKeyField") %>' />    
                        <asp:CheckBox runat="server" ID="UpdateThisRow_CheckBox"  Checked="false" ToolTip="Check this box to update this row"/>                        
                        <asp:Label runat="server" ID="CurrentValue_Label"  Text='<%# Bind("CurrentValue") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:Label runat="server" ID="TestOutput_Label"></asp:Label>        
    </form>
</body>
</html>

GridBulkEdit.aspx。cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using System.Data;
public partial class GridBulkEdit : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        //Create a data table to bind the grid
        DataTable DT = new DataTable();
        DT.Columns.Add("PrimaryKeyField");
        DT.Columns.Add("CurrentValue");
        DataRow DR1 = DT.NewRow();
        DR1["PrimaryKeyField"] = 1;
        DR1["CurrentValue"] = "value one";
        DT.Rows.Add(DR1);
        DataRow DR2 = DT.NewRow();
        DR2["PrimaryKeyField"] = 2;
        DR2["CurrentValue"] = "value two";
        DT.Rows.Add(DR2);
        DataRow DR3 = DT.NewRow();
        DR3["PrimaryKeyField"] = 3;
        DR3["CurrentValue"] = "value three";
        DT.Rows.Add(DR3);
        grid.DataSource = DT;
        grid.DataBind();
    }
    protected void Update_Button_Click(object sender, EventArgs e)
    {
        TestOutput_Label.Text = "";
        for (int i = 0; i < grid.Rows.Count; i++)
        {
            HiddenField PrmaryKeyForThisRow_HiddenField = grid.Rows[i].FindControl("PrmaryKeyForThisRow_HiddenField") as HiddenField;
            CheckBox UpdateThisRow_CheckBox = grid.Rows[i].FindControl("UpdateThisRow_CheckBox") as CheckBox;
            if (UpdateThisRow_CheckBox.Checked)
            {
                UpdateRow(PrmaryKeyForThisRow_HiddenField.Value);
            }
        }       
    }
    private void UpdateRow(object PrimaryKey)
    {
        object NewValue = NewValue_TextBox.Text;
        //Execute SQL query to update row with the passed PrimaryKey with the NewValue
        TestOutput_Label.Text += "<br/> Update row whose PrimaryKey is: " + PrimaryKey + " with new value: " + NewValue;
    }
}