• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

Followup DataGrid question

Starter (with attached project) question: http://www.experts-exchange.com/Q_21863651.html

Now, once I've established the datagrid via code - how can I access the datarows for use in calculations?
0
sirbounty
Asked:
sirbounty
  • 6
  • 3
  • 2
1 Solution
 
Bob LearnedCommented:
If you are working with an unbound DataGrid, then how are you adding items to the DataGrid?

Bob

0
 
sirbountyAuthor Commented:
I've got one routine that adds it/binds the datagrid.
But if I wanted another routine to 'loop' through the records, how would I accomplish that?
0
 
Bob LearnedCommented:
Can you show me what you mean by all that?  Are you using a DataGrid in unbound mode with a DataTable/DataSet?

Bob
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sirbountyAuthor Commented:
Yes.  Let's say my form load populates my datagrid.  In your example, the datagrid would hold the batter's stats (hard stats).  If I wanted to 'average' that data, how could I do that with what's 'currently' in the grid?  Do I have to (re)declare rows to access data that's currently displayed or is it simply a 'accesscurrentrowdata' procedure?
0
 
sirbountyAuthor Commented:
More specifically - if I didn't know his average, and the grid held the stats from each game, how would I go about acquiring his batting average?
0
 
SanclerCommented:
Having looked at the latest version of your project, your datagrid is bound, not unbound.  You bind it in code

                With dvSales
                    .Table = mdsStoreSales.Tables("Sales")
                    .RowFilter = "stor_id = '" & cboStore.SelectedValue.ToString() & "'"
                    dgrSales.DataSource = dvSales '<<< THIS LINE
                End With

rather than at design-time, but it's still "bound".

So the "normal" (not necessarily the only) way of dealing with the data would be would be via the datasource, rather than the datagrid.  It's the datasource that "holds" the data, the datagrid just "displays" it.  Stick these lines after the code I've just quoted to see what I mean.

                Dim totalQuantity As Short = 0
                For Each drv As DataRowView In dvSales
                    totalQuantity += CType(drv("qty"), Short)
                Next
                MsgBox("Average quantity = " & totalQuantity / dvSales.Count)

Roger
0
 
sirbountyAuthor Commented:
If I wanted to do the calculations in another sub, the dataview/datasource would need to be declared module-level?
0
 
SanclerCommented:
That would be one way, but it's not necessarily the only way.  E.g.

   Private Function GetTotal(dv As Dataview, colName As String) As Short
      Dim totalQuantity As Short = 0
      For Each drv As DataRowView In dvSales
         totalQuantity += CType(drv(colName), Short)
      Next
      Return totalQuantity
   End Function

Called

   Dim MyTotal As Short = GetTotal(dvSales, "qty")

That is, pass the dataview and/or the columname to a calculation sub/function.

Similar for Average, or any other calculation.

Roger
0
 
sirbountyAuthor Commented:
I still don't understand how you pass dvSales if it's not declared 'globally'...
I got it working by declaring it at the module-level, but was just curious about what you referenced in your last post.
0
 
SanclerCommented:
When you pass an object to a Sub or Function as one of its arguments it means that the Sub or Function can "see" that object wherever it was declared.  If you pass it ByVal - which is the default - the Sub or Function cannot alter it in any way.  But if you pass it ByRef, then that allows the Sub or Function to alter it, even though it was declared as Private and in a different Class.  Effectively, the class/module that is calling a Sub or Function that requires its arguments to passed ByRef is giving that Sub or Function direct access to one of its private members.

If you don't pass an object to a Sub or Function as one of its arguments - whether ByVal or ByRef - the Sub or Function will only be able to "see" that object if it is declared with a scope that is accessible to the Sub or Function.  That might be Global, but it needn't be.  So, for instance, even if it was declared as Private, but in the same class/module as the Sub or Function concerned, the Sub or Function would still be able to see it.

That's perhaps all a bit confusing - and I've only skimmed over it ;-) - but to put some meat on the theoretical bones, if instead of

   Private Function GetTotal(dv As Dataview, colName As String) As Short

the first line of the code I'd posted before had been

   Private Function GetTotal() As Short '<<< ARGUMENTS REMOVED

and I'd just copied the earlier code into the body

       Dim totalQuantity As Short = 0
       For Each drv As DataRowView In dvSales
          totalQuantity += CType(drv("qty"), Short)
       Next
       Return totalQuantity
   End Function

it would not have worked unless the Function could "see" dvSales.  If it was declared at Form level in the same Form as the Function then it would be OK.  But if the Function was in a module, and the Form had not declared dvSales as Public, the Function would not have worked.  But if dvSales had been declared anywhere else in the Project as Public, or Global, then the Function would have worked.

Roger
 
0
 
sirbountyAuthor Commented:
Ok - now I get it (thought you were changing the rules on me ;).  That's the way I understood it.  Thanx again.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now