Search code examples
sqlvb.netms-accessdatagridviewdapper

How to change value from SQL in datagridview using Dapper with MS-Access database in VB.NET


I want to change the value that appears in the datagridview whether there is a solution and for the actual database record information if in union then there are 100,000 but each value of the column "WRH" for each table only has one value unless there is one table that has 2 such values.

The value information I want to change is as follows:

001 BECOMES A
002 BECOMES B
003 BECOMES C

Or there is another solution please guide me

Thanks

Const constrA = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\TABLEA.accdb;Persist Security Info=False;"
Const constrB = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\TABLEB.accdb;Persist Security Info=False;"
Const constrC = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\TABLEC.accdb;Persist Security Info=False;"

Function GetUsers(constr As String) As IEnumerable(Of Users)
        Using con As OleDbConnection = New OleDbConnection(constr)
      Return con.Query(Of Users)("SELECT * From Users Where WRH ='001' OR WRH ='002' OR WRH ='003'")
        End Using
End Function

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
   DataGridView1.DataSource = GetUsers(constrA).Union(GetUsers(constrB)).Union(GetUsers(constrC)).ToList
End Sub

Public Class Users
    Public Property Id() As Integer
    Public Property Firstname() As String
    Public Property LastName() As String

    Public Property WRH() As String
End Class

Table A : Users

ID FIRSTNAME LASTNAME WRH
1 TEST1000 1000TEST 001
2 TEST2000 2000TEST 001

Table B : Users

ID FIRSTNAME LASTNAME WRH
1 TEST3000 3000TEST 002
2 TEST4000 4000TEST 002

Table C : Users

ID FIRSTNAME LASTNAME WRH
1 TEST5000 5000TEST 003
2 TEST6000 6000TEST 004

Desired result in datagridview

FIRSTNAME LASTNAME WRH
TEST1000 1000TEST A
TEST2000 2000TEST A
TEST3000 3000TEST B
TEST4000 4000TEST B
TEST5000 5000TEST C

Solution

  • I have 2 answers.

    First Answer below based on link Changing datagridview cell color based on condition regularly!

    Only for small records

        Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
            For Each Myrow As DataGridViewRow In DataGridView1.Rows 'Here 2 cell is target value and 1 cell is Volume
                If Myrow.Cells(3).Value.ToString() = "001" Then
                    Myrow.Cells(4).Value = "A"
                End If
                If Myrow.Cells(3).Value.ToString() = "002" Then
                    Myrow.Cells(4).Value = "B"
                End If
                If Myrow.Cells(3).Value.ToString() = "003" Then
                    Myrow.Cells(4).Value = "C"
                End If
            Next Myrow
    
        End Sub
    

    Second Answer below based on link Multiple IIF Criteria in Access regularly!

    Only for large records

    "SELECT ID,Firstname,LastName,WRH,IIF([WRH] ='001','A',IIF([WRH] ='002','B',IIF([WRH] ='003','C'))) AS WRH_Name From Users Where WRH ='001' OR WRH ='002' OR WRH ='003'"