The below example demonstrates hiding a property of a class from displaying in Datagridview. When you bind a class objec to datagridview with help of bindinglist, by default all properties will be displayed in datagridview.
Property name will be the column name of datagridview and each object values are displayed as one row.
If you want to hide any property of class from displaying , you can use Browsable attribute which is present in System.ComponentModel namespace.
Please find the code snippet below for making property invisible in Datagridview.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | private void Form1_Load(object sender, EventArgs e) { BindingList<Employee> employeelist = new BindingList<Employee>(); Employee employee01 = new Employee { EmpID = 1000, EmployeeName = "John", EmployeeLocation = "Bristol" }; Employee employee02 = new Employee { EmpID = 1000, EmployeeName = "John", EmployeeLocation = "Bristol" }; Employee employee03 = new Employee { EmpID = 1000, EmployeeName = "John", EmployeeLocation = "Bristol" }; employeelist.Add(employee01); employeelist.Add(employee02); employeelist.Add(employee03); dataGridView1.DataSource = employeelist; } |
Employee
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public class Employee { public int EmpID { get; set; } public string EmployeeName { get; set; } //The below property will not be visible in Datagridview or in properties window [Browsable(false)] public string EmployeeLocation { get; set; } } |
When you assign above datatable to Datagridview, the data in Datagridview looks as below.