![](/img/trans.png)
[英]System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index'
[英]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);
}
我只想在gridview的頁腳添加價格。 我不明白怎么了。 該行中的錯誤是GridView1.Columns [3] .FooterText = String.Format(“ {0:c}”,TotalSales);
您可能在GridView中使用了AutoGenerateColumns="true"
。 使用此方法時,您只能訪問RowCreated
和RowDataBound
事件中的列。 網格構建完成后,列數將為0。
更好地使用TemplateFields。 這樣您將擁有更多的控制權。
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%# Eval("myColumn") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
現在GridView1.Columns.Count
將返回1
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.