Click here to Skip to main content
15,888,031 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have two lists.allotment list count is 16 and tailorlist count is 4. i need to display 4 rows and 4 cols. But it displaying 4 columns and 16 rows. Please tell me whats the problem. Below is my code

C#
private void Allocate()
        {
            int var1 = 0;
            Table1.Controls.Clear();
            Table1.Rows.Clear();
            int count = 0;
            IList<allotmentlist> objallotmentlist = new List<allotmentlist>();
            objallotmentlist = (IList<allotmentlist>)Session["Allotment"];
            IList<tailorlist> objtailorlist = new List<tailorlist>();
            objtailorlist = (IList<tailorlist>)Session["TailorDetails"];
            
            TableHeaderRow hrow = new TableHeaderRow();
            TableHeaderCell hcell1 = new TableHeaderCell();
            hcell1.Text = " ";
            hrow.Controls.Add(hcell1);
           
            foreach (TailorList tailor in objtailorlist)
            {
                TableHeaderCell hcell2 = new TableHeaderCell();
                hcell2.Text = tailor.TailorName;
                hrow.Controls.Add(hcell2);
                count++;
            }

            Table1.Controls.Add(hrow);


            foreach (AllotmentList item in objallotmentlist)
            {
                TableHeaderRow hrow1 = new TableHeaderRow();
                TableHeaderCell hcell3 = new TableHeaderCell();
                hcell3.Text = item.ItemName;
                hrow1.Controls.Add(hcell3);

                foreach (TailorList itm in objtailorlist)
                {
                    TableHeaderCell hcell4 = new TableHeaderCell();
                    Label lbltime = new Label();
                    lbltime.ID = "text" + var1;
                    lbltime.Text = Convert.ToInt32(item.Quantity).ToString();
                    lbltime.Width = 60;
                    hcell4.Controls.Add(lbltime);
                    hrow1.Controls.Add(hcell4);
                    ++var1;
                }

                Table1.Controls.Add(hrow1);

            }
          
        }
Posted
Updated 3-Jul-13 1:27am
v3
Comments
ArunRajendra 3-Jul-13 7:17am    
Which list is 4 and which one is 16?
Lakshmimsridhar 3-Jul-13 7:19am    
allotment list is 16 and tailorlist is 4
Sadique KT 3-Jul-13 7:57am    
You Are iterating through objtailorlist and it contains 16 elements,
In foreach block you are adding new rows for 16 times,
Do your filtering in objtailorlist before iterating to satisfy your condition (4 rows)..
or check your condition in foreach block before adding row to the table...
Lakshmimsridhar 3-Jul-13 7:59am    
please check code once. first i taken tailorlist which contains 4 counts. then i iterating allotment which contains 16 rows
Sadique KT 3-Jul-13 8:08am    
in first for loop you are adding header cells to the HeaderROw And its not the rows to the table

1 solution

C#
TableHeaderRow hrow = new TableHeaderRow();
            TableHeaderCell hcell1 = new TableHeaderCell();
            hcell1.Text = " ";
            hrow.Controls.Add(hcell1);
            int objcount = 0;
            foreach (AllotmentList item in objallotmentlist)
            {
                objcount++;
                TableHeaderCell hcell2 = new TableHeaderCell();
                hcell2.Text = item.ItemName;
                hrow.Controls.Add(hcell2);
                if (objcount == objtailorlist.Count)
                    break;
            }
            Table1.Controls.Add(hrow);
            int flag = 0;          
            for (int i = 0; i < objtailorlist.Count; i++)
            {
                TableHeaderRow hrow1 = new TableHeaderRow();
                TableHeaderCell hcell3 = new TableHeaderCell();
                hcell3.Text = objtailorlist[i].TailorName;
                hrow1.Controls.Add(hcell3);
                flag++;

                for (int j = 0; j < objtailorlist.Count; j++)
                { 
                    TableHeaderCell hcell4 = new TableHeaderCell();
                    Label lbltime = new Label();
                    lbltime.ID = "text" + var1;

                    lbltime.Text = objallotmentlist[countItem].Quantity.ToString();
                    lbltime.Width = 60;
                    hcell4.Controls.Add(lbltime);
                    hrow1.Controls.Add(hcell4);
                    countItem++;
                    ++var1;
                }

                Table1.Controls.Add(hrow1);
                if (flag == objtailorlist.Count)
                    break;
            }
            
            }
 
Share this answer
 
v2

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