How to hide a property on DataGrid

B

Boaz Ben-Porat

Is there an attribute for a property on an object that saus :"This property
is not shown on a DataGrid" ?

I know it can be done on the grid itself, using DataGridTableStyle,
DataGridColumnStyle etc. But I`d like to get the same result by changing my
class, not at run-time changing grid`s behaveiour.

A small example:

public class User
{
private string m_Uid;
private string m_Name;
private string m_Pwd;

// UID should be visible on a DataGrid
public string UID
{
get {return m_Uid;}
set {m_Uid = value;}
}

// Name should be visible on a DataGrid
public string Name
{
get {return m_Name;}
set {m_Uid = Name;}
}
// PWD should *NOT* be visible on a DataGrid
//is there an attribute for this ?

[SomeAttribute.PreventBindingToGrid(true)] // this is what I`m looking
for
public string PWD
{
get {return m_Pwd;}
set {m_Pwd = value;}
}
} // End of User.


On a form with a DataGrid - gridUsers, We implement (implementation not
shown) a method to fetch users into an ArrayList of User objects, and bind
gridUsers to the list:

ArrayList alUsers = FetchUsers();
gridUsers.DataSource = alUsers; // Currently, the grid has 3 columns,
including PWD.
Can this be done (prevent PWD property from binding) ?

TIA Boaz Ben-Porat
DataPharm a/s
Denmark
 
N

Nicholas Paldino [.NET/C# MVP]

Boaz,

Unfortunately not. You will have to change the DataGridColumnStyle (or
rather, remove it) as you stated.

Hope this helps.
 
M

M

I have this same issue. Can you demonstrate a way to do this cleanly?

I want to set security so that some people can view some fields, others
cannot. Hiding from the middle-tier would be cleanest, but you are saying
this cannot be done. Can you please provide a simple example of how to do
this your way?

Thank you!


Nicholas Paldino said:
Boaz,

Unfortunately not. You will have to change the DataGridColumnStyle (or
rather, remove it) as you stated.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- nick(dot)paldino=at=exisconsulting<dot>com

Boaz Ben-Porat said:
Is there an attribute for a property on an object that saus :"This property
is not shown on a DataGrid" ?

I know it can be done on the grid itself, using DataGridTableStyle,
DataGridColumnStyle etc. But I`d like to get the same result by changing my
class, not at run-time changing grid`s behaveiour.

A small example:

public class User
{
private string m_Uid;
private string m_Name;
private string m_Pwd;

// UID should be visible on a DataGrid
public string UID
{
get {return m_Uid;}
set {m_Uid = value;}
}

// Name should be visible on a DataGrid
public string Name
{
get {return m_Name;}
set {m_Uid = Name;}
}
// PWD should *NOT* be visible on a DataGrid
//is there an attribute for this ?

[SomeAttribute.PreventBindingToGrid(true)] // this is what I`m looking
for
public string PWD
{
get {return m_Pwd;}
set {m_Pwd = value;}
}
} // End of User.


On a form with a DataGrid - gridUsers, We implement (implementation not
shown) a method to fetch users into an ArrayList of User objects, and bind
gridUsers to the list:

ArrayList alUsers = FetchUsers();
gridUsers.DataSource = alUsers; // Currently, the grid has 3 columns,
including PWD.
Can this be done (prevent PWD property from binding) ?

TIA Boaz Ben-Porat
DataPharm a/s
Denmark
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

There is not such a feature.
You can implement it, though. Just create a custom Attribute to decorate
the class and in your new implementation of datagrid you can check for this
attribute and decide what to do.

Or just use the *Style properties of the standard grid.


Cheers,
 
I

Ignacio Machin

Hi M,

I think remember that a time ago in the MSDN magazine Dino Esposito
explained something similar, take a look at the msdn archives.
 

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