Populate CheckedComboBox from Database using C# in Windows Application

Last Reply 12 days ago By pandeyism

Posted 12 days ago

Hi! I used check combo box, but couldn’t fill it from database in C#.

Adding CheckBox to ComboBox in Windows Application

InitializeComponent();
checkComboBox1.ValueMember = "Id";
checkComboBox1.DisplayMember = "NAMEBOL";
checkComboBox1.DataSource = cash.getNameUnderBolDep(cmbUndep.SelectedValue.ToString());
public DataTable getNameUnderBolDep(string id)
{
    DataTable dt = new DataTable();
    SqlCommand cmd = con.CreateCommand();
    cmd.CommandText = "select Id, NAMEBOL from MANUALSICK where IdUndep = '" + id + "'";
    con.Open();
    try
    {
        dt.Load(cmd.ExecuteReader());
    }
    catch { }
    con.Close();
    return dt;
}

 

Posted 12 days ago

Hey PRA,

Please refer below sample.

Namespaces

C#

using System.Data.SqlClient;
using System.Data;

Code

C#

CheckComboBoxItem.cs

public class CheckComboBoxItem
{
    public CheckComboBoxItem(string text, bool initialCheckState)
    {
        _checkState = initialCheckState;
        _text = text;
    }

    private bool _checkState = false;
    public bool CheckState
    {
        get { return _checkState; }
        set { _checkState = value; }
    }

    private string _text = "";
    public string Text
    {
        get { return _text; }
        set { _text = value; }
    }

    private object _tag = null;
    public object Tag
    {
        get { return _tag; }
        set { _tag = value; }
    }
    public override string ToString()
    {
        return "Select Options";
    }
}

public partial class CheckComboBox : ComboBox
{
    public CheckComboBox()
    {
        this.DrawMode = DrawMode.OwnerDrawFixed;
        this.DrawItem += new DrawItemEventHandler(CheckComboBox_DrawItem);
        this.SelectedIndexChanged += new EventHandler(CheckComboBox_SelectedIndexChanged);
        SelectedText = "Select Options";
    }

    void CheckComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        CheckComboBoxItem item = (CheckComboBoxItem)SelectedItem;
        item.CheckState = !item.CheckState;
        if (CheckStateChanged != null)
            CheckStateChanged(item, e);
    }

    void CheckComboBox_DrawItem(object sender, DrawItemEventArgs e)
    {
        if (e.Index == -1)
        {
            return;
        }

        if (!(Items[e.Index] is CheckComboBoxItem))
        {
            e.Graphics.DrawString(Items[e.Index].ToString(), this.Font, Brushes.Black, new Point(e.Bounds.X, e.Bounds.Y));
            return;
        }

        CheckComboBoxItem box = (CheckComboBoxItem)Items[e.Index];
        CheckBoxRenderer.RenderMatchingApplicationState = true;
        CheckBoxRenderer.DrawCheckBox(e.Graphics, new Point(e.Bounds.X, e.Bounds.Y), e.Bounds, box.Text, this.Font, (e.State & DrawItemState.Focus) == 0, box.CheckState ? CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal);
    }
    public event EventHandler CheckStateChanged;

}

Form1.cs

public Form1()
{
    InitializeComponent();
    string constr = @"Data Source=.\SQL2005;Initial Catalog=Test;uid=user;pwd=password";
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter("SELECT CustomerId, Name,Country FROM Customers", con))
        {
            DataTable dt = new DataTable();
            sda.Fill(dt);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                checkComboBox1.Items.Add(new CheckComboBox.CheckComboBoxItem(dt.Rows[i]["CustomerId"].ToString() + " " + dt.Rows[i]["Name"].ToString() + " " + dt.Rows[i]["Country"].ToString(), true));
                checkBox1.Visible = false;
                checkBox2.Visible = false;
                checkBox3.Visible = false;
                this.checkComboBox1.CheckStateChanged += new System.EventHandler(this.checkComboBox1_CheckStateChanged);
            }
        }
    }
}

private void checkComboBox1_CheckStateChanged(object sender, EventArgs e)
{
    if (sender is CheckComboBox.CheckComboBoxItem)
    {
        CheckComboBox.CheckComboBoxItem item = (CheckComboBox.CheckComboBoxItem)sender;
        switch (item.Text)
        {
            case "CustomerId":
                checkBox1.Checked = item.CheckState;
                break;
            case "Name":
                checkBox2.Checked = item.CheckState;
                break;
            case "Country":
                checkBox3.Checked = item.CheckState;
                break;
        }
    }
}

Screenshot