如何在DataGridView的整数单元格中显示字符串值?

时间:2014-02-20 15:17:38

标签: vb.net datagridview formatting

这段代码填充了数据库中的DataGridViewVolunteers.Status在数据库级别定义为Integer。

我想在状态列中显示字符串而不是整数,但是当我尝试将DataGridView1.Rows(row.Index).Cells(14).Value设置为字符串时,应用程序失败。

Error: INACTIVE is not a valid value for Int32

代码

        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

更新

然后我尝试了一个技巧,使这个列不可见,然后根据隐藏列添加一个额外的列。

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

此更改不会引发任何异常,但新列上的单元格为空。

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

此代码根本不应编译:

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

你传递一个整数byref ...(为什么?)...并试图给它分配一个字符串值,然后返回它。你想要:

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

试试这个,看看你得到了什么。我怀疑.CellFormatting事件可能不是你应该在这里处理的事情,但请试一试。

答案 1 :(得分:1)

我会更改sql语句以获取您需要的字符串,如下所示

SELECT  Volunteers.VolunteerID,  StatusString =
      CASE Volunteers.Status   
         WHEN 0 THEN 'INACTIVE'
         WHEN 1 THEN 'ACTIVE'
         ELSE  'OTHER'
      END, ............
FROM ......