Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
i have a dgv and i want to calculate marks. the dgv accepts string format from access db datatype set to text and sums various columns, To be able to sum the cells value i use the code below which works fine. My question is how to improve the decision making part so that at some point it can re-accept string  format to allow for both numbers as well as string operations. at the same time the dgv encounters empty strings.


What I have tried:

'Function Overloading with Decision Making in a type cast
        Private Function GetIntOnly(ByVal value As String) As Integer
            Dim x As Int16
            For j As Int16 = 0 To dgw.Columns.Count - 1
                For r As Int16 = 0 To dgw.Rows.Count - 1
                    If value = "" Then
                        value = ""
                    Else
                    x = value.ToString
                    End If
                Next
            Next
            Return x
        End Function


 'I call the function whenever I sum dgv cell value using the overload something like this ..
 
Private Function AddElement()
Return Compulsory() + Sciences() + humanities() + Booster() 
End Function 

Public Function Compulsory() As Int16 
Dim c As Int16 
For r As Int16 = 0 To dgw.Rows.Count - 1
c = GetIntOnly(dgw.Rows(r).Cells("english").Value.ToString) + GetIntOnly(dgw.Rows(r).Cells("kiswahili").Value.ToString) + GetIntOnly(dgw.Rows(r).Cells("Mathematics").Value.ToString) dgw.Rows(r).Cells("total").Value = c 
Next 
Return c 
End Function
Posted
Updated 23-Oct-22 3:59am
v6
Comments
[no name] 19-Oct-22 20:29pm    
The "value" you're looping has no connection to the columns and rows you're looping over.
Ralf Meier 20-Oct-22 2:52am    
... and also makes no sense at all ...
Perhaps you post us the correct code for suggestions ...
Member 14861076 20-Oct-22 11:46am    
I call the function whenever I sum dgv cell value using the overload something like this .. Private Function AddElement() Return Compulsory() + Sciences() + humanities() + Booster() End Function Public Function Compulsory() As Int16 Dim c As Int16 For r As Int16 = 0 To dgw.Rows.Count - 1 c = GetIntOnly(dgw.Rows(r).Cells("english").Value.ToString) + GetIntOnly(dgw.Rows(r).Cells("kiswahili").Value.ToString) + GetIntOnly(dgw.Rows(r).Cells("Mathematics").Value.ToString) dgw.Rows(r).Cells("total").Value = c Next Return c End Function
Ralf Meier 20-Oct-22 16:45pm    
use the "improve question" widget to show the actual code - but not as "something like this"
Member 15627495 23-Oct-22 10:43am    
use the 'cast' operators :
Cstr(var) // Cint(var)

I understand hardly your questions, the only type handling integer and string is object type.
you really can not have one var as integer and as string at the same time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900