Results 1 to 8 of 8

Thread: datagrid total

  1. #1

    Thread Starter
    Fanatic Member davebat's Avatar
    Join Date
    Dec 2002
    Posts
    727

    Resolved datagrid total

    I have a datagrid which is bound to a filtered dataview, what is the best way to go about totalling the numerical values and putting them in the footer

    VB Code:
    1. Dim dr As DataRowView
    2.         Dim range01tot As Decimal = 0.0
    3.         Dim i As IEnumerator = dvOne.GetEnumerator()
    4.         While (i.MoveNext())
    5.             dr = i.Current
    6.             range01tot += (dr("value"))
    7.         End While
    Last edited by davebat; Oct 6th, 2004 at 07:24 AM.

  2. #2
    Hyperactive Member temp_12000's Avatar
    Join Date
    Jan 2004
    Location
    LA, USA
    Posts
    411

    Re: datagrid total

    Originally posted by davebat
    I have a datagrid which is bound to a filtered dataview, what is the best way to go about totalling the numerical values and putting them in the footer
    I tried two methods:

    1) use SQL to compute SUM;
    2) use a loop and fetch the values for each row in the datagrid, then add them;

    do not know whether they are good methods, but both work for me.



  3. #3
    Fanatic Member
    Join Date
    May 2002
    Posts
    746
    It's in C# but you should get the idea.
    Code:
    DataRowView dr; int hoursCount=0;
    IEnumerator i=viewStaffSchedule.GetEnumerator();
    while(i.MoveNext())
         {
              dr=(DataRowView)i.Current;	
              hoursCount+=int.Parse(dr["ProjectHours"].ToString());
         }
    lblTotalHours.Text=hoursCount.ToString();

  4. #4

    Thread Starter
    Fanatic Member davebat's Avatar
    Join Date
    Dec 2002
    Posts
    727
    thanks both of you, Brian I have translated this as well as i can into VB and am just unsure about the int.parse part, Im still pretty new to this.

    VB Code:
    1. Dim dr As DataRowView
    2.         Dim rangeo1tot As Integer = 0
    3.         Dim i As IEnumerator = dvOne.GetEnumerator()
    4.         While (i.MoveNext())
    5.             dr = i.Current
    6.             range01tot += Int.Parse(dr, "range01".ToString())
    7.         End While

    The int is underlined as an error "Overload resolution failed because no accesible 'int' accepts this number of arguments" Do you know what i need to do

  5. #5
    Hyperactive Member fret's Avatar
    Join Date
    Sep 2004
    Posts
    472
    try to use the:
    VB Code:
    1. range01tot += Integer.Parse(dr, "range01".ToString())

  6. #6

    Thread Starter
    Fanatic Member davebat's Avatar
    Join Date
    Dec 2002
    Posts
    727
    it needed to be

    VB Code:
    1. range01tot += Integer.Parse(dr("range01").ToString())

    I found a good c# to vb converter at

    http://authors.aspalliance.com/aldot...translate.aspx

    Only problem now is that I get the error message "Input string was not in a correct format. "

  7. #7

    Thread Starter
    Fanatic Member davebat's Avatar
    Join Date
    Dec 2002
    Posts
    727
    bump

  8. #8
    Fanatic Member
    Join Date
    May 2002
    Posts
    746
    This line maybe?
    Code:
    dr=(DataRowView)i.Current;
    I noticed yours doesn't have it. It's casting the enumerator to a DataRowView - I think the VB equivalent is CType.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width