Dim i1 As Integer
        Dim i2 As Integer
        For i1 = 0 To Me.DataGridView5.Columns.Count - 1
            For i2 = 0 To Me.DataGridView5.Rows.Count - 1
                If String.IsNullOrEmpty(Me.DataGridView5.Item(i1, i2).Value) Then
                    MessageBox.Show("You must fill in all cells before you can save the data.", "Empty Cells", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    DataGridView5.Rows(i1).Cells(i2).Style.BackColor = Color.Bisque  'Index was out of range. Must be non-negative and less than the size of the collection.
                    Exit Sub
                    'MessageBox.Show(String.Format("{0},{1}", i1, i2)
                End If
            Next
        Next

Dani AI

Generated

Root cause: the row/column indices were swapped when you tried to color the cell. As pointed out, the counter that iterates columns must be used as the column index and the row counter as the row index. DataGridView expects Rows(rowIndex).Cells(colIndex) (or Item(colIndex, rowIndex)), so passing the column index into Rows(...) produces the "Index out of range" exception. Use descriptive names (for example row/col) to avoid that confusion.

A safer, more robust pattern is to iterate rows then cells, skip the new blank row, convert cell values to string safely, and show one message after scanning the grid. Example:

Dim foundEmpty As Boolean = False

For Each row As DataGridViewRow In DataGridView5.Rows
    If row.IsNewRow Then Continue For
    For Each cell As DataGridViewCell In row.Cells
        Dim textValue As String = Convert.ToString(cell.Value)
        If String.IsNullOrWhiteSpace(textValue) Then
            cell.Style.BackColor = Color.Bisque
            foundEmpty = True
        End If
    Next
Next

If foundEmpty Then
    MessageBox.Show("You must fill in all cells before you can save the data.", "Empty Cells", MessageBoxButtons.OK, MessageBoxIcon.Information)
    Exit Sub
End If

Troubleshooting tips: enable Option Strict On to catch implicit conversions at compile time; use Convert.ToString (or check If cell.Value Is Nothing Then ...) rather than calling .ToString() on a potential Nothing. If the grid is data-bound or in virtual mode, validate the underlying source or use CellValidating/RowValidating events instead. Rename loop counters (for example colIndex, rowIndex) while testing to make the intent obvious and prevent the same off-by-index mistake.

This addresses the index error and implements the intended behavior without popping lots of message boxes during the scan.

Recommended Answers

All 2 Replies

i want to check empty cells in datagrid view when button clicked and change its color but i find this error..

Line 7: you pass i1 to the Rows collection, i1 is the column counter.
Perfect example of giving sensible names to your counter variables. If you named i1, "col" or something you would not have made that mistake.
Hope I helped you out!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.