smith269
smith269

Reputation: 135

Display data as group using datalist

I have a datatable which looks like this..

s.no product categoryno 

1.   product1    1
2.   product2    1
3.   product3    1
4.   Product4    2
5.   Product5    2
6.   product6    2

Now I want to bind this datatable to a datalist as

some tittle:

 product1
 product2
 product3

some tittle:

 product4
 product5
 product6

I need to separate the data according to a column(like category in above example) and display it using datalist.Any sugessions will be appreciated..

NOTE: Product denotes image of the product whose path is in database..

Upvotes: 0

Views: 1231

Answers (2)

shahnawaz
shahnawaz

Reputation: 1

    ds.Tables[0].TableName = "Cats";
    ds.Tables[1].TableName = "Products";
    ds.Relations.Add("children",       ds.Tables["Cats"].Columns["categoryno"],ds.Tables["Products"].Columns["categoryno"]);
    YourDataList.DataSource = ds;
    YourDataList.DataBind();

Upvotes: 0

Kaf
Kaf

Reputation: 33809

If you have two DataTables (as per comments), you could add a relationship between two tables using categoryno and then bind it to the Datalist as below.

//Assuming your datatables are in ds (DataSet)

ds.Tables[0].TableName = "Cats";
ds.Tables[1].TableName = "Products";
ds.Relations.Add("children", ds.Tables["Cats"].Columns["categoryno"],
                             ds.Tables["Products"].Columns["categoryno"]);
YourDataList.DataSource = ds;
YourDataList.DataBind();

UPDATE: as per recent comments I think you just need to add the rows from one to the other.

foreach(DataRow dr2 in Table2.Rows)
{
   DataRow dr1 = Table1.NewRow(); 
   dr1[0] = dr2[0];
   dr1[1] = dr2[1];
   dr1[2] = dr2[2];
   //... if you have many columns 
   Table1.Rows.Add(dr1);
}
//Bind Table1 here

Upvotes: 1

Related Questions