Wednesday, August 13, 2008

Set DisplayName to a property to shown in a datagridview

We have a class or a structure which contains few properties which are bound to a
datagrid.

The example class is like this:

public class Details
{
private string vaultName;
private bool isBackUp;
private bool isArchive;
private string vaultLocation;


public bool IsBackUp
{
get { return isBackUp; }
set { isBackUp = value; }
}
public string VaultName
{
get { return vaultName; }
set { vaultName = value; }
}
public bool IsArchive
{
get { return isArchive; }
set { isArchive = value; }
}
public string VaultLocation
{
get { return vaultLocation; }
set { vaultLocation = value; }
}
}
The datagrid when bounded look like this:


That contains just the property names as the column headers which are not meaningfull to users.

So if we want to show other names , we can just add DisplayName attribute to the
property.Just like this:

public class Details
{
private string vaultName;
private bool isBackUp;
private bool isArchive;
private string vaultLocation;

[DisplayName("Add")]
public bool IsBackUp
{
get { return isBackUp; }
set { isBackUp = value; }
}
public string VaultName
{
get { return vaultName; }
set { vaultName = value; }
}
[DisplayName("Add Repository")]
public bool IsArchive
{
get { return isArchive; }
set { isArchive = value; }
}
public string VaultLocation
{
get { return vaultLocation; }
set { vaultLocation = value; }
}
}
Now the datagridview look like this:


Ajith