Thursday 30 May 2013

How to get valuemember of display member in checkedlistbox c#

See more: C#
public void FillCheckedListBox1party()
            {
               try
                  {
                        if (con.State == ConnectionState.Closed)
                        {
                              con.Open();
                        }

                        string commandString = "SELECT NAME ,AC_CODE FROM AccountM where compcode='" + Compcls.Gcomp_cd + "'";
                 
                        DataTable accTable = new DataTable();
                        SqlCommand cmd = new SqlCommand(commandString, con);
                        SqlDataAdapter adpObj = new SqlDataAdapter(cmd);
                        accTable.TableName = "tbl";
                        adpObj.Fill(accTable);
                        con.Close();
                        checkedListBox1.DataSource = accTable;
                        checkedListBox1.ValueMember = "AC_CODE";
                        checkedListBox1.DisplayMember ="NAME";
                        checkedListBox1.BindingContext = new BindingContext();
                        // myChkListBox.BindingContext = new BindingContext();
                  }
                  catch (Exception ex)
                  {

                        throw ex;
                  }
                  finally
                  {
                        con.Close();
                  }
            }
 =================================>
 StringBuilder sb = new StringBuilder();
            for (int i = 0; i < ChkListBox.CheckedItems.Count; i++)
            {
                if (ChkListBox.CheckedItems.Count > 0)
                {
                    DataRow row;
                    row = ((DataRowView)this.ChkListBox.CheckedItems[i]).Row;
                   var val = (row[this.ChkListBox.ValueMember]).ToString();
                    row = null;

                    sb.AppendFormat("{0}, ", val.ToString());
                }
            }
            MessageBox.Show(sb.ToString().TrimEnd(',', ' '));







===================>

Ref: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/405e0178-bfb2-494e-ba80-f06f89038ac0

Saturday 18 May 2013


Step1:

class Employee
    {
        public string ID{ get; set; }
        public string Name{ get; set; }
    }
Step2: Place a checkedListBox on Form.
Step3:
public void bind()
        {
            List<Employee> lis = new List<Employee>();
            for (int i = 0; i < 10; i++)
            {
                Employee obj = new Employee();
                obj.ID = i.ToString();
                obj.Name = "Name:" + i;
                lis.Add(obj);
            }
            checkedListBox1.DataSource = lis.ToList<Employee>();
            checkedListBox1.DisplayMember = "Name";
            checkedListBox1.ValueMember = "ID";

        }

Above method call in Form_Load.

Find the Item checked or not.
checkedListBox1.SetItemChecked(5, true);
Find the checkedList Items:
  foreach (var item in checkedListBox1.CheckedItems)
            {
                var Emp = (Employee)item;
                MessageBox.Show(Emp.ID + ": " + Emp.Name);
            }

Thursday 16 May 2013

Winform : How to Databind CheckedListBox if it is missing DataSource

Winform : How toDatabind CheckedListBox if it is missing DataSource


When you decide to use CheckedListBox Control, you might expect to useDisplayMemberValueMember or DataSource property like you do for example with ListBox, but you will discover that the Intellisense is not showing those properties at all.
The good news is that even though Intellisense will not show them, the code will still compile when you type those properties by hand.
Now you might think problem solved. Well not exactly.
Microsoft is not recommending this approach. According to them, Data Binding is not supported on CheckedListBox but since the above properties are inherited from a base class, they can not be removed, so the Microsoft just hid them. 
So keep this in mind if you decide to use those properties on  CheckedListBox

How do you fill the control with items without using DataSource property?

One solution of course is to use Items.Add method to add individual item. If you already have an array or a list that contains items, you then use foreach loop and add every item with Items.Add like this:
string[] IceCreamFlavors = {"Banana", "Chocolate", "Pistachio", "Strawberry""Vanilla"};
foreach (var item in IceCreamFlavors)
{
  checkedListBox1.Items.Add(item);
}
But there is simpler way to add items from array or a List by using Items.AddRangemethod instead like this:
checkedListBox1.Items.AddRange(IceCreamFlavors);
If you want to add items from a List, just use ToArray method:
checkedListBox1.Items.AddRange(myList.ToArray());
I hope you found this article useful. Feel free to drop a comment or connect with this blog on the social networks.