none
Determine System.ComponentModel.Bindable property value of grid column RRS feed

  • Question

  • I am binding strongly typed objects to grids in my winforms application, making use of the System.ComponnetModel.Bindable property of the object properties to help control which object properties are visible in the grids and which arnt.

    Now I need to determine programatically which columns in a given grid have this property set to no, and which dont. How do I go about this?

    Apologies if I have put this in the wrong forum.

    Thanks.

    Sunday, February 27, 2011 8:12 AM

Answers

  • Hi,

     

    To determine whether a property of a class has BindableAttribute, you may want to use reflection, something like this:

     

    using System;

    using System.ComponentModel;

    using System.Windows.Forms;

    using System.Reflection;

     

    namespace WindowsFormsApplication1

    {

        public partial class Form1 : Form

        {

            public Form1()

            {

                InitializeComponent();

            }

     

            private void button1_Click(object sender, EventArgs e)

            {

                Test obj = new Test();

                Type t = obj.GetType();

     

                Console.WriteLine(string.Format("Type: {0}", t.Name));

     

                foreach (PropertyInfo pi in t.GetProperties())

                {

                    Console.WriteLine(string.Format("    Property: {0}", pi.Name));

                    foreach (object attri in pi.GetCustomAttributes(typeof(BindableAttribute), false))

                    {

                        BindableAttribute ba = attri as BindableAttribute;

                        if (ba != null)

                        {

                            Console.WriteLine(string.Format("        Bindable: {0}", ba.Bindable));

                        }

                        else

                        {

                            Console.WriteLine("No Bindable attribute found");

                        }

                    }

                }

            }

        }

     

        public class Test

        {

            [Bindable(true)]

            public int MyProperty

            {

                get

                {

                    // Insert code here.

                    return 0;

                }

                set

                {

                    // Insert code here.

                }

            }

     

            public int AnotherProperty

            {

                get

                {

                    // Insert code here.

                    return 0;

                }

                set

                {

                    // Insert code here.

                }

            }

        }

    }


    Eric Yang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by fweeee Monday, February 28, 2011 10:41 AM
    Monday, February 28, 2011 7:53 AM

All replies

  • Hi,

     

    To determine whether a property of a class has BindableAttribute, you may want to use reflection, something like this:

     

    using System;

    using System.ComponentModel;

    using System.Windows.Forms;

    using System.Reflection;

     

    namespace WindowsFormsApplication1

    {

        public partial class Form1 : Form

        {

            public Form1()

            {

                InitializeComponent();

            }

     

            private void button1_Click(object sender, EventArgs e)

            {

                Test obj = new Test();

                Type t = obj.GetType();

     

                Console.WriteLine(string.Format("Type: {0}", t.Name));

     

                foreach (PropertyInfo pi in t.GetProperties())

                {

                    Console.WriteLine(string.Format("    Property: {0}", pi.Name));

                    foreach (object attri in pi.GetCustomAttributes(typeof(BindableAttribute), false))

                    {

                        BindableAttribute ba = attri as BindableAttribute;

                        if (ba != null)

                        {

                            Console.WriteLine(string.Format("        Bindable: {0}", ba.Bindable));

                        }

                        else

                        {

                            Console.WriteLine("No Bindable attribute found");

                        }

                    }

                }

            }

        }

     

        public class Test

        {

            [Bindable(true)]

            public int MyProperty

            {

                get

                {

                    // Insert code here.

                    return 0;

                }

                set

                {

                    // Insert code here.

                }

            }

     

            public int AnotherProperty

            {

                get

                {

                    // Insert code here.

                    return 0;

                }

                set

                {

                    // Insert code here.

                }

            }

        }

    }


    Eric Yang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by fweeee Monday, February 28, 2011 10:41 AM
    Monday, February 28, 2011 7:53 AM
  • Thanks. That did the trick nicely.
    Monday, February 28, 2011 10:42 AM