Usercontrol databinding within a databound datagridview
Posted
by user328259
on Stack Overflow
See other posts from Stack Overflow
or by user328259
Published on 2010-05-12T17:23:52Z
Indexed on
2010/05/12
20:44 UTC
Read the original article
Hit count: 255
Good day. I'm developing a Windows application and working with Windows Forms; .Net 2.0.
I have an issue databinding a generic List of Car Rental Companies to a DataGridView when that list contains (one of its properties) anoother generic List of Car Makes. I also have a UserControl that I need to bind to this [inner] generic list ...
Class CarRentalCompany contains: string Name, string Location, List CarMakes
Class CarMake contains: string Name, bool isFord, bool isChevy, bool isOther
The UserControl has a label for CarMake.Name and 3 checkboxes for each of the booleans of the class. HOw do I make this user control bindable for the class?
In my form, I have a DataGridView binded to the CarRentalCompany object. The list CarMakes could be 0 or more items and I can add these as necessary. How do I establish the binding of CarRentalCompanies properly so CarMakes will bind accordingly??
For example, I have: List CarRentalCompanies = new List();
CarRentalCompany company1 = new CarRentalCompany();
company1.Name = "Acme Rentals";
company1.Location = "New York, NY";
company1.CarMakes = new List<CarMake>();
CarMake car1 = new CarMake();
car1.Name = "The Yellow Car";
car1.isFord = true;
car1.isChevy = false;
car1.isOther = false;
company1.CarMakes.Add(car1);
CarMake car2 = new CarMake();
car2.Name = "The Blue Car";
car2.isFord = false;
car2.isChevy = true;
car2.isOther = false;
company1.CarMakes.Add(car2);
CarMake car3 = new CarMake();
car3.Name = "The Purple Car";
car3.isFord = false;
car3.isChevy = false;
car3.isOther = true;
company1.CarMakes.Add(car3);
CarRentalCompanies.Add(company1);
CarRentalCompany company2 = new CarRentalCompany();
company1.Name = "Z-Auto Rentals";
company1.Location = "Phoenix, AZ";
company1.CarMakes = new List<CarMake>();
CarMake car4 = new CarMake();
car4.Name = "The OrangeCar";
car4.isFord = true;
car4.isChevy = false;
car4.isOther = false;
company2.CarMakes.Add(car4);
CarMake car5 = new CarMake();
car5.Name = "The Red Car";
car5.isFord = true;
car5.isChevy = false;
car5.isOther = false;
company2.CarMakes.Add(car5);
CarMake car6 = new CarMake();
car6.Name = "The Green Car";
car6.isFord = true;
car6.isChevy = false;
car6.isOther = false;
company2.CarMakes.Add(car6);
CarRentalCompanies.Add(company2);
I load my form and in my load form I have the following: Note: CarDataGrid is a DataGridView
BindingSource bsTheRentals = new BindingSource();
DataGridViewTextBoxColumn companyName = new DataGridViewTextBoxColumn();
companyName.DataPropertyName = "Name";
companyName.HeaderText = "Company Name";
companyName.Name = "CompanyName";
companyName.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
DataGridViewTextBoxColumn companyLocation = new DataGridViewTextBoxColumn();
companyLocation.DataPropertyName = "Location";
companyLocation.HeaderText = "Company Location";
companyLocation.Name = "CompanyLocation";
companyLocation.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
ArrayList carMakeColumnsToAdd = new ArrayList();
// Loop through the CarMakes list to add each custom column
for (int intX=0; intX < CarRentalCompanies.CarMakes[0].Count; intX++) {
// Custom column for user control
string carMakeColumnName = "carColumn" + intX;
CarMakeListColumn carMakeColumn = new DataGridViewComboBoxColumn();
carMakeColumn.Name = carMakeColumnName;
carMakeColumn.DisplayMember = CarRentalCompanies.CarMakes[intX].Name;
carMakeColumn.DataSource = CarRentalCompanies.CarMakes; // this is the CarMAkes List
carMakeColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
carMakeColumnsToAdd.Add(carMakeColumn);
}
CarDataGrid.DataSource = bsTheRentals;
CarDataGrid.Columns.AddRange(new DataGridViewColumn[] { companyName, companylocation, carMakeColumnsToAdd });
CarDataGrid.AutoGenerateColumns = false;
The code I provided does not work because I am unfamiliar with UserControls and custom DataGridViewColumns and DataGridViewCells - I know I must derive from these classes in order to use my User Control properly. I appreciate any advice/assistance/help in this. Thank you.
- Lawrence
© Stack Overflow or respective owner