Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CheckedListBox Bound to Object - Problem

Status
Not open for further replies.

eb24

Programmer
Dec 17, 2003
240
US
I am trying to bind a CheckedListBox to an Object and when I execute the code, no data is displayed nor I get any errors.

This is what I have in my Type.cs file:
Code:
private void LoadTypes()
{
  CheckedListBox clb = new CheckedListBox();
  clb.Name = "clbType";
  clb.Sorted = true;
  clb.DataSource = _Types; //Object DataSource Collection
  clb.DisplayMember = "Type_Name"; 
  clb.ValueMember = "TypeID"; 
  clb.CheckOnClick = true;
  clb.BindingContext = new BindingContext();
  for (int i = 0; i < clb.Items.Count; i++)
  {
    if (i % 2 == 0)
    {
      clb.SetItemCheckState(i, CheckState.Checked);
    }
  }
  this.Controls.Add(clb);
}
And this is what is in my Type.Designer.cs file:
Code:
this.clbType.FormattingEnabled = true;
this.clbType.Location = new System.Drawing.Point(149, 187);
this.clbType.Name = "clbType";
this.clbType.Size = new System.Drawing.Size(120, 94);
this.clbType.TabIndex = 42;
 
the 2 are not related: this.clbType is a different object then clb. try this instead.
Code:
private void LoadTypes()
{
  this.clbType.Name = "clbType";
  this.clbType.Sorted = true;
  this.clbType.DataSource = _Types; //Object DataSource Collection
  this.clbType.DisplayMember = "Type_Name"; 
  this.clbType.ValueMember = "TypeID"; 
  this.clbType.CheckOnClick = true;
  this.clbType.BindingContext = new BindingContext();
  for (int i = 0; i < this.clbType.Items.Count; i++)
  {
    if (i % 2 == 0)
    {
      this.clbType.SetItemCheckState(i, CheckState.Checked);
    }
  }
}
I haven't designed desktop apps so I'm not sure what [tt]this.clbType.BindingContext = new BindingContext();[/tt] is for. chances are you could also have an issue here.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Well, I changed all of the "clb" to "clbType" and that doesn't do anything. Any other suggestions?
 
Jason:

Why would I use the BindingContext if I'm only binding to one control? As per the link you sent me: "... used to return a single BindingManagerBase for all data-bound controls contained by the Control". Agree?
Code:
private void GetTypes()
{
   TypeController ctr = new TypeController();

   _types = null;
   _types = ctr.Retrieve();

   clbType.DataSource = _types;
   clbType.DisplayMember = "TypeName";
   clbType.ValueMember = "TypeID";
   clbType.CheckOnClick = true;
            
   for (int i = 0; i < clbType.Items.Count; i++)
   {
      clbType.SetItemCheckState(i, CheckState.Checked);
   }
}
This is what I have so far...
 
eb24 - can you explain what it is you are trying to do? What is it you are trying to show in the checkedlistbox? To databind an object to a control the normal process is to set a bindingsource up in your project with the object as the data source. You then use the bindingsource to bind to the control - all of this can be done through the visual designer. If you let us know what you are trying to do I'll see if I can get some example code to you.
 
Thanks - things have changed a bit, but this is what I would like seen done:

I have a C# 2.0 Windows Form where one of the controls is a CheckedBoxList. The list should display all of the values from a table, e.g. tblTypes. When the form loads, I would like this ChechedBoxList to have some of the CheckBoxes 'checked' depending if that record has any of the Types linked to it. So from the main table, tblMain, there is a one-to-many and then a many-to-one relationship.

[tlbMain] 1----* [tblMainTypes] *----1 [tblTypes]

So, let's say, tblMain's PK is MainID and tblTypes's PK is TypeID. Then for record MainID = 5, it will, for our example, be in tblMainTypes like this:

------------------
tblMainTypes
------------------
MainTypeID MainID TypeID
1 5 3
2 5 8
3 5 11

So, given this, my CheckedBoxList should display all of the values from tblTypes, say DisplayMembers 1-12, and have only 3 values checked, i.e. those found in the tblMainTypes.

Does this help? From my previous post, I am trying to loop through the values and find the checked ones. Oh, I should probably mention that I am binding everything to objects and the DAL is separated - the pertinent Oracle Packages are separated, so tblTypes's Procedures are separated from tblMainTypes's Procedures. All of these Procedures were code-generated, so all I have to play with are stuff like SELECT_ALL, SELECT_ALL_ID, etc.

Hopefully I haven't caused more confusion. Thanks in advance for any assistance.
 
Ok so you're getting a list of values out of a database? I would be inclined to putting the results of the db query into a BindingList of objects eg:

BindingList<Record> records = new BindingList<Record>

where the Record object would something like:

class Record
{
private int mainTypeId
private bool typeMatch = false;

public int MainTypeId
{
get...
set...
}

public bool TypeMatch
{
get...
set...
}

}

You could then bind the bindinglist to the checklistbox control where TypeMatch would determine the state of the checkbox. This suggestion won't be exactly what you need but I'm hoping it might help! Sorry if I have misunderstood.....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top