Link to home
Start Free TrialLog in
Avatar of sirbounty
sirbountyFlag for United States of America

asked on

Followup DataGrid question

Starter (with attached project) question: https://www.experts-exchange.com/questions/21863651/Pretty-up-datagrid.html

Now, once I've established the datagrid via code - how can I access the datarows for use in calculations?
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

If you are working with an unbound DataGrid, then how are you adding items to the DataGrid?

Bob

Avatar of sirbounty

ASKER

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?
Can you show me what you mean by all that?  Are you using a DataGrid in unbound mode with a DataTable/DataSet?

Bob
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?
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?
ASKER CERTIFIED SOLUTION
Avatar of Sancler
Sancler

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If I wanted to do the calculations in another sub, the dataview/datasource would need to be declared module-level?
Avatar of Sancler
Sancler

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
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.
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
 
Ok - now I get it (thought you were changing the rules on me ;).  That's the way I understood it.  Thanx again.