p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 1.0 and 1.1 Basics (http://p2p.wrox.com/forumdisplay.php?f=60)
-   -   display sum in footer of gridview (http://p2p.wrox.com/showthread.php?t=51973)

sarah lee December 27th, 2006 11:41 AM

display sum in footer of gridview
 
hi all

I am trying to display sum in the footer of a gridview., i am getting the following error.

can anyone please tel me what can be the reason

Specified argument was out of the range of valid values.
Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index

Source Error:



Line 44:
Line 45: Dim unitprice As Decimal = Convert.ToDecimal(e.Row.Cells(2).Text)
Line 46: Dim t As TextBox = CType(e.Row.Cells(3).Controls(1), TextBox)
Line 47:
Line 48: Dim quantity As Integer = Integer.Parse(t.Text)




Source File: C:\VS2003\ShoppingCart3VB\example.aspx.vb Line: 46

Stack Trace:



[ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index]
   System.Web.UI.ControlCollection.get_Item(Int32 index) +69
   example.GridView1_RowDataBound(Object sender, GridViewRowEventArgs e) in C:\VS2003\ShoppingCart3VB\example.aspx.vb:46
   System.Web.UI.WebControls.GridView.OnRowDataBound( GridViewRowEventArgs e) +75
   System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) +233
   System.Web.UI.WebControls.GridView.CreateChildCont rols(IEnumerable dataSource, Boolean dataBinding) +3008
   System.Web.UI.WebControls.CompositeDataBoundContro l.PerformDataBinding(IEnumerable data) +59
   System.Web.UI.WebControls.GridView.PerformDataBind ing(IEnumerable data) +12
   System.Web.UI.WebControls.DataBoundControl.OnDataS ourceViewSelectCallback(IEnumerable data) +101
   System.Web.UI.DataSourceView.Select(DataSourceSele ctArguments arguments, DataSourceViewSelectCallback callback) +25
   System.Web.UI.WebControls.DataBoundControl.Perform Select() +140
   System.Web.UI.WebControls.BaseDataBoundControl.Dat aBind() +68
   System.Web.UI.WebControls.GridView.DataBind() +5
   example.FillCartFromDB() in C:\VS2003\ShoppingCart3VB\example.aspx.vb:31
   example.Page_Load(Object sender, EventArgs e) in C:\VS2003\ShoppingCart3VB\example.aspx.vb:19
   System.EventHandler.Invoke(Object sender, EventArgs e) +0
   System.Web.UI.Control.OnLoad(EventArgs e) +80
   System.Web.UI.Control.LoadRecursive() +49
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3745





the code which i used is

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

If e.Row.RowType = DataControlRowType.DataRow Then

Dim unitprice As Decimal = Convert.ToDecimal(e.Row.Cells(2).Text)

Dim t As TextBox = CType(e.Row.Cells(3).Controls(1), TextBox)

Dim quantity As Integer = Integer.Parse(t.Text)

total = total + (unitprice * quantity)

'ElseIf e.Row.RowType= DataControlRowType.Footer Then

e.Row.Cells(3).Text = "Total Amount : " & total.ToString("c")

'End If

end Sub
thanks



dparsons December 27th, 2006 11:48 AM

Dim t As TextBox = CType(e.Row.Cells(3).Controls(1), TextBox)

Try this instead:

Dim t as TextBox = CType(e.Item.Cells(3).Controls(1), TextBox)

If this errors out then either the cell you are referencing does not exist or there is only one control in the cell thus controls(1) does not exist.

-------------------------
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.

^^Thats my signature

sarah lee December 27th, 2006 11:55 AM

but item is not supported by gridview control, right?


dparsons December 27th, 2006 12:36 PM

I have not used a gridview control so I am not certain but i believe that is true. What you might want to try is something along the lines of:

GridViewRow row = Gridview.Rows[e.RowIndex];
        if (row != null)
        {

            TextBox txt= row.FindControl("yourtextbox") as TextBox;
            //do something with the text box control.
        }

-------------------------
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.

^^Thats my signature


All times are GMT -4. The time now is 09:50 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.