Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I need to make a RDLC report that has concept like one-many relationship.
Suppose we have Product and their variances. and one method GetProdctList() which returning product list.
Now Need to bind report.

Here is the class :

SQL
public class Product
{
    public INT ProdId  {get;set;}
    public string Name {get;set;}
    public List<Variance> ProductVariance {get;set;}
}

public class Variance
{
  public INT ProdId {get;set;}
  public string Color {get;set;};
}


How can I implement it Please let me know your view? I tried but at the time of report design I am unable to access variance's members from the same Dataset.

Note : From front end only List of Products available.

Thanks,
Padam
Posted
Updated 24-Aug-15 5:45am
v2
Comments
Maciej Los 24-Aug-15 13:57pm    
Not enough information. Please, be more specific and provide more details. What have you tried? Where are you stuck?

1 solution

As i mentioned in the comment to the question you did not provide enough information. Nevertheless...

You can use Linq[^] to get the list of products and its varances. Have a look at example:

C#
//class definition:
public class Variance
{
    private int pid = 0;
    private string c = string.Empty;

    public Variance(int _pid, string _c)
    {
        pid = _pid;
        c = _c;
    }

    public int ProdId
    {
        get{return pid;}
        set{pid = value;}
    }

    public string Color
    {
        get{return c;}
        set{c = value;}
    }
}
public class Product
{
    private int pid = 0;
    private string sname = string.Empty;
    private List<Variance> cvariances = null;

    public Product(int _pid, string _sname, List<Variance> _cvariances)
    {
        pid = _pid;
        sname = _sname;
        cvariances = _cvariances;
    }

    public int ProdId
    {
        get{return pid;}
        set{pid = value;}
    }

    public string Name
    {
        get{return sname;}
        set{sname = value;}
    }

    public List<Variance> ProductVariances
    {
        get{return cvariances;}
        set{cvariances = value;}
    }
}
//main program:
	List<product> products  = new List<product>()
	{
		new Product(1, "GummiBear", new List<variance>()
		{
			new Variance(1, "black"),
			new Variance(1, "red"),
			new Variance(1, "white")
		}),
		new Product(2, "CellPhone", new List<variance>()
		{
			new Variance(2, "blue"),
			new Variance(2, "gold")
		})
	};
	
	DataTable dt = new DataTable();
	DataColumn dc = new DataColumn("ProdId", Type.GetType("System.Int32"));
	dt.Columns.Add(dc);
	dc = new DataColumn("Name", Type.GetType("System.String"));
	dt.Columns.Add(dc);
	dc = new DataColumn("Color", Type.GetType("System.String"));
	dt.Columns.Add(dc);
	
	var result = from v in products.SelectMany(p=>p.ProductVariances.Select(pv=>pv))
		join p in products on v.ProdId equals p.ProdId
		select new object[]
			{
				p.ProdId,
				p.Name,
				v.Color
			};
        //fill datatable
	foreach (var r in result)
	{
		dt.Rows.Add(r);
	}
        //TO DO:
        //use datatable as a datasource for rdlc report


Result:

ProdId Name      Color
1      GummiBear black 
1      GummiBear red 
1      GummiBear white 
2      CellPhone blue 
2      CellPhone gold 



Note: the definition of classes has been changed.

You can use CopyToDataTable extension method[^] to create DataTable object in simpler manner.
 
Share this answer
 
v2
Comments
Padam Agrawal 25-Aug-15 2:58am    
Thanks Maciej,
You got exactly what i want that would work also;

But is there any way to pass directly list<Product> to RDLC As data source rather than flat records. because in case of flat records mostly columns are repeating.

I sent List<product> at RDLC and able to access Pid, sName but not List<variance> cvariances

This was my concern hope you undersatnd.

Thanks a ton for quick response and solution.
Maciej Los 25-Aug-15 3:00am    
As far as i know you can't pass list of products as a source of rdlc report. Please, accept my answer as a solution (green button).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900