1
Answer

Complex Type Binding in datagridview

Photo of Ajai Ajai

Ajai Ajai

7y
910
1

We have two class and there is relation with many to one company and product. So when we display product then we show the product name with company, so there is any direct way to bind DataGridvView.

public class Product : Common 
{

   public Int32 ProductID { get; set; }
  [
MaxLength(100)]
public
string ProductName { get; set; }
public
virtual ProductType ProductOfType { get; set; }
public virtual Company ProductCompany { get; set; }
public
virtual PackageType ProductPackageType { get; set; }
}

DGProduct.DataSource = db.Product.toList(), so when we bind the DataGridvView then its show MedicineEntity.ProductCompany as rows, so there is any way to direct binding or need to bind manually using loop. Please some one suggest if any option is available, otherwise need to bind DataGridvView using loop or convert list into datatable.

Some one guide me simple answer 
 

The simplest way is to define ToString method on Company class.

class Company 
{
   public override string ToString()
   {
      return this.Name;
   }
}

But its not work.

So, can any one suggest and help me any simple solution about that.

Also, I have find some solution, but only difference is that they used getter
setter method properly. So, what is the difference between getter setter with
proper code with initalization and normal as we have used.

 

Answers (1)