Binding the control to a DataSource

Jan 8, 2009 at 11:58 AM
Hi,

I'm trying to make the control databound (having object DataSource and string DataMember properties and maybe a public RetrieveStructure() method).

Basically create the columns and fill rows dynamically and listening to ListChanged event when setting the DataSource property to an IBindingList implementation.

Creating the columns is ok but I cannot get it to display the rows correctly.

Also the ObjectComparer throws an exception because it is null.

I think having databinding functionality would make the control much more useful and was wondering if anyone had similar thoughts?

M
Coordinator
Jan 8, 2009 at 2:08 PM
Hi Michael,

Have you looked at the ClickAndQuery example, the ResultsControl takes a SQLCommand and generates the columns / rows - it might help to have a look at that. Failing that send me your code so far and I'll have a look for you. My email address is leea@binarycomponents_removethis_.com.

Regards
Lee
Jan 8, 2009 at 5:05 PM

Lee hi,

I had a look at the ClickAndQuery example it appears to be a bit complicated and when I ran it I kept getting a NullReferenceException _in ListSection where _rowInformation is null.

To get the DataSource and DataMember properties I tried adding something like this to ListControl.cs and then set the DataSource property to a IBindingList implementation but I couldn't get it to work.

Regs,
M

#region DataBinding

        private BindingSource _dataSource;

        public object DataSource
        {
            get { return _dataSource; }
            set
            {
                if (value != null)
                {
                    _dataSource = new BindingSource(value, _dataMember);
                    _dataSource.ListChanged += _dataSource_ListChanged;
                    RetreiveStructure();
                }
                else
                {
                    _dataSource = null;
                    //TODO: Clear colums and rows.
                }
            }
        }

        string _dataMember = string.Empty;
        public string DataMember
        {
            get { return _dataMember; }
            set
            {
                if (value != null && _dataMember != value)
                {
                    _dataMember = value;
                    RetreiveStructure();
                }
            }
        }

        private void _dataSource_ListChanged(object sender, ListChangedEventArgs e)
        {
            RetreiveStructure();
        }

        public void RetreiveStructure()
        {
            SuspendLayout();
            _dataSource.SuspendBinding();

            CreateColumns();
            AddRows();

            _dataSource.ResumeBinding();
            ResumeLayout();
        }

        void CreateColumns()
        {
            foreach (PropertyDescriptor property in _dataSource.GetItemProperties(null))
            {
                Columns.Add(new Column(property.DisplayName, property.Name, 0, null));
            }
        }

        void AddRows()
        {
            PropertyDescriptorCollection Descriptors = _dataSource.GetItemProperties(null);
            foreach (object row in _dataSource.List)
            {
                if (row is string)
                {
                    Items.Add(row);
                }
                else
                {
                    List<string> subItemArray = new List<string>();
                    foreach (PropertyDescriptor propertyDescriptor in Descriptors)
                    {
                        object value = propertyDescriptor.GetValue(row);
                        if (value != null) subItemArray.Add(value.ToString());
                        else subItemArray.Add(string.Empty);
                    }
                    Items.Add(subItemArray.ToArray());
                }
            }
        }

#endregion

Coordinator
Jan 8, 2009 at 5:10 PM
I'll have a little play with your code in the next day or so and get back to you. Could you email me your test application (Assuming you have one) so I can see the crash you get....

Cheers
Lee
Coordinator
Jan 15, 2009 at 10:00 AM
I sent you an email with some updated source code, I don't know if it got through so I'm leaving a message here.

Regards
Lee
Feb 6, 2009 at 3:56 PM

Cheers,

I found that in CreateColumns() using the lamda expression didn't work so I removed it.

foreach( PropertyDescriptor property in _dataSource.GetItemProperties( null ) )

{
 Columns.Add( new Column( property.DisplayName, property.Name, 60, rowItem => property.GetValue( rowItem ) ) );

}

i.e

foreach (PropertyDescriptor property in _dataSource.GetItemProperties(null))

{

 Columns.Add(new Column(property.DisplayName, property.Name, 60, property.GetValue));

}

Do you plan to add DataBinding funtionality to the SuperList?

Coordinator
Feb 6, 2009 at 4:36 PM
>Do you plan to add DataBinding funtionality to the SuperList?
Yep I reckon it would be nice to have - When I get a spare minute, hopefully in the next week or so. I have a couple of ideas that I want to chew on anyway, it's basically the next thing I'll do.

Regards
Lee
Feb 6, 2009 at 4:51 PM
Nice. 

I ask because what I'm trying to achieve is have the logic in the DataSource (which for me usually is an IList or IBindingList implementation) and seperated from the control yet have it respond to ListChanged and stuff.

More specifically and I don't know if this is possible at present with the SuperList but I 'm trying to get the list to display rows of a different colour conditioned on a property value of an item in the datasource so that when a property changes for the row colour to change accordingly?  Can I do this?

Cheers,

Michael

Coordinator
Feb 6, 2009 at 4:59 PM
Yeah you can but it doesn't come out of the box - there is an example in the test app under the Customise menu called "Toggle Row Painting Override" which shows you how to change the colour of the row. In your case you would access the data item or datasource object and change the colour accordingly.

Regards
Lee
Feb 6, 2009 at 5:39 PM
Kay, I can't see how I can get an individual row or where the colours would be defined.

wrt DataBinding it would be really nice to get the data item for each SuperList row via a property and something like a SuperList.GetRow() method that takes the data item and return its corresponding row.

Cheers

Michael
Apr 16, 2009 at 11:54 PM
Mike

I was wondering if you were able to have the data binding working with superlist. Would appreciate if you could share the working code please.

Regards
Anil
Coordinator
Apr 27, 2009 at 9:08 AM
Hi Anil,

Part of my next tranch of work for Superlist will be to add data binding support in - I'll be looking into it next week....

Regards
Lee
Jun 5, 2009 at 9:01 AM

Hi Lee,

tried out your Superlist today - my congratulation for this great job!

Did you succeed in your next tranch of work in the meanwhile - especially the Data Binding Support ?

Regards

Klaus

 

Coordinator
Jun 6, 2009 at 8:48 AM

Hi Klaus,

 

Thanks for the positive feedback - I haven't had a chance to look into the data binding stuff yet, I have a paying contract I need to clear off my plate at least the first milestone bit before I can look into anything else. It's the next thing I'll do though.

 

Cheers

Lee