C# 如何在栅格视图中批量编辑单个列

C# 如何在栅格视图中批量编辑单个列,c#,asp.net,gridview,C#,Asp.net,Gridview,我正在开发一个ASP.net应用程序(c#)。在其中,我有一个网格视图,其中列显示为ItemTemplates。我想编辑一列的值 我在网格视图下面有一个更新按钮。因此,当我单击update时,新值应该更新到数据库中 我使用列表集合将数据绑定到网格视图 我不知道如何做到这一点。假设在列中有文本框,用户将在其中更新新值。您可以通过这种方式迭代gridview行 foreach(GridViewRow row in GridView1.Rows) { if(row.RowType == Dat

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

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

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


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

假设在列中有文本框,用户将在其中更新新值。您可以通过这种方式迭代gridview行

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
    }
}

假设在列中有文本框,用户将在其中更新新值。您可以通过这种方式迭代gridview行

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
    }
}
检查
文本框的单元格值


检查
文本框的单元格值

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

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;
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web.UI.WebControl;
使用系统数据;
公共部分类GridBulkEdit:System.Web.UI.Page
{
受保护的无效页\u PreInit(对象发送方,事件参数e)
{
//创建数据表以绑定网格
DataTable DT=新的DataTable();
DT.Columns.Add(“PrimaryKeyField”);
DT.列。添加(“当前值”);
DataRow DR1=DT.NewRow();
DR1[“PrimaryKeyField”]=1;
DR1[“当前值”]=“值一”;
DT.行。添加(DR1);
DataRow DR2=DT.NewRow();
DR2[“PrimaryKeyField”]=2;
DR2[“当前值”]=“值二”;
DT.行。添加(DR2);
DataRow DR3=DT.NewRow();
DR3[“PrimaryKeyField”]=3;
DR3[“当前值”]=“值三”;
DT.行。添加(DR3);
grid.DataSource=DT;
grid.DataBind();
}
受保护的无效更新按钮单击(对象发送者,事件参数e)
{
TestOutput_Label.Text=“”;
对于(int i=0;i用新值“+NewValue”更新PrimaryKey为“+PrimaryKey+”的行;
}
}

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

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;
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web.UI.WebControl;
使用系统数据;
公共部分类GridBulkEdit:System.Web.UI.Page
{
受保护的无效页\u PreInit(对象发送方,事件参数e)
{
//创建数据表以绑定网格
DataTable DT=新的DataTable();
DT.Columns.Add(“PrimaryKeyField”);
DT.列。添加(“当前值”);
DataRow DR1=DT.NewRow();
DR1[“PrimaryKeyField”]=1;
DR1[“当前值”]=“值一”;
DT.行。添加(DR1);
DataRow DR2=DT.NewRow();
DR2[“PrimaryKeyField”]=2;
DR2[“当前值”]=“值二”;
DT.行。添加(DR2);
DataRow DR3=DT.NewRow();
DR3[“PrimaryKeyField”]=3;
DR3[“当前值”]=“值三”;
DT.行。添加(DR3);
grid.DataSource=DT;
grid.DataBind();
}
受保护的无效更新按钮单击(对象发送者,事件参数e)
{
TestOutput_Label.Text=“”;
对于(int i=0;i用新值“+NewValue”更新PrimaryKey为“+PrimaryKey+”的行;
}
}

如果您使用的是GridView,那么您是否也在使用备份对象(某种集合)绑定到网格行?这似乎是因为您使用的是ItemTemplates来显示它们

在这种情况下,您能否访问希望从此支持集合中更改的项目

编辑

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

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

如果您使用的是GridView,那么您是否也使用支持对象(某种集合)绑定到网格行-