1

I have a gridview which is populated from a datasource, as such:

   Date              Hours
 January/2016        26.31
 January/2016        25.65
 February/2016       12.3
   ....          ...     ....

From that table I check for the weeks every month had and count them, so I can get an average later on, this is my code.

For intMonth = 1 To 12
    For X = 0 To GridView.Rows.Count - 1

        intDate = Convert.ToDateTime(gvTiempos.Rows(X).Cells(0).Text)

        If DatePart("m", intDate) = intMonth Then
            Calcular()            
        End If

      Next
Next

This is the Calcular() function:

Public Function Calcular()
 Select Case intMonth
   Case 1: JanWeeks+=1
   Case 2: FebWeeks+=1
    ...
   Case 12: DecWeeks+=1

What I would like to do is to avoid the whole Calcular() function, and do it "dynamically".

In other words, I would like to get something like this:

 Weeks(intMonth) = "Sum of Weeks for that month"

Where intMonth determinate in which WeekVariable the Sum will be storaged.
I have tried with the List(Of T) method unsuccessfully, because this creates a list of variables with a static value, according to my understanding.

Thanks in advance

6
  • you could use a dictionary colMonths(intMonth) +=1 If you have a data source you should use that rather than the control. You could probably eveb write a linq query and get all 12(?) results at once. It also looks like you should turn on Option Strict Commented Mar 31, 2016 at 18:19
  • You're probably better off to use an array. Then just use Weeks(intMonth) = Weeks(intMonth) + 1. On a side note, you can eliminate this line: intDate = GridView.Rows(X).Cells(0).Text. It's value is not being used and you are reassigning it on the next line. Commented Mar 31, 2016 at 18:23
  • @Icemanind yep ypu are right about that line, just edited the code, was a misspelled line. About the Weeks(intMonth) = Weeks(intMonth) + 1 it throwed this error: Index was out of range. Must be non-negative and less than the size of the collection. Commented Mar 31, 2016 at 18:29
  • @abichango - Did you declare it? : Dim Weeks(12) As Integer Commented Mar 31, 2016 at 18:31
  • @Plutonix could you explain a bit more on the dictonary method, I did look for it prior to my question but couldnt quite understand it. About the Option Strict its a huge project in which I was barely involved and I don't know what issues will come up with the other forms, if I turn it on :/ Commented Mar 31, 2016 at 18:33

2 Answers 2

1

As per our conversation in your comments, you should be able to simply do this:

Dim Weeks(12) As Integer

Then replace your call to Calcular() to this:

If DatePart("m", intDate) = intMonth Then
    Weeks(intMonth) = Weeks(intMonth) + 1
End If

This creates an array to hold the sum for each month in the year (12 months).

Sign up to request clarification or add additional context in comments.

Comments

1

Given a DataTable dt as the DataSource and cell/item 1 (not 0) has the formatted date, you can create a Dictionary(of Int32, Int32) to hold the count for each month. Iterating the table should be faster than poking around the control; besides, the DataTable is where the data is.

Dim col As New Dictionary(Of Int32, Int32)
' create 12 keys for 12 months
For n As Int32 = 1 To 12
    col.Add(n, 0)
Next

For Each row As DataRow In dt.Rows
    col(Convert.ToDateTime(row(1)).Month) += 1
Next

Some slots will be zero, because the code added them all ahead of time. A List(Of Int32) would also work using MonthList(n) to represent the month N-1. You can also just query the table, group and count in one line:

Dim monthCount = dt.AsEnumerable.GroupBy(Function(v) v.Field(Of DateTime)(1).Month,
                            Function(k, v) New With {.Month = k, .Count = v.Count()}).
                       ToArray()

In this case, the result is a succinct list with the month and count (my date list was: {#1/11/2016#, #1/23/2016#, #1/16/2016#, #2/11/2016#, #4/15/2016#}):

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.