0

In my database, I have stored Male / Female as 1 / 0 for Gender. Now I have to show Gender values to the user as Male / Female.
How can I do that in a DataGridView?

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Try
        Dim gender_ As Int16
        If (rbMale.Checked = True) Then
            gender_ = 1
        Else
            gender_ = 0
        End If
        conn_.Open()
        Dim command As New SqlCommand("INSERT INTO STUDENT (STUDENTNAME, CONTACT, GENDER) VALUES (@NAME, @CONTACT, @GENDER)", conn_)
        command.Parameters.AddWithValue("@NAME", tb1.Text)
        command.Parameters.AddWithValue("@CONTACT", tb2.Text)
        command.Parameters.AddWithValue("@GENDER", gender_)
        command.Connection = conn_
        command.ExecuteNonQuery()
        MsgBox("Record saved.", MsgBoxStyle.Information)

        LoadData()
        conn_.Close()

    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical)
    End Try
End Sub

Private Sub LoadData()
    Try
        Dim command As New SqlCommand("SELECT STUDENTNAME, CONTACT, GENDER FROM STUDENT", conn_)
        Dim dataAdapter_ As New SqlDataAdapter(command)

        Dim dt_ As New DataTable

        dataAdapter_.Fill(dt_)

        dgv_student.DataSource = dt_
    Catch ex As Exception

    End Try
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    rbMale.Checked = True
    LoadData()
End Sub
Jimi
  • 29,621
  • 8
  • 43
  • 61
nischalinn
  • 1,133
  • 2
  • 12
  • 38
  • I would suggest that you should have a `Gender` table and store a `GenderId` in your `Student` table. You can then just use a `DataGridViewComboBox` column to display the appropriate text like you would for any other related table. – user18387401 Mar 20 '22 at 01:41
  • It sounds like you need a `DataGridViewComboBoxColumn` but this is not clear. What have you tried as there are many examples of how to implement the combo boxes as you describe? Is there any particular part of setting up the combo box column that is not working as you expect? In other words, I see nothing in the posted code that attempts to create this “Male/Female” selection/combo box/option in the grid. – JohnG Mar 20 '22 at 06:06
  • [Show ComboBox for bool and Enum columns in DataGridView](https://stackoverflow.com/a/40843518/3110834) – Reza Aghaei Mar 21 '22 at 23:56

2 Answers2

1

Because the requirement of the DataGridView is simply to display read only values, I don't think a combo box is required. You just need to modify the query from:

SqlCommand("SELECT STUDENTNAME, CONTACT, GENDER FROM STUDENT", conn_)

to:

SqlCommand("SELECT STUDENTNAME, CONTACT, 
CASE 
    WHEN GENDER=0 THEN 'Female'
    WHEN GENDER=1 THEN 'Male'
    ELSE 'Unknown'
END AS GENDER
FROM STUDENT", conn_)

to display the literal values.

Catherine
  • 163
  • 6
  • That's fine if you're just displaying data but if you're adding or editing data it doesn't really help. The `ComboBox` will allow you to select an appropriate value in those cases. – user18387401 Apr 06 '22 at 08:01
-1

I think that you should have a related table, as I mentioned in a comment, but if you don't, you can still use a combo box column and bind a local list for the text to be displayed. You can do something like this:

Dim genders = {New With {.Value = CShort(0), .Name = "Female"},
               New With {.Value = CShort(1), .Name = "Male"}}

With genderColumn
    .DisplayMember = "Name"
    .ValueMember = "Value"
    .DataSource = genders
End With

This assumes that genderColumn is a DataGridViewComboBoxColumn that the GENDER column of your STUDENT table is bound to.

user18387401
  • 2,514
  • 1
  • 3
  • 8