Showing posts with label FillCheckedList Binding. Show all posts
Showing posts with label FillCheckedList Binding. Show all posts

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