Skip to main content

Classes and Property Grid

The property grid is fairly easy to use.The hard part is making the class that you want to display in the grid "Property Grid Friendly".

1. 
Create public properties for fields you want to expose.  All properties should have get and set methods.(If you don't have a get method, the property won't show up in the PropertyGrid).
2. 
System.ComponentModel namespace have the following attribute
 a. 
CategoryAttribute: This attribute places your property in the appropriate category in a node on the property grid.
 b. 
DescriptionAttribute: This attribute places a description of your property at the bottom of the property grid
 c. 
BrowsableAttribut: This is used to determine whether or not the property is shown or hidden in the property grid
 d. 
ReadOnlyAttribute: Use this attribute to make your property read only inside the property grid
 e.
DefaultValueAttribute: Specifies the default value of the property shown in the property grid
 f: 
DefaultPropertyAttribute:If placed above a property, this property gets the focus when the property grid is first launched. Unlike the other attributes, this attribute goes above the class.


eg: Customer Class: 
we simply create the customer class with private fields and public properties that access these fields. Attributes are placed above the properties to ready each property for the property grid.
using System.ComponentModel;
[DefaultPropertyAttribute("Name")]public class Customer
{
private string _name;private int _age;private DateTime _dateOfBirth;private string _SSN;private string _address;private string _email;private bool _frequentBuyer; // Name property with category attribute and // description attribute added [CategoryAttribute("ID Settings"), DescriptionAttribute("Name of the customer")]
public
 string Name
{
get{return _name;
set
{
_name = 
value;
}
}
[CategoryAttribute("ID Settings"),
DescriptionAttribute("Social Security Number of the customer")]
public string SSN
{
get{return _SSN;
set{
_SSN = 
value;
}
}
[CategoryAttribute("ID Settings"),
DescriptionAttribute("Address of the customer")]
public string Address
{
get{return _address;
}
set{
_address = 
value;
}
}
[CategoryAttribute("ID Settings"),
DescriptionAttribute("Date of Birth of the Customer (optional)")]
public DateTime DateOfBirth
{
get{return _dateOfBirth;
set{
_dateOfBirth = 
value;
}
}
[CategoryAttribute("ID Settings"), DescriptionAttribute("Age of the customer")]

public
 int Age
{
get{return _age;
set{
_age = 
value;
}
}
[CategoryAttribute("Marketting Settings"), DescriptionAttribute("If the customer as bought more than 10 times, 
this is set to true")]public bool FrequentBuyer
{
get{return _frequentBuyer;
set{
_frequentBuyer = 
value;
}
}
[CategoryAttribute("Marketting Settings"), DescriptionAttribute("Most current e-mail of the customer")] 
public string Email
{
get{return _email;
set{
_email = 
value;
}
}
public Customer()
{
}
}


Assigning the PropertyGrid an Object
All that is left to do to get the grid running is assign an instance of our customer class to the property grid.The property grid will automatically figure out all the fields of the customer through reflection and display the property name along with the property value on each line of the grid.  Another nice feature of the property grid is it will create special editing controls on each line that correspond to the value type on that line.For example, a Date of Birth property (of type DateTime) of the customer will allow you to edit the value of the the date with the calendar control.  Booleans can be edited with a combo box showing True or False (saves you from excess typing).
created a new customer and populated it with values through its properties.We then use the SelectObject property of the PropertyGrid and assign our customer object to this property.Upon assigning the customer to the grid, the grid will display all of the public properties we defined in our Customer class. 
private void Form1_Load(object sender, System.EventArgs e)
{
// Create the customer object we want to displayCustomer bill = new Customer();// Assign values to the propertiesbill.Age = 50;
bill.Address = " 114 Maple Drive ";
bill.DateOfBirth = Convert.ToDateTime(" 9/14/78");
bill.SSN = "123-345-3566";
bill.Email = bill@aol.com;
bill.Name = "Bill Smith"; 
// Sets the the grid with the customer instance to be// browsedpropertyGrid1.SelectedObject = bill;
}

Comments

Popular posts from this blog

JavaScript Interview Questions

This is a compilations of all the interview questions related to Javascript that i have encountered.  Q: Difference between window.onload and onDocumentReady? A: The onload event does not fire until every last piece of the page is loaded, this includes css and images, which means there’s a huge delay before any code is executed. That isnt what we want. We just want to wait until the DOM is loaded and is able to be manipulated. onDocumentReady allows the programmer to do that. Q:  What is the difference between == and === ? A: The == checks for value equality, but === checks for both type and value. Few examples: "1" == 1; // value evaluation only, yields true "1" === 1; // value and type evaluation, yields false "1" == true; // "1" as boolean is true, value evaluation only, yields true "1" === false; // value and type evaluation, yields false Q: What does “1″+2+5 evaluate to? What about 5 + 2 +...

Adding a linked Server using the GUI

Adding a linked Server using the GUI There are two ways to add another SQL Server as a linked server.  Using the first method, you need to specify the actual server name as the “linked server name”.  What this means is that everytime you want to reference the linked server in code, you will use the remote server’s name.  This may not be beneficial because if the linked server’s name changes, then you will have to also change all the code that references the linked server.  I like to avoid this method even though it is easier to initially setup.  The rest of the steps will guide you through setting up a linked server with a custom name: To add a linked server using SSMS (SQL Server Management Studio), open the server you want to create a link from in object explorer. In SSMS, Expand Server Objects -> Linked Servers -> (Right click on the Linked Server Folder and select “New Linked Server”) Add New Linked Server The “New Linked Server” Dialog a...

Lookup!!

LOOKUP As the name suggests, Excel gives us the option to lookup for a number or text in a specific area which needs to be stated. If the value is found the corresponding value or text is returned The syntax for LOOKUP is as follows; =LOOKUP( lookup_value , lookup_vector , result_vector )       In the diagram, column D contains varying salaries, against which there is a company car in column E which corresponds to each salary. For example, a £20030 salary gets a Golf, a £35000 salary gets a Scorpio. A LOOKUP formula can be used to return whatever car is appropriate to a salary figure that is entered. In this case, the lookup_value is the cell where the salary is entered (B13), the lookup_vector is the salary column (D3:D11), and the result_vector is the car column (E3:E11). Hence the formula; =LOOKUP(B13,D3:D11,E3:E11) Typing 40000 in cell B13 will set the lookup_value. LOOKUP will search through the lookup_vector to find the matching salary, and return th...