C# 如何在网格视图中显示特定列的标签消息?

C# 如何在网格视图中显示特定列的标签消息?,c#,asp.net,aspxgridview,C#,Asp.net,Aspxgridview,这是我在网格视图下的一小段.aspx代码: <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" HorizontalAlign="Center" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowDataBound="GridView1_RowDataBound"> <Columns> <asp:Temp

这是我在网格视图下的一小段.aspx代码:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" HorizontalAlign="Center" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Quantity" >
<ItemTemplate>
<asp:TextBox ID ="TextBox4" runat="server" Width="60px" DataField="Product_Quantity" Text='<%#Eval("Product_Quantity")%>' />
<asp:Button ID ="Button12" runat="server" OnClick="Quantity_Update_Click" CommandArgument="Button12" CommandName="Update"  Text="Update" />
<asp:Label ID="Label6" runat="server"></asp:Label>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ErrorMessage="Numbers only" ControlToValidate="TextBox4" ValidationExpression="^[0-9]*$"></asp:RegularExpressionValidator>      
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
现在问题在Label6下,它显示一条红线,并提到:

The name 'Label6' does not exist in the current context.
但是我已经在.aspx代码中添加了Label6。 我不确定我犯的错误在哪里


如果提供了推荐的语法解决方案,这将很有帮助。

就像您通过行索引在网格中找到Textbox控件一样,您还必须找到标签

请尝试此代码。 当然,我无法测试sql连接

    protected void Quantity_Update_Click(object sender, EventArgs e)
    {
        GridViewRow gvr = (GridViewRow)(sender as Control).Parent.Parent;
        int index = gvr.RowIndex;
        TextBox box4 = (TextBox)GridView1.Rows[index].FindControl("TextBox4");
        Label Label6 = (Label)GridView1.Rows[index].FindControl("Label6");
        int Quantity;
        bool qty = int.TryParse(box4.Text, out Quantity);

        Button btn = (Button)sender;
        GridViewRow row = (GridViewRow)btn.NamingContainer;
        string ProductNo = row.Cells[0].Text;

        if (Quantity > 0)
        {
            string CS;
            CS = "data source=LAPTOP-ODS96MIK\\MSSQL2014; database = Grocery_Demo; integrated security=SSPI";
            SqlConnection con = new SqlConnection(CS);
            SqlCommand cmd = new SqlCommand("UpdateProductQuantity", con);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            con.Open();
            cmd.Parameters.AddWithValue("@ProductQuantity", Quantity);
            cmd.Parameters.AddWithValue("@ProductNo", ProductNo);
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox("Quantity has been updated");
            DisplayProducts();
        }

        else if (Quantity == 0 || qty == false)
        {
            Label6.Text = "Please add at least one quantity";
            DisplayProducts();
        }

    }

请注意,我已将GridView1.Rows[index]更改为不包含.Cells[]部分,因为findcontrol将在整行中查找控件,以下内容应满足您的需要(假设所有单元格都包含整数):


根据需要调整代码

我想你必须在datagrid的上下文中找到Label6,因为它在itemtemplate中。你能发布整个gridview标记吗?我已经添加了gridview标记。。。您可以再次引用已编辑的.aspx代码:)好的,那么DisplayProducts是做什么的?类似这样的事情?我使用了你推荐的逻辑。。。现在,在编译过程中,C代码中没有错误。。。。现在,如果我在数量框中添加任意随机数并单击更新按钮,它将得到更新并显示消息框“数量已更新”。。。但有一个问题。。。如果我在“数量”框中输入值0,然后单击“更新”按钮,该值将被重定向回其以前的非零值,但它不会显示标签消息。。。您可以在我的.aspx代码中引用和,看看我是否在那里犯了错误。您是否在页面加载中使用if(!Page.IsPostBack)?请给我看你的页面加载事件代码现在你可以在我的帖子中查看我的页面加载事件代码。我编辑过:)
    protected void Quantity_Update_Click(object sender, EventArgs e)
    {
        GridViewRow gvr = (GridViewRow)(sender as Control).Parent.Parent;
        int index = gvr.RowIndex;
        TextBox box4 = (TextBox)GridView1.Rows[index].FindControl("TextBox4");
        Label Label6 = (Label)GridView1.Rows[index].FindControl("Label6");
        int Quantity;
        bool qty = int.TryParse(box4.Text, out Quantity);

        Button btn = (Button)sender;
        GridViewRow row = (GridViewRow)btn.NamingContainer;
        string ProductNo = row.Cells[0].Text;

        if (Quantity > 0)
        {
            string CS;
            CS = "data source=LAPTOP-ODS96MIK\\MSSQL2014; database = Grocery_Demo; integrated security=SSPI";
            SqlConnection con = new SqlConnection(CS);
            SqlCommand cmd = new SqlCommand("UpdateProductQuantity", con);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            con.Open();
            cmd.Parameters.AddWithValue("@ProductQuantity", Quantity);
            cmd.Parameters.AddWithValue("@ProductNo", ProductNo);
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox("Quantity has been updated");
            DisplayProducts();
        }

        else if (Quantity == 0 || qty == false)
        {
            Label6.Text = "Please add at least one quantity";
            DisplayProducts();
        }

    }
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.DataItem != null)
        {
            // Set the capacity label text
            Label1.Text = e.Row.Cells[0].Text;


            // Calc the sum of all of the row values
            int sum = 0;
            foreach(TableCell c in e.Row.Cells)
            {
                  sum+= Int32.Parse(c.Text);
            }


            // Set the sum label text value
            Label2.Text = sum.ToString();        }
    }