databindings

  • Thread starter Thread starter frazer
  • Start date Start date
F

frazer

hi i have the following code in my project

ArrayList pictures = (new Pictures()).GetAllPictures();
allPictures.DataSource = pictures;
allPictures.DisplayMember = "pictures.ToString()"; //pictures.ToString
returns the names of the pictures.
allPictures.ValueMember = "pictures.Bitmap"; //this returns the
actual picture. and i get the foll error here



private void allPictures_SelectedValueChanged(object sender,
System.EventArgs e)
{
if (allPictures.SelectedIndex != -1)
pictureBox.Image = (Image)allPictures.SelectedValue; //this is used to
display the image in the picture box


}





System.ArgumentException: Cannot create a child list for field pictures.
at System.Windows.Forms.BindingContext.EnsureListManager(Object
dataSource, String dataMember)
at System.Windows.Forms.BindingContext.get_Item(Object dataSource, String
dataMember)
at System.Windows.Forms.ListControl.SetDataConnection(Object
newDataSource, BindingMemberInfo newDisplayMember, Boolean force)
at System.Windows.Forms.ListControl.set_ValueMember(String value)
at TreeViewImages.Form1.Form1_Load(Object sender, EventArgs e) in
form1.cs:line 314


what am i doing wrong.
 
Hi,

First of all, try to remove the "pictures." prefix from the DisplayMember
and the ValueMember initialization values. To the best of my knowledge, the
prefix is not necessary in your databinding scenario.
 
hi i have the following code in my project

ArrayList pictures = (new Pictures()).GetAllPictures();
allPictures.DataSource = pictures;
allPictures.DisplayMember = "pictures.ToString()"; //pictures.ToString
returns the names of the pictures.

You can't bind to a method, only to properties. You bind the
datasource pictures. In that datasource, objects are stored. for
DisplayMember you specify which property of THESE OBJECTS the control has
to bind to. If you picture object doesn't have a property 'Name', add it
and bind to that. Below, bind to Bitmap, not pictures.Bitmap.

FB
 
hi
that works fine.
now i want to display an image when the user clicks on combo box items.
i can do that but i have to cast it.

Picture p = (Picture)allPictures.SelectedItem;
this.pictureBox.Image = (Bitmap)(p.Bitmap);

this doesnt work.
this.pictureBox.Image = (Bitmap)allPictures.SelectedItem ;

shouldnt it cast it to a picture object automatically?
rather than me explicitly casting it?
 
frazer said:
that works fine.
now i want to display an image when the user clicks on combo box items.
i can do that but i have to cast it.

Picture p = (Picture)allPictures.SelectedItem;
this.pictureBox.Image = (Bitmap)(p.Bitmap);

this doesnt work.
this.pictureBox.Image = (Bitmap)allPictures.SelectedItem ;

shouldnt it cast it to a picture object automatically?
rather than me explicitly casting it?

What's the type of allPictures?

Either way, this should be the code, IF SelectedItem returns an
object of type 'Picture', that's why I asked what type allPictures is:

this.pictureBox.Image = (Bitmap)(allPictures.SelectedItem.Bitmap) ;

FB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top