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

Insufficient access rights to perform the operation. (Exception from HRESULT: 0x80072098)

While accessing the active directory (AD) and authorization manager (AZMAN) , If you get “   Insufficient access rights to perform the operation. (Exception from HRESULT: 0x80072098)  “ message check the    account that is being used to get the LDAP query from AD .  ERROR DETAILS Exception Details:  System.Runtime.InteropServices.COMException: Insufficient access rights to perform the operation. (Exception from HRESULT: 0x80072098) Source Error: Line 154:    'Session("FullName") = System.Security.Principal.WindowsIdentity.GetCurrent.Name.ToString() Line 155: Line 156:    If Not User.IsInRole("Role1") Then Line 157:          Response.Redirect("./Login.aspx") Line 158:    End If  Stack Trace : .... SOLVE IT Steps to do check the app pool rights: Click on the website name that you are having problem with in IIS  In the right panel you will see 'Basic Settings'. Click It. Select the specific pool option and enter the name of the ac

Sql Server database Read_Only / Read_Write

The ALTER DATABASE command allows a database administrator to modify SQL Server databases and their files and filegroups. This includes permitting the changing of database configuration options. Why Read Only ? When you need to ensure that the data is a database is not modified by any users or automated processes, it is useful to set the database into a read-only mode. Once read-only, the data can be read normally but any attempts to create, updated or delete table rows is disallowed. This makes the read-only mode ideal when preparing for data migration, performing data integrity checking or when the data is only required for historical reporting purposes. Make Database Read Only USE  [master] GO ALTER DATABASE  [TESTDB]  SET  READ_ONLY  WITH  NO_WAIT GO Make Database Read/Write USE  [master] GO ALTER DATABASE  [TESTDB]  SET  READ_WRITE  WITH  NO_WAIT GO In case you get the following error message make the database single user: Msg 5070, Level 16, Stat

Do's and Don't SQL

Do's: Writing comments whenever something is not very obvious, as it won’t impact the performance.  (--) for single line  (/*…*/) to mark a section Use proper indentation Use Upper Case for all SQL keywords. SELECT, UPDATE, INSERT, WHERE, INNER JOIN, AND, OR, LIKE. Use BEGIN... END block for multiple statements in conditional code  Use Declare and Set in beginning of Stored procedure Create objects in same database where its relevant table exists otherwise it will reduce network performance. Use PRIMARY key in WHERE condition of UPDATE or DELETE statements as this will avoid error possibilities. If User table references Employee table than the column name used in reference should be UserID where User is table name and ID primary column of User table and UserID is reference column of Employee table. Use select column name instead of select * Use CTE (Common Table Expression); its scope is limited to the next statement in SQL query, instead of temporary tables and der