ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Populate CheckedComboBox from Database using C# in Windows Application<p>Hey <a class="username" rel="PRA">PRA</a>,</p> <p>Please refer below sample.</p> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><span style="text-decoration: underline;"><strong>C#</strong></span></p> <pre class="brush: csharp">using System.Data.SqlClient; using System.Data;</pre> <p><strong><span style="text-decoration: underline;">Code</span></strong></p> <p><strong>C#</strong></p> <p><strong>CheckComboBoxItem.cs</strong></p> <pre class="brush: csharp">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 = &#34;&#34;; 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 &#34;Select Options&#34;; } } public partial class CheckComboBox : ComboBox { public CheckComboBox() { this.DrawMode = DrawMode.OwnerDrawFixed; this.DrawItem += new DrawItemEventHandler(CheckComboBox_DrawItem); this.SelectedIndexChanged += new EventHandler(CheckComboBox_SelectedIndexChanged); SelectedText = &#34;Select Options&#34;; } 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 &amp; DrawItemState.Focus) == 0, box.CheckState ? CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal); } public event EventHandler CheckStateChanged; }</pre> <p><strong>Form1.cs</strong></p> <pre class="brush: csharp">public Form1() { InitializeComponent(); string constr = @&#34;Data Source=.\SQL2005;Initial Catalog=Test;uid=user;pwd=password&#34;; using (SqlConnection con = new SqlConnection(constr)) { using (SqlDataAdapter sda = new SqlDataAdapter(&#34;SELECT CustomerId, Name,Country FROM Customers&#34;, con)) { DataTable dt = new DataTable(); sda.Fill(dt); for (int i = 0; i &lt; dt.Rows.Count; i++) { checkComboBox1.Items.Add(new CheckComboBox.CheckComboBoxItem(dt.Rows[i][&#34;CustomerId&#34;].ToString() + &#34; &#34; + dt.Rows[i][&#34;Name&#34;].ToString() + &#34; &#34; + dt.Rows[i][&#34;Country&#34;].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 &#34;CustomerId&#34;: checkBox1.Checked = item.CheckState; break; case &#34;Name&#34;: checkBox2.Checked = item.CheckState; break; case &#34;Country&#34;: checkBox3.Checked = item.CheckState; break; } } }</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://imgur.com/HeDU2ko.jpg" alt="" width="251" height="131" /></p>https://www.aspforums.net:443/Threads/194793/Populate-CheckedComboBox-from-Database-using-C-in-Windows-Application/https://www.aspforums.net:443/Threads/194793/Populate-CheckedComboBox-from-Database-using-C-in-Windows-Application/Tue, 08 Jan 2019 07:05:05 GMT