2
\$\begingroup\$

I want to check if any empty cells found or not in a Datagridview when save data to database, so I've written this function:

Public Function IsDataGridViewEmpty(ByRef dataGridView As DataGridView) As Boolean
Dim isEmpty As Boolean
isEmpty = False
For Each row As DataGridViewRow In dataGridView.Rows             
    For Each cell As DataGridViewCell In row.Cells                
        If Not String.IsNullOrEmpty(cell.Value) Then              

            If Not String.IsNullOrEmpty(Trim(cell.Value.ToString())) = True Then
                Dim d = Trim(cell.Value.ToString())
                isEmpty = False

            End If
        Else
            isEmpty = True
            Exit For
        End If
    Next
Next
Return isEmpty
End Function

The code is working now, but are any improvements needed? Is there any way to do this without a foreach loop?

Function calling:

 Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If IsDataGridViewEmpty(Me.DataGridView1) = True Then
            MsgBox("Empty Cells Found PLease Check!")
        End If
    End Sub

Source

I changed the code from the source website, so see the difference with my code and the source link.

Can anyone explain why we use By ref in a function parameter?

 Public Function IsDataGridViewEmpty(ByRef dataGridView As DataGridView) As Boolean
\$\endgroup\$
  • 1
    \$\begingroup\$ the ByRef simply means that what you feed in here is going to be affected by what you do to it in the function. you are passing a reference into the function, whereas ByVal you are only passing the value into the function/method \$\endgroup\$ – Malachi Dec 19 '17 at 18:50
1
\$\begingroup\$

The ByRef simply means that what you feed in here is going to be affected by what you do to it in the function. You are passing a Variable or object into the function, whereas ByVal you are only passing the value of the Variable into the function/method.

Also, I am not sure what exactly you were meaning to do with this:

Dim d = Trim(cell.Value.ToString())

but it is an orphan, it is not being used for anything.

Another thing that you should keep in mind is that VB is sensitive and you should indent consistently with the VB Standard.

so the entire function should be indented like this:

Public Function IsDataGridViewEmpty(ByRef dataGridView As DataGridView) As Boolean
    Dim isEmpty As Boolean
    isEmpty = False
    For Each row As DataGridViewRow In dataGridView.Rows             
        For Each cell As DataGridViewCell In row.Cells                
            If Not String.IsNullOrEmpty(cell.Value) Then              

                If Not String.IsNullOrEmpty(Trim(cell.Value.ToString())) = True Then
                    Dim d = Trim(cell.Value.ToString())
                    isEmpty = False

                End If
            Else
                isEmpty = True
                Exit For
            End If
        Next
    Next
    Return isEmpty
End Function
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.