Need an attribute to hide a field when databinding

M

moondaddy

using c# 3.5 I have list of business objects which I will use in lists for
databinding and I want to hide some of the fields so they don't show up in
the list control.

some of my list will be:
List<someClass> list
Observable collections
and
a custom list derived from the BindingList.

[someAttribute]
public int SomeField
{
get...
set...
}

Thanks.
 
Z

Zhi-Xin Ye [MSFT]

Dear moondaddy,

As I understand, you have a list of business objects, when performing data
binding, you want some of the properties in the custom class not be
displayed in the list control.

You can use the Browsable attribute, setting it to false would make the
property hidden when performing data binding. e.g.

============== Code Sample ===============

class Customer
{
private int id;
private string name;
private string coutry;

public Customer(int id, string name, string coutry)
{
this.id = id;
this.name = name;
this.coutry = coutry;
}

public int ID
{
get { return id; }
set { this.id = value; }
}

[Browsable(false)]
public string Name
{
get { return name; }
set { this.name = value; }
}

public string Country
{
get { return coutry; }
set { this.coutry = value; }
}
}

private void Form2_Load(object sender, EventArgs e)
{
List<Customer> arr = new List<Customer>();
arr.Add(new Customer(1, "Zhang", "China"));
arr.Add(new Customer(2, "John", "US"));
arr.Add(new Customer(3, "Ricky", "UK"));
arr.Add(new Customer(4, "Hkjer", "UK"));
arr.Add(new Customer(5, "Rbtrw", "DE"));
arr.Add(new Customer(6, "Ricky", "AU"));
arr.Add(new Customer(7, "Griao", "FR"));

this.dataGridView1.DataSource = arr;
}

=============================================

When running the code, you'll find the "Name" property is not displayed in
the DataGridView.

Should you have any questions, please feel free to let me know.

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Marc Gravell

For the UI: [Browsable(false)]

For intellisense: [EditorBrowsable(blah.Never)]

Marc
 
M

moondaddy

Thanks,

I tested this and it did work in a winforms app, but doesnt work in a wpf
app. I should have specified that I was working in wpf, but I didnt think
it made a difference until now. Do you know what I need to do to make this
work in wpf?


Zhi-Xin Ye said:
Dear moondaddy,

As I understand, you have a list of business objects, when performing data
binding, you want some of the properties in the custom class not be
displayed in the list control.

You can use the Browsable attribute, setting it to false would make the
property hidden when performing data binding. e.g.

============== Code Sample ===============

class Customer
{
private int id;
private string name;
private string coutry;

public Customer(int id, string name, string coutry)
{
this.id = id;
this.name = name;
this.coutry = coutry;
}

public int ID
{
get { return id; }
set { this.id = value; }
}

[Browsable(false)]
public string Name
{
get { return name; }
set { this.name = value; }
}

public string Country
{
get { return coutry; }
set { this.coutry = value; }
}
}

private void Form2_Load(object sender, EventArgs e)
{
List<Customer> arr = new List<Customer>();
arr.Add(new Customer(1, "Zhang", "China"));
arr.Add(new Customer(2, "John", "US"));
arr.Add(new Customer(3, "Ricky", "UK"));
arr.Add(new Customer(4, "Hkjer", "UK"));
arr.Add(new Customer(5, "Rbtrw", "DE"));
arr.Add(new Customer(6, "Ricky", "AU"));
arr.Add(new Customer(7, "Griao", "FR"));

this.dataGridView1.DataSource = arr;
}

=============================================

When running the code, you'll find the "Name" property is not displayed in
the DataGridView.

Should you have any questions, please feel free to let me know.

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
 
Z

Zhi-Xin Ye [MSFT]

Dear moondaddy,

As far as I know there is not such an attribute in WPF. However, if you
don't want to show the field in the control, you can just don't assign that
field for the DisplayMemberBinding or DisplayMemberPath property. Could you
please explain your scenario in more details?

I look forward to your reply.

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

moondaddy

DisplayMemberBinding or DisplayMemberPath properties don't apply here. I
have a grid (in this case its a grid from Infragistics) and set its data
source like this:

grid.DataSource = myList;

and I waned to use an attribute to hide some of the props in the business
objects in myList. I even tried the observablecollection with no luck.
 
Z

Zhi-Xin Ye [MSFT]

Dear moondaddy,

I'm not familiar with the infragistics DataGrid, but as far as I know
there's no such attribute in WPF. Would you mind trying in another way? For
example, you can hide the fields which you don't need from the grid. I
find some threads on the internet on how to hide a field from the
Infragistics DataGrid.

Threads for your information:

Using xamdatagrid to hide columns programmically
http://forums.infragistics.com/forums/p/8493/33545.aspx

how to control columns display when displaying hierarchical data using
XamDataGrid
http://forums.infragistics.com/forums/p/2568/15732.aspx

Should you have any questions, please don't hesitate to let me know.

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

moondaddy

Thanks,

Yes I can write code to hide those columns in the grid, but I was hoping I
could use an attribute. I codegen my custom list classes and it's real easy
to assign attributes in the code generator. if I know ahead of time what
properties I don't want to be visible in the UI, I can just assign the
attribute to the property or field one time and then when every the business
object is used in a list, I only see what I want to see. This greatly
simplifies things.

Thanks again.
 
Z

Zhi-Xin Ye [MSFT]

Dear moondaddy,

Yes I know it would make things easy if we had an attribute to hide the
field when binding, I would suggest you post this feature as a suggestion
to our Connect feedback portal. Our developer
will evaluate it seriously and take them into consideration when designing
furture release of the product.

Here is the link for the Connect feedback portal:

http://connect.microsoft.com/VisualStudio/

For the time being, you can write code to hide the columns in the grid,
sorry for any inconvenience this may cause.

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can

improve the support we provide to you. Please feel free to let my manager
know what you think of the level

of service provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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

Top