Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleCreate a DataTable with row cell errors, and display it in a DataGridView in Visual Basic .NET
DescriptionThis example shows how to Create a DataTable with row cell errors, and display it in a DataGridView in Visual Basic .NET.
KeywordsDataGridView, DataTable, error, row error, cell error, Visual Basic .NET, VB.NET
CategoriesControls, Database
 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    ' Make a DataTable.
    Dim students_table As New DataTable("Students")

    ' Add columns.
    students_table.Columns.Add("FirstName", GetType(String))
    students_table.Columns.Add("LastName", GetType(String))
    students_table.Columns.Add("Quiz Average", _
        GetType(String))
    students_table.Columns.Add("Test Average", _
        GetType(String))

    ' Make the combined FirstName/LastName unique.
    Dim first_last_columns() As DataColumn = { _
        students_table.Columns("FirstName"), _
        students_table.Columns("LastName") _
    }
    students_table.Constraints.Add( _
        New UniqueConstraint(first_last_columns))

    ' Make some student data.
    students_table.Rows.Add(New Object() {"Art", "Ant", 74, _
        77})
    students_table.Rows.Add(New Object() {"", "Bug", 80, _
        81})
    students_table.Rows.Add(New Object() {"Cid", "Cat", 97, _
        89})
    students_table.Rows.Add(New Object() {"Deb", "", 82, _
        76})
    students_table.Rows.Add(New Object() {"Ed", "Eager", _
        67, 60})
    students_table.Rows.Add(New Object() {"Fran", "Fix", _
        71, 69})
    students_table.Rows.Add(New Object() {"Gus", "Gantry", _
        88, 95})
    students_table.Rows.Add(New Object() {"Hil", "Harris", _
        63, 58})

    ' Set errors.
    For r As Integer = 0 To students_table.Rows.Count - 1
        ' If a student has no first or last name,
        ' flag the row as in error.
        If students_table.Rows(r).Item(0) = "" OrElse _
           students_table.Rows(r).Item(1) = "" _
        Then
            students_table.Rows(r).RowError = "Missing " & _
                "first or last name"
        End If

        ' Loop over the quiz and test average columns.
        For c As Integer = 2 To 3
            If students_table.Rows(r).Item(c) < 70 Then
                ' Place a cell error in this cell.
                students_table.Rows(r).SetColumnError(c, _
                    "Score under 70!")
            End If
        Next c
    Next r

    ' Attach dgvStudents to the DataTable.
    dgvStudents.DataSource = students_table

    ' Shade scores below 75 pink.
    Dim low_score_style As New DataGridViewCellStyle()
    low_score_style.BackColor = Color.Pink
    low_score_style.ForeColor = Color.Red
    For r As Integer = 0 To students_table.Rows.Count - 1
        If students_table.Rows(r).Item("Quiz Average") < 75 _
            Then
            dgvStudents.Item(2, r).Style = low_score_style
        End If
        If students_table.Rows(r).Item("Test Average") < 75 _
            Then
            dgvStudents.Item(3, r).Style = low_score_style
        End If
    Next r
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated