Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hi,

I have a datagridview that has a column for a checkbox, but when selected its showing the following code
System.Windows.Forms.CheckBox, CheckState: 1
Rather than just the check, and if Check = Yes, and Non-Check = No.

How can i change it to show yes and no, rather than checkstate:1

Code:

VB
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    table.Rows.Add(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, ComboBox1.SelectedItem.ToString(), DateTimePicker1.Text, Label8.Text, PictureBox1.Image, CheckBox1.ToString)
    DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
    DataGridView1.RowTemplate.Height = 100
    DataGridView1.AllowUserToAddRows = False
    DataGridView1.DataSource = table


What I have tried:

Searched forums, but doesn't match to the way i'm declaring columns as its outside database.
Posted
Updated 17-Apr-17 6:20am

I think the easiest way is to change the source to output Yes/No instead of true/false. But, if that not doable, you also can write code to manipulate the DataTable(I'm assuming table = DataTable). Here is an example, is not type safe, but you can enhance it. The below function will change the column boolean type to string and its value to Yes/No.

VB
Public Shared Function ChangeColumnDataType(ByRef table As DataTable, columnname As String, newtype As Type) As Boolean
    If table.Columns.Contains(columnname) = False Then
        Return False
    End If

    Dim column As DataColumn = table.Columns(columnname)
    If column.DataType = newtype Then
        Return True
    End If

    Try
        Dim newcolumn As New DataColumn("temporary", newtype)
        table.Columns.Add(newcolumn)
        For Each row As DataRow In table.Rows
            Try
                row("temporary") = Convert.ChangeType(row(columnname), newtype)
                row("temporary") = If(CBool(row(columnname)), "Yes", "No")
            Catch
            End Try
        Next
        table.Columns.Remove(columnname)
        newcolumn.ColumnName = columnname
    Catch generatedExceptionName As Exception
        Return False
    End Try

    Return True
End Function

Then call the ChangeColumnDataType function before the DataSource binding. I'm assuming the column name is "Active"
VB
ChangeColumnDataType(table, "Active", GetType(String))
DataGridView1.DataSource = table

Source:
c# - Changing populated DataTable column data types - Stack Overflow[^]
 
Share this answer
 
Part of your problem is that you are converting the object CheckBox1 to a string rather than it's value.

There are several ways to do this properly:

1. CheckBox1.CheckState.ToString() will give you "Checked" / "Unchecked"

2. CheckBox1.Checked.ToString() will give you "True" / "False"

3. IIf(CheckBox1.Checked, "Yes", "No") will give you "Yes" / "No"
 
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