RSS

How to Use PropertyGrid

How to use PropertyGrid in our C# Application

- The Propertygrid is like our Property window available in the .Net IDE. We can also add the propertygrid in C# window application

-The property grid is a powerful control for allowing users to edit the internals of your published classes.Because of the ability for the property grid to reflect and bind to your class, there is not much work involved in getting the property grid up and working.You can add categories and descriptions to your property grid by using the special grid attributes in the System.Component model.

-It displays the Properties and attributes with it's Description and the Category wise details of Object.

- 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).




- The Bellow attributes are used to manipulate the Properties of the class

1.CategoryAttribute - This will Arrange the Properties as a Category wise.
2.DescriptionAttribute - This will used for description of the Property
3.BrowsableAttribute -
This is used to determine whether or not the property is shown or hidden in
the property grid.
4.ReadOnlyAttribute -
Use this attribute to make your property read only inside the property grid
5.DefaultValueAttribute -
Specifies the default value of the property shown in the property grid
6.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.
The Bellow cust class object is used to load in PropertyGrid

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
namespace ExampleForTabControl
{
[DefaultPropertyAttribute("ID Settings")]
class Cust
{
private string _name;
private int _age;
private DateTime _dob;
private string _address;
private string _email;
private bool _freq;

[CategoryAttribute("ID Settings"), DescriptionAttribute("Name of the customer") ]
public string Name
{
get {return _name; }
set { _name = value; }
}
[CategoryAttribute("Age Details"), DescriptionAttribute("DOB Details")]
public DateTime DOB
{
get { return _dob; }
set { _dob = Convert.ToDateTime(value); }
}
[CategoryAttribute("Age Details"), DescriptionAttribute("Age in No of Years")]
public int Age
{
get { return _age; }
set { _age = Convert.ToInt32(value); }

}

[CategoryAttribute("Addressing Settings"), DescriptionAttribute("Customers Address")]
public string Address
{
get { return _address; }
set { _address = value; }
}
[CategoryAttribute("Marketting Settings"), DescriptionAttribute("Most current e-mail of the customer")]
public string Email
{
get { return _email; }
set { _email = value; }
}
[CategoryAttribute("Buying Status"), DescriptionAttribute("Frequently Buying Status")]
public bool FrequentBuyer
{
get { return _freq; }
set { _freq =Convert.ToBoolean(value); }
}
}
}


To Bind with PropertyGrid available in the Form1:
private void Form1_Load(object sender, EventArgs e)
{
tabEx1.Visible = false;
Cust Siva = new Cust();
Siva.Name = "Raja";
Siva.Age = 55;
Siva.DOB = Convert.ToDateTime("10-Feb-1985");
Siva.Email = "Siva@gmail.com";
Siva.FrequentBuyer = true;
Siva.Address = "Pallikkaranai,Chennai";
this.proGridCust.SelectedObject = Siva;
this.Text = "Customer";
}

* To download the Source code for the PropetyGrid Example, Please Click Here

0 comments:

Post a Comment