dcsimg
Results 1 to 9 of 9

Thread: Check for blank/empty cells in DataGridView Column

  1. #1

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,118

    Check for blank/empty cells in DataGridView Column

    Hi,

    If I have this:

    Name:  1.JPG
Views: 154
Size:  11.2 KB

    I would like to disable the save button.. It can be there never was a value, or there was a value and the user deleted it.

    This is what I have tried (but I am getting both messages) and I can't figure out where am I going wrong:

    Code:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            For i As Integer = 0 To TblCausesDataGridView.RowCount - 1
    
                If String.IsNullOrEmpty(TblCausesDataGridView.Rows(i).Cells.Item(0).Value.ToString()) Then
    
    
                    MsgBox("There are blanks")
                Else
                    MsgBox("No blanks")
                End If
            Next
        End Sub
    Thanks...
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,146

    Re: Check for blank/empty cells in DataGridView Column

    For each individual row you are checking the value, and showing a message.

    In order to check whether any rows are blank, use a boolean variable. Set it to false before the loop, and set it to true where you currently have the "There are blanks" message.

    After the loop use the boolean variable to determine which message to show.

  3. #3

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,118

    Re: Check for blank/empty cells in DataGridView Column

    Quote Originally Posted by si_the_geek View Post
    For each individual row you are checking the value, and showing a message.

    In order to check whether any rows are blank, use a boolean variable. Set it to false before the loop, and set it to true where you currently have the "There are blanks" message.

    After the loop use the boolean variable to determine which message to show.
    Hi SI

    The message boxes is just to test... Thank you for that Idea I can do that..

    The problem is with the code.. please see here:


    Code:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim thereIsBlanks As Boolean = False
    
            For i As Integer = 0 To TblCausesDataGridView.RowCount - 1
    
                If String.IsNullOrEmpty(TblCausesDataGridView.Rows(i).Cells.Item(0).Value.ToString()) Then
    
                    thereIsBlanks = True
                Else
                    thereIsBlanks = False
                End If
            Next
    
            MsgBox("Blank was found: " & thereIsBlanks, vbInformation, "Result")

    Name:  1.JPG
Views: 38
Size:  14.4 KB
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,146

    Re: Check for blank/empty cells in DataGridView Column

    The Else section shouldn't be there, and it is just undoing the point of the variable.

    The original value of the variable is false (no blanks), and it is set to true if a blank is found.

    Setting it back to false again means that you are ignoring blanks unless they are on the final row.

  5. #5

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,118

    Re: Check for blank/empty cells in DataGridView Column

    Quote Originally Posted by si_the_geek View Post
    The Else section shouldn't be there, and it is just undoing the point of the variable.

    The original value of the variable is false (no blanks), and it is set to true if a blank is found.

    Setting it back to false again means that you are ignoring blanks unless they are on the final row.
    Good morning SI,

    Thank you very much for helping me with this. It it is working perfectly now

    I want to understand what I did wrong.... can you tell me?


    Here I set the booloean to false

    Code:
    Dim thereIsBlanks As Boolean = False
    Going though the rows:


    Code:
     For i As Integer = 0 To TblCausesDataGridView.RowCount - 1

    So whilst going through the rows if a blank/null value is found then set the boolean to true


    Code:
    If String.IsNullOrEmpty(TblCausesDataGridView.Rows(i).Cells.Item(0).Value.ToString()) Then
    
                    thereIsBlanks = True


    So here is my mistake and am try to understand why it is a mistake. So I already say whilst going through the rows if there is a blank then
    Code:
    thereIsBlanks = True
    But if no rows was found to be blank/null then set the boolean to false

    Code:
    Else
                    thereIsBlanks = False

    Code:
    End If
    
            Next
    
            MsgBox("Blank was found: " & thereIsBlanks, vbInformation, "Result")

    It is obviously wrong... I just don't get this part:
    Setting it back to false again means that you are ignoring blanks unless they are on the final row
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  6. #6

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,118

    Re: Check for blank/empty cells in DataGridView Column

    Also, what would be the recommenced event for this check?

    I currently use Paint... But I thin this will fire non stop

    Thank you
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  7. #7
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    601

    Re: Check for blank/empty cells in DataGridView Column

    As I understand it, you want to check if ANY row has a null/empty value in a particular cell. Say you have that condition on the first row: your original code would detect that and set thereISblanks to TRUE. However, if the next row did not have a "blank", your thereISblanks boolean would be set back to FALSE and the result from the check on the previous row would be lost. Once you find a blank, you are done and you could exit your FOR loop at that point, which would speed up the test if you have many rows:
    Code:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim thereIsBlanks As Boolean = False
    
            For i As Integer = 0 To TblCausesDataGridView.RowCount - 1
    
                If String.IsNullOrEmpty(TblCausesDataGridView.Rows(i).Cells.Item(0).Value.ToString()) Then
                    thereIsBlanks = True
                    Exit For
                End If
            Next
    
            MsgBox("Blank was found: " & thereIsBlanks, vbInformation, "Result")

  8. #8

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,118

    Re: Check for blank/empty cells in DataGridView Column

    Quote Originally Posted by paulg4ije View Post
    As I understand it, you want to check if ANY row has a null/empty value in a particular cell. Say you have that condition on the first row: your original code would detect that and set thereISblanks to TRUE. However, if the next row did not have a "blank", your thereISblanks boolean would be set back to FALSE and the result from the check on the previous row would be lost. Once you find a blank, you are done and you could exit your FOR loop at that point, which would speed up the test if you have many rows:
    Code:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim thereIsBlanks As Boolean = False
    
            For i As Integer = 0 To TblCausesDataGridView.RowCount - 1
    
                If String.IsNullOrEmpty(TblCausesDataGridView.Rows(i).Cells.Item(0).Value.ToString()) Then
                    thereIsBlanks = True
                    Exit For
                End If
            Next
    
            MsgBox("Blank was found: " & thereIsBlanks, vbInformation, "Result")
    Hi Paul, this is a great explanation... Okay... Now I makes sense.. (and I feel very stupid) Because.... now I see it is actually so obvious!!

    Thank you very much
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  9. #9
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    158

    Re: Check for blank/empty cells in DataGridView Column

    An otherway would be to use the row ErrorText and the cell or row validating event.

    Code:
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            For i = 0 To 9
                Dim Index As Integer = Me.DataGridView1.Rows.Add()
                Me.DataGridView1.Rows(Index).ErrorText = "Empty row"
            Next
    
        End Sub
    Code:
        Private Sub DataGridView1_RowValidating(sender As Object, e As DataGridViewCellCancelEventArgs) Handles DataGridView1.RowValidating
            Dim Cell As System.Windows.Forms.DataGridViewCell = Me.DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex)
    
            If Cell.Value = Nothing Then
            Else
                Me.DataGridView1.Rows(e.RowIndex).ErrorText = String.Empty
            End If
        End Sub
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width