0

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

我在网格视图下方有一个更新按钮。所以当我点击更新时,新值应该更新到数据库中。

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

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

4

4 回答 4

3

假设您在列中有 TextBox,用户将在其中更新新值。您可以以这种方式遍历 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
    }
}
于 2013-09-04T11:58:45.010 回答
1

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

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;
    }
}
于 2013-09-04T13:28:31.470 回答
1

如果您使用的是 GridView,您是否还使用支持对象(某种集合)来绑定到网格行 - 因为您正在使用 ItemTemplates 来显示它们,所以这似乎很可能。

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

编辑

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

foreach(var listItem in MyList)
{
    listItem.ThingToChange = newValueForThisProperty;
}
SubmitChangesToDatabase(MyList);
于 2013-09-04T13:32:11.840 回答
1
foreach(GridViewRow row in GridView1.Rows) 
   {
     if(row.RowType == DataControlRowType.DataRow) 
       {        

            String str = ((txtbx)(row.Cells[2].Controls[0])).Text;

        }
    }

检查您的单元格值textbox

于 2013-09-04T13:12:48.680 回答