Reputation: 4512
This piece of code is populating a DataGridView
from a database. Volunteers.Status
is defined as Integer at database level.
I want to show a string instead the integer number in Status column, but the application fails when I try to set the DataGridView1.Rows(row.Index).Cells(14).Value
to a string.
Error: INACTIVE is not a valid value for Int32
Code
SQL = "SELECT Volunteers.VolunteerID, Volunteers.NameVol AS Nombre, Volunteers.Surname AS Apellidos, Volunteers.email, Volunteers.Address AS Dirección, " & _
"Volunteers.ID AS DNI, Volunteers.DateOfBirth AS Fecha, Volunteers.PostalCode AS CP, Volunteers.City AS Ciudad, Volunteers.Province AS Provincia, Volunteers.Phone AS Telefono, " & _
"Volunteers.Studies AS Estudios, Volunteers.Vehicle AS Vehículo, Volunteers.Clothes AS Camiseta, Volunteers.Status AS Estado FROM Volunteers " & _
"LEFT JOIN ActivityVolunteer ON Volunteers.VolunteerID = ActivityVolunteer.VolunteerID " & _
"WHERE ActivityVolunteer.ActivityID = " & IDAct
Dim ds1 As New DataSet
Dim DA As New System.Data.OleDb.OleDbDataAdapter(SQL, SQLConnection)
DA.Fill(ds1)
DataGridView1.DataSource = ds1.Tables(0)
For Each row As DataGridViewRow In DataGridView1.Rows
Select Case DataGridView1.Rows(row.Index).Cells(14).Value
Case 0
DataGridView1.Rows(row.Index).Cells(14).Value = "INACTIVE"
Case 1
DataGridView1.Rows(row.Index).Cells(14).Value = "ACTIVE"
Case 2
DataGridView1.Rows(row.Index).Cells(14).Value = "OTHER"
End Select
Next
UPDATE:
Then I tried a trick, making this column invisible and then add an extra column with values based on the hidden column.
DataGridView1.Columns(14).Visible = False
DataGridView1.Columns.Add("Estado", "Estado")
For Each row As DataGridViewRow In DataGridView1.Rows
DataGridView1.Rows(row.Index).Cells(15).Value = Convert(row.Cells(14).Value)
Next
Function Convert(ByVal e As Integer) As String
Select Case e
Case 0
Return "INACTIVE"
Case 1
Return "ACTIVE"
Case 2
Return "OTHER"
End Select
Return String.Empty
End Function
This change does not throw any exception, but cells on the new column are empty.
How can I achieve that?
Upvotes: 0
Views: 3817
Reputation: 63065
I would change the sql statement to get the string as you needed like below
SELECT Volunteers.VolunteerID, StatusString =
CASE Volunteers.Status
WHEN 0 THEN 'INACTIVE'
WHEN 1 THEN 'ACTIVE'
ELSE 'OTHER'
END, ............
FROM ......
Upvotes: 1
Reputation: 491
This code should not compile at all:
Function Convert(ByRef e As Integer) As Integer
Select Case e
Case 0
e = "INACTIVE"
Case 1
e = "ACTIVE"
Case 2
e = "OTHER"
End Select
Return e
End Function
You are passing an integer byref...(why?)... and trying to assign it a string value and then return it. You want:
Function Convert(ByVal e As Integer) As String
Select Case e
Case 0
return "INACTIVE"
Case 1
return "ACTIVE"
Case 2
return "OTHER"
End Select
return String.Empty
End Function
Try this and see what you get. I suspect that the .CellFormatting event is probably not what you should be handling here, but give this a shot.
Upvotes: 1