Click here to Skip to main content
15,908,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends,
I am developing a windows application in vb.net and MS Access (back end)
in which i have two tables 1st is StudentInfo table and Second is attendance table.
in studentinfo table all the students records stored, but in attendance table only present students name and roll no. stored.
after taking attendance i want to show all records in Data GridView which store in studentinfo table and Highlight only those records which are present in attendance table with different colors.
I have done 50% job i.e. show all records from studentinfo table in data grid view.
<pre lang="vb">

Dim cn As OleDbConnection
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
Dim da As OleDbDataAdapter
Dim str As String
Dim icount As Integer

Private Sub filldata()
        cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= SRS.mdb;")
        cn.Open()
        
        da = New OleDbDataAdapter("SELECT * FROM StudentInfo", cn)
        Dim dt As New DataTable("StudentInfo")
        da.Fill(dt)
        DataGridView1.DataSource = dt
    End Sub


How can i highlight attendance tables records in data grid view.
Please Remember i use VB.net as front end and MS Access 2003 as back end.
Thanks
Posted

1 solution

Try this and see if it works. I don't use DataSets.
VB
Private Sub LoadGrid()
    Try
        Dim oODBCConnection As Odbc.OdbcConnection
        Dim sConnString As String = "Driver={Microsoft Access Driver (*.mdb)};Dbq=SRS.mdb;"
        Dim comm As Odbc.OdbcCommand
        oODBCConnection = New Odbc.OdbcConnection(sConnString)
        oODBCConnection.Open()
        Dim dr As Odbc.OdbcDataReader

        Dim sql As String
        Dim rownum as Integer = 0
        sql = "SELECT * FROM StudentInfo"
        comm = New Odbc.OdbcCommand(sql, oODBCConnection)
        dr = comm.ExecuteReader()
        With Me.DataGridView.Rows
            .Clear()
            While dr.Read()
                .Add(dr!field1, _
                    dr!StudentIsPresentTableFieldName, _
                    dr!field3)
                ' Here is where the checking is accomplished,
                ' Look through the new row added and highlight
                ' the row that meets the proper conditions.
                With Me.DataGridView.Rows(rownum)
                    If .Cell(1).Value = 1 Then
                        .DefaultCellStyle.BackColor = Color.Yellow
                        .DefaultCellStyle.SelectionBackColor = Color.Yellow
                    ElseIf .Cell(1).Value = 2 then
                        .DefaultCellStyle.BackColor = Color.Silver
                        .DefaultCellStyle.SelectionBackColor = Color.Silver
                    End If
                End With
                rownum = rownum + 1
            End While
        End With

        dr.Close()
        oODBCConnection.Close()
        comm.Dispose()
        oODBCConnection.Dispose()
    Catch ex As Exception
        MessageBox.Show("FAILED TO LOAD GRID: " & ex.Message)
    End Try
End Sub
 
Share this answer
 

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