Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In addition to my previous answer, since you prefer to indicate not to add the columns manually, I suggest you another option: <strong>using custom attributes in your properties definition</strong>.</p> <p>First, you have to code your custom attribute:</p> <p><strong>MyPropertyAttribute class</strong></p> <pre><code>[AttributeUsage(AttributeTargets.Property)] public class MyPropertyAttribute : Attribute { public enum VisibilityOptions { visible, invisible } private VisibilityOptions visibility = VisibilityOptions.visible; public MyPropertyAttribute(VisibilityOptions visibility) { this.visibility = visibility; } public VisibilityOptions Visibility { get { return visibility; } set { visibility = value; } } } </code></pre> <p>You could use it in your classes, just like this:</p> <p><strong>Foo class</strong></p> <pre><code>public class Foo { private string name; private string surname; [MyPropertyAttribute(MyPropertyAttribute.VisibilityOptions.visible)] public string Name { get { return name; } set { name = value; } } [MyPropertyAttribute(MyPropertyAttribute.VisibilityOptions.invisible)] public string Surname { get { return surname; } set { surname = value; } } } </code></pre> <p>You could write a method, that iterates the properties in your binded objects, using reflection, and test if they are marked as visible or invisible, in order to add or don´t add columns. You could even have a custom DataGridView with this behavior, so you don´t have to repeat this everytime. You´ll only to use your custom DataGridView, and mark the visibility in the properties.</p> <p>Something like this...</p> <pre><code>public class MyCustomDataGridView : DataGridView { public MyCustomDataGridView() { this.AutoGenerateColumns = false; } public void Load&lt;T&gt;(ICollection&lt;T&gt; collection) { foreach(object myAttribute in typeof(T).GetCustomAttributes(typeof(MyPropertyAttribute).GetType(), true)) { if (((MyPropertyAttribute)myAttribute).Visibility == MyPropertyAttribute.VisibilityOptions.visible) { //... } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload