简体   繁体   中英

How can I iterate through a BindingSource items in Compact Framework c#

I hope there is somewhere a guru that can help me out on this.

I am creating a custom control for a windows ce mobile device. I created a property so that the user can bind any source to my control. I need to iterate through the source and get the item value for the display member path.

I declare my source and then create the property. In the construct I have an event registered if the source change so that I can start painting my object again. This is my first time working with binding source from this angle. Am I missing something or using it wrong? Here is some code snippets

private BindingSource _bindingCollection;

public object DataSource
        {
            set { _bindingCollection.DataSource = value; }
        }
public string DisplayMemberPath { get; set; }

ctor()
{
_bindingCollection.DataSourceChanged += _bindingCollection_DataSourceChanged;
}


void _bindingCollection_DataSourceChanged(object sender, EventArgs e)

        {
            foreach (var item in _bindingCollection)
            {
                //I am stuck here on getting the value from the item that is
                // specified by DisplayMemberPath to paint my objects on the control
                //I can only paint on the control and cannot add any controls to it.
                //So here a method is called with the value as a parameter and the 
                //Graphics object will draw the string
            }
        }

Thanks in advanced.

Regards Riaan

EDIT:

Dude I know you use on paint method to paint, this is an example of what I want to do. I create everything from scratch, the entire control is drawn from code and I override all the needed events. Maybe set the _bindingCollection.DataMember in the DisplayMemberPath? Will that then give the item when iterating through the source? Will test now and post answer if it works. Please no more comments or answers telling me to use on paint or something like that. Concentrate on question of getting the display member value from the collection :)

Edit: Found the answer Ok sorry this was actually a really stupid question that I asked while being busy with a lot of things. This was actually easy to get the property value.

        for (int index = 0; index < _bindingCollection.Count; index++)
        {
            var propertyValue = _bindingCollection[index].GetType().GetProperty(DisplayMemberPath ).GetValue(_bindingCollection[index], null);
            // Can do anthing now with the value here or just create a 
            //method of returning this value with this line of code on top.

        }

The question can be deleted if someone needs to delete it. I will leave it here if there is anyone else to much in a hurry for this solution and do not know how to get it. These are just code snippets and not in anyway methods that can be used.

I am just marking the question as answered. Read my edits to see my stupid thinking mistake and why it was easy.

I will leave the post for people that may go and think the same as me and struggle unneccarily unless someone feels the question must be deleted you can go right ahead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM