简体   繁体   English

System.ArgumentOutOfRangeException:'索引超出范围。 必须为非负数,并且小于集合的大小。”

[英]System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection.'

protected void getSUM()
{
    // SQL query that gets total of product sales where category id = 1
    string SqlQuery = @"SELECT Price AS TotalSales 
  FROM STOCK
  WHERE Barcode = '" + TextBox1 + "'";

    // Declare and open a connection to database


    sqlcon.Open();

    // Creates SqlCommand object
    SqlCommand comm = new SqlCommand(SqlQuery, sqlcon);

    // Gets total sales
    decimal TotalSales = Convert.ToDecimal(comm.ExecuteScalar());

    // Close connection
    sqlcon.Close();

    // Adds formatted output to GridView footer
    GridView1.Columns[3].FooterText = String.Format("{0:c}", TotalSales);
}

i just want to add the price at the footer of the gridview. 我只想在gridview的页脚添加价格。 i don't understand what's wrong. 我不明白怎么了。 the error is in this line GridView1.Columns[3].FooterText = String.Format("{0:c}", TotalSales); 该行中的错误是GridView1.Columns [3] .FooterText = String.Format(“ {0:c}”,TotalSales);

You are probably using AutoGenerateColumns="true" in your GridView. 您可能在GridView中使用了AutoGenerateColumns="true" When using this you will only have access to the columns in the RowCreated and RowDataBound events. 使用此方法时,您只能访问RowCreatedRowDataBound事件中的列。 When the grid is done building the column count will be 0. 网格构建完成后,列数将为0。

Better use TemplateFields. 更好地使用TemplateFields。 You'll have much more control that way. 这样您将拥有更多的控制权。

<asp:GridView ID="GridView1" runat="server">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <%# Eval("myColumn") %>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Now GridView1.Columns.Count will return 1 . 现在GridView1.Columns.Count将返回1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.ArgumentOutOfRangeException: &#39;索引超出范围。 必须是非负的并且小于集合的大小。 参数名称:索引&#39; - System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index' System.ArgumentOutOfRangeException:索引超出范围。 必须是非负的并且小于集合的大小。 参数名称:索引 - System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index System.ArgumentOutOfRangeException 索引超出范围。 必须是非负数且小于集合的大小。 参数名称:索引 - System.ArgumentOutOfRangeException Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 错误:System.ArgumentOutOfRangeException:索引超出范围。 必须为非负数且小于集合的大小 - Error: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection 指数超出范围。 必须是非负数且小于集合的大小。 参数名称:index - Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 索引超出范围。 必须为非负数并且小于集合的大小。 参数名称:索引 - Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 索引超出范围。 必须为非负数并且小于集合的大小。 参数名称:索引 - Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 索引超出范围。 必须为非负数并且小于集合的大小。 参数名称:索引 - Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 指数超出范围。 必须是非负数且小于集合的大小。 参数名称:index - Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 指数超出范围。 必须是非负数且小于集合的大小。 (参数‘索引’)" - Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM