How to Databind a CheckBoxAdv to Bit Field in SQL Database

29 May 20233 minutes to read

The CheckBoxAdv BoolValue property can be used to data bind to bit values as illustrated below.

public partial class Form1 : Form
{
    public static string dataBasePath = Path.GetFullPath("..\\..\\Database1.mdf");
    public  string connectString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=" + dataBasePath + ";Integrated Security=True";
    public Form1()
    {
        InitializeComponent();

        using (SqlConnection sqlConnection = new SqlConnection(connectString))
        {
            sqlConnection.Open();

            SqlDataAdapter dataAdapter = new SqlDataAdapter("SELECT * FROM [Table]", sqlConnection);

            DataTable dataTable = new DataTable("Table");
            dataAdapter.Fill(dataTable);

            dataGridView1.DataSource = dataTable;
            this.checkBoxAdv1.DataBindings.Add("BoolValue", dataTable, "CheckValue");
        }
    }
}
Public Partial Class Form1
    Inherits Form

    Public Shared dataBasePath As String = Path.GetFullPath("..\..\Database1.mdf")
    Public connectString As String = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=" & dataBasePath & ";Integrated Security=True"

    Public Sub New()
        InitializeComponent()

        Using sqlConnection As SqlConnection = New SqlConnection(connectString)
            sqlConnection.Open()
            Dim dataAdapter As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM [Table]", sqlConnection)
            Dim dataTable As DataTable = New DataTable("Table")
            dataAdapter.Fill(dataTable)
            dataGridView1.DataSource = dataTable
            Me.checkBoxAdv1.DataBindings.Add("BoolValue", dataTable, "CheckValue")
        End Using
    End Sub
End Class

CheckBox binding to Database in WindowsForms

View Sample in GitHub