Skip to main content Skip to footer

C1TrueDbGrid Binding to BindingList Implementing INotifyPropertyChanged Interface

The most commonly used data binding in C1TrueDbGrid is binding the grid to the List<> collection. And the most commonly faced problem in C1TrueDbGrid is that the updations to the collection at runtime are not shown on the grid. To solve this problem, you can bind the grid with a BindingList<> collection. Now when any new object is added or deleted to or from the collection, the grid is immediately notified and the changes are shown on the grid. One problem with this approach is that if any edition is done to the already existing collection objects, the changes are not reflected on the control. The solution to the above problem is implementing the interface INotifyPropertyChanged and use a BindingList<> collection of objects of this class. Now when a addition, deletion or updation is done to the collection, the grid is immediately notified. To start with, create a class Customers as follows :

public class Customer  
{  
int custId;  
string companyName;  
string region;  
string phone;  

public int CustomerId  
{  
get { return custId; }  
set { custId = value; }  
}  

public string CompanyName  
{  
get { return companyName; }  
set { companyName = value; }  
}  

public string Region  
{  
get { return region; }  
set { region = value; }  
}  

public string Phone  
{  
get { return phone; }  
set { phone = value; }  
}  

public Customer(int number, string str1, string str2, string str3)  
{  
CustomerId = number;  
CompanyName = str1;  
Region = str2;  
Phone = str3;  
}  
public Customer()  
{  
CustomerId = 0;  
CompanyName = "";  
Region = "";  
Phone = "";  
}  
}

Now create a List<> collection of Customer class, add some objects and bind it to C1TrueDbGrid as follows :

List<Customer> _list = new List<Customer>();  
_list.Add(new Customer(1, "Trevors & Trevors Co Ltd", "Washington","(206) 555-8257"));  
_list.Add(new Customer(2, "Alfreds Futterkiste", "Germany", "030-0074321"));  
c1TrueDBGrid1.DataSource = _list;

Run the application. When any new row is added to the collection at runtime using the following code, the new row is not shown on the grid:

Customer cst1 = new Customer(id1, "Tortuga Restaurante", "Mexico", "(5) 555-2933");  
_list.Add(cst1);

Also, whenever any field is modified in the List collection, the changes are not immediately reflected on the grid. To solve this, create a BindingList<> collection of the objects :

BindingList<Customer> _bList;  
_bList.Add(new Customer(1, "Trevors & Trevors Co Ltd", "Washington","(206) 555-8257"));  
_bList.Add(new Customer(2, "Alfreds Futterkiste", "Germany", "030-0074321"));  
c1TrueDBGrid1.DataSource = _bList;

When the C1TrueDbGrid is bound to a BindingList<> collection, and any new object is added to the _bList collection, the new row is shown immediately on the grid. Another problem is that when any changes are made to the fields of the existing objects in bindinglist collection, the changes are not immediately reflected on the grid. The grid needs to be refreshed, either by clicking the grid or by changing any property explicitly to show the changes. To solve the above problem, you need to inherit the class with INotifyPropertyChanged interface as follows :

 public class Customer_Modified : INotifyPropertyChanged  
{  
int custId;  
string companyName;  
string region;  
string phone;  

public event PropertyChangedEventHandler PropertyChanged;  

public int CustomerId  
{  
get { return custId; }  
set  
{  
custId = value;  
NotifyPropertyChanged("CustId");  
}  
}  

public string CompanyName  
{  
get { return companyName; }  
set  
{  
companyName = value;  
NotifyPropertyChanged("CompanyName");  
}  
}  

public string Region  
{  
get { return region; }  
set  
{  
region = value;  
NotifyPropertyChanged("Region");  
}  
}  

public string Phone  
{  
get { return phone; }  
set  
{  
phone = value;  
NotifyPropertyChanged("Phone");  
}  
}  

public void NotifyPropertyChanged(string propertyName)  
{  
if (PropertyChanged != null)  
{  
PropertyChanged(this,  
new PropertyChangedEventArgs(propertyName));  
}  
}  

public Customer_Modified()  
{  
CustomerId = 0;  
CompanyName = "";  
Region = "";  
Phone = "";  
}  

public Customer_Modified(int number, string str1, string str2, string str3)  
{  
CustomerId = number;  
CompanyName = str1;  
Region = str2;  
Phone = str3;  
}  
}

Now create a BindingList<> collection of this class as follows and bind it to the grid :

BindingList<Customer\_Modified> \_newbList;  
\_newbList = new BindingList<Customer\_Modified>();  
\_newbList.Add(new Customer\_Modified(1, "Trevors & Trevors Co Ltd", "Washington", "(206) 555-8257"));  
c1TrueDBGrid1.DataSource = _newbList;

Now, if any object is added or removed or edited in this _newList collection, the changes are immediately reflected on the C1TrueDbGrid and you do not need to refresh the grid explicitly. Download Sample

The most commonly used data binding in C1TrueDbGrid is binding the grid to the List<> collection. And the most commonly faced problem in C1TrueDbGrid is that the updations to the collection at runtime are not shown on the grid.

To solve this problem, you can bind the grid with a BindingList<> collection. Now when any new object is added or deleted to or from the collection, the grid is immediately notified and the changes are shown on the grid. One problem with this approach is that if any edition is done to the already existing collection objects, the changes are not reflected on the control.

The solution to the above problem is implementing the interface INotifyPropertyChanged and use a BindingList<> collection of objects of this class. Now when a addition, deletion or updation is done to the collection, the grid is immediately notified.

To start with, create a class Customers as follows :


public
class Customer

{

int custId;

string companyName;

string region;

string phone;

public int CustomerId

{

get { return custId; }

set { custId = value; }

}

public string CompanyName

{

get { return companyName; }

set { companyName = value; }

}

public string Region

{

get { return region; }

set { region = value; }

}

public string Phone

{

get { return phone; }

set { phone = value; }

}

public Customer(int number, string
str1, string str2, string str3)

{

CustomerId = number;

CompanyName = str1;

Region = str2;

Phone = str3;

}

public Customer()

{

CustomerId = 0;

CompanyName = "";

Region = "";

Phone = "";

}

}




Now create a List<> collection of Customer class, add some objects and bind it to C1TrueDbGrid as follows :


List
_list = new List();

_list.Add(new
Customer(1, "Trevors & Trevors Co Ltd",
"Washington","(206) 555-8257"));

_list.Add(new
Customer(2, "Alfreds Futterkiste", "Germany",
"030-0074321"));

c1TrueDBGrid1.DataSource
= _list;




Run the application. When any new row is added to the collection at runtime using the following code, the new row is not shown on the grid:


Customer
cst1 = new Customer(id1, "Tortuga Restaurante", "Mexico",
"(5) 555-2933");

_list.Add(cst1);




Also, whenever any field is modified in the List collection, the changes are not immediately reflected on the grid.

To solve this, create a BindingList<> collection of the objects :


BindingList
_bList;

_bList.Add(new
Customer(1, "Trevors & Trevors Co Ltd",
"Washington","(206) 555-8257"));

_bList.Add(new
Customer(2, "Alfreds Futterkiste", "Germany",
"030-0074321"));

c1TrueDBGrid1.DataSource
= _bList;




When the C1TrueDbGrid is bound to a BindingList<> collection, and any new object is added to the _bList collection, the new row is shown immediately on the grid.

Another problem is that when any changes are made to the fields of the existing objects in bindinglist collection, the changes are not immediately reflected on the grid. The grid needs to be refreshed, either by clicking the grid or by changing any property explicitly to show the changes.

To solve the above problem, you need to inherit the class with INotifyPropertyChanged interface as follows :


public
class Customer_Modified : INotifyPropertyChanged

{

int custId;

string companyName;

string region;

string phone;

public event PropertyChangedEventHandler
PropertyChanged;

public int CustomerId

{

get { return custId; }

set

{

custId = value;


NotifyPropertyChanged("CustId");

}

}

public string CompanyName

{

get { return companyName; }

set

{

companyName = value;


NotifyPropertyChanged("CompanyName");

}

}

public string Region

{

get { return region; }

set

{

region = value;


NotifyPropertyChanged("Region");

}

}

public string Phone

{

get { return phone; }

set

{

phone = value;


NotifyPropertyChanged("Phone");

}

}

public void NotifyPropertyChanged(string
propertyName)

{


if (PropertyChanged != null)

{

PropertyChanged(this,

new
PropertyChangedEventArgs(propertyName));

}

}

public Customer_Modified()

{

CustomerId = 0;

CompanyName = "";

Region = "";

Phone = "";

}

public Customer_Modified(int number, string
str1, string str2, string str3)

{

CustomerId = number;

CompanyName = str1;

Region = str2;

Phone = str3;

}

}




Now create a BindingList<> collection of this class as follows and bind it to the grid :


BindingList<Customer_Modified>
_newbList;

_newbList =
new BindingList<Customer_Modified>();

_newbList.Add(new
Customer_Modified(1, "Trevors & Trevors Co Ltd", "Washington",
"(206) 555-8257"));

c1TrueDBGrid1.DataSource
= _newbList;




Now, if any object is added or removed or edited in this _newList collection, the changes are immediately reflected on the C1TrueDbGrid and you do not need to refresh the grid explicitly.

MESCIUS inc.

comments powered by Disqus