C# 如何从gridview中的相应按钮单击中获取特定行值

C# 如何从gridview中的相应按钮单击中获取特定行值,c#,asp.net,gridview,C#,Asp.net,Gridview,我声明了一个gridview,其中一些字段是从数据库绑定的。我添加了一个项目模板,其中有一个文本框和按钮。现在,当我单击按钮时,我想捕捉一列的值和对应行的文本框。我怎样才能完成它 我的aspx gridview标记: <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="FOODITEM_ID" DataSourceID="SqlDataSource2" EnableMod

我声明了一个gridview,其中一些字段是从数据库绑定的。我添加了一个项目模板,其中有一个文本框和按钮。现在,当我单击按钮时,我想捕捉一列的值和对应行的文本框。我怎样才能完成它

我的aspx gridview标记:

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="FOODITEM_ID" DataSourceID="SqlDataSource2" EnableModelValidation="True" OnSelectedIndexChanged="GridView2_SelectedIndexChanged"
    OnRowCommand="GridView2_OnRowCommand">
    <Columns>
        <asp:BoundField DataField="FOOD_ITEM_NAME" HeaderText="FOOD_ITEM_NAME" SortExpression="FOOD_ITEM_NAME" />
        <asp:BoundField DataField="FOODITEM_ID" HeaderText="FOODITEM_ID" ReadOnly="True" SortExpression="FOODITEM_ID" />
        <asp:BoundField DataField="PRICE" HeaderText="PRICE" SortExpression="PRICE" />
        <asp:BoundField DataField="DAY_AVAILABLE" HeaderText="DAY_AVAILABLE" SortExpression="DAY_AVAILABLE" />
        <asp:BoundField DataField="TIME_AVAILABLE" HeaderText="TIME_AVAILABLE" SortExpression="TIME_AVAILABLE" />
        <asp:BoundField DataField="DISCOUNT_PERCENTAGE" HeaderText="DISCOUNT_PERCENTAGE" SortExpression="DISCOUNT_PERCENTAGE" />
        <asp:BoundField DataField="START_DATE" HeaderText="START_DATE" SortExpression="START_DATE" />
        <asp:BoundField DataField="DEADLINE" HeaderText="DEADLINE" SortExpression="DEADLINE" />
        <asp:BoundField DataField="Rating" HeaderText="Rating" SortExpression="Rating" />
        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                 <asp:Button ID="Button1" runat="server" CausesValidation="false" CommandName="Add"
                    Text="Add" CommandArgument='<%# ((GridViewRow) Container).RowIndex  %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView> 


现在,当我按下按钮时,如何获取相应行的文本框和其他字段的值。

Flowwing代码应该可以工作:

void GridView2_OnRowCommand(Object sender, GridViewCommandEventArgs e)
 {
    if(e.CommandName=="Add")
    {
      int index = Convert.ToInt32(e.CommandArgument);
      GridViewRow selectedRow = CustomersGridView.Rows[index];
      string otherFieldText = row.Cells[0].Text; // put othe fields' index
      TextBox txt = (TextBox)row.FindControl("TextBox1"); // Textbox
      string txtVal = txt.text; // Textbox value
    }
}

你遗漏了一些东西,所以我决定给你我的完整样品

第一个aspx代码。请注意,我使用命令名(编辑、删除、取消、更新)来执行相应的命令

其次,我绑定了“ItemsGridView\u RowUpdate”以捕获新值(这就是您要寻找的)

快乐编码

                <asp:GridView ID="ItemsGridView" runat="server" DataKeyNames="ItemId,FieldName" PageSize="1000"
                    AutoGenerateColumns="False" AllowPaging="False" AllowSorting="False" SkinID="DefaultGridView"
                    OnRowEditing="ItemsGridView_RowEditing"
                    OnRowCancelingEdit="ItemsGridView_RowCancelingEdit"
                    OnRowDeleting="ItemsGridView_RowDeleting"
                    OnRowUpdating="ItemsGridView_RowUpdating"
                     >
                    <Columns>
                        <asp:TemplateField ItemStyle-CssClass="TemplateFieldFourColumns">
                            <ItemTemplate>
                                <asp:ImageButton ID="ibEdit" runat="server" ToolTip="<% $resources:AppResource,Edit %>" SkinID="EditPage" CommandName="Edit" />
                                <asp:ImageButton ID="ibDelete" runat="server" ToolTip="<% $resources:AppResource,Delete %>" SkinID="DeletePage" CommandName="Delete" OnClientClick='<%#this.GetDeleteConfirmation() %>' />
                                <asp:ImageButton ID="ibUp" runat="server" ToolTip="<% $resources:AppResource,Up %>" SkinID="UpPage" OnClick="ibUp_Click" CommandArgument='<%#Eval("ItemId")%>' />
                                <asp:ImageButton ID="ibDown" runat="server" ToolTip="<% $resources:AppResource,Down %>" SkinID="DownPage" OnClick="ibDown_Click" CommandArgument='<%#Eval("ItemId")%>' />
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:ImageButton ID="ibCancel" runat="server" ToolTip="<% $resources:AppResource,Cancel %>" SkinID="Cancel" CommandName="Cancel" />
                                <asp:ImageButton ID="ibUpdate" runat="server" ToolTip="<% $resources:AppResource,Save %>" SkinID="Save" CommandName="Update" />
                            </EditItemTemplate>
                        </asp:TemplateField>

                        <asp:TemplateField  HeaderText="<% $resources:AppResource,Format %>">
                            <ItemTemplate>
                                <asp:Label ID="lblFormat" runat="server" Text='<%#Eval("Format")%>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtFormat" runat="server" Text='<%#Bind("Format")%>' Width="50"></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>

                    </Columns>
                </asp:GridView>
    protected void ItemsGridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        ItemsGridView.EditIndex = e.NewEditIndex;
        ItemsGridView.DataSource = this.genericForm.FormItems; //TODO: get your data source
        ItemsGridView.DataBind();
    }

    protected void ItemsGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        ItemsGridView.EditIndex = -1;
        ItemsGridView.DataSource = this.genericForm.FormItems; //TODO: get your data source
        ItemsGridView.DataBind();
    }

    protected void ItemsGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //delete...
        try
        {
            Guid itemId = (Guid)e.Keys[0]; //key
            //TODO: delete your item and bind new data
        }
        catch (Exception ex)
        {
            this.MessageBoxError(ex);
        }
    }


   protected void ItemsGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        //update...
        try
        {   
        //get your key and read new values for update....
            Guid itemId = (Guid)e.Keys[0];
            string fieldName = (string)e.Keys[1];
            string Format = (string)e.NewValues["Format"];

          //TODO: make an update and rebind your data
        }
        catch (Exception ex)
        {
            this.MessageBoxError(ex);
        }
    }