给学生画个时间table(代码逻辑错误..)

drawing a time table for students ( error in code logic..)

当我试图为我们学院的学生绘制这个 "timeTable" 时,我遇到了这个问题,如果我下午有半天的讲座,我将其设置为 collspan=2,但在下一个单元格 属于第二天...? 如果我没有很好地描述我的问题,请原谅,但我希望我的代码提供的图像对你有意义。

void emploi()
        {

            bool flag = false;
            int jour = 1;
            while (jour <= 6)
            {
                switch (jour)
                {
                    case 1: cell1.Text = "Lundi"; break; case 2: cell1.Text = "Mardi"; break; case 3: cell1.Text = "Mercredi"; break; case 4: cell1.Text = "Jeudi"; break;case 5: cell1.Text = "Vendredi"; break;case 6: cell1.Text = "Samedi"; break; 
                }

                tRow.Cells.Add(cell1); cell1 = new TableCell();
                foreach (var v in emp.function_Afficher_Group("TDI202"))
                { flag = false;

                    if (v.Seance.Substring(0, 1) == jour.ToString())
                    {

                        if (v.Seance.Substring(1) == "S1")
                        {

                            cell1.ID = jour + "/" + v.Seance.Substring(2);
                            SeanceInfo.Text = v.Module + Environment.NewLine + v.Salle + Environment.NewLine + v.Formateur + Environment.NewLine;
                            cell1.Controls.Add(SeanceInfo); 
                            fillcombos(jour + "S1",2019,false);
                            SeanceInfo.Click += (object sender, EventArgs e) => {LinkButton button = sender as LinkButton;button.Visible = false;};

                        }
                        if (v.Seance.Substring(1) == "S2")
                        {

                            cell1.ID = jour + "/" + v.Seance.Substring(2);
                            SeanceInfo.Text = v.Module + Environment.NewLine + v.Salle + Environment.NewLine + v.Formateur + Environment.NewLine;
                            cell1.Controls.Add(SeanceInfo);
                            fillcombos(jour + "S2", 2019, false);
                            SeanceInfo.Click += (object sender, EventArgs e) => { LinkButton button = sender as LinkButton; button.Visible = false; };
                        }
                        if (v.Seance.Substring(1) == "S3")
                        {

                            cell1.ID = jour + "/" + v.Seance.Substring(2);
                            SeanceInfo.Text = v.Module + Environment.NewLine + v.Salle + Environment.NewLine + v.Formateur + Environment.NewLine;
                            cell1.Controls.Add(SeanceInfo);
                            fillcombos(jour + "S3", 2019, false);
                            SeanceInfo.Click += (object sender, EventArgs e) => { LinkButton button = sender as LinkButton; button.Visible = false; };
                        }
                        if (v.Seance.Substring(1) == "S4")
                        {

                            cell1.ID = jour + "/" + v.Seance.Substring(2);
                            SeanceInfo.Text = v.Module + Environment.NewLine + v.Salle + Environment.NewLine + v.Formateur + Environment.NewLine;
                            cell1.Controls.Add(SeanceInfo);
                            fillcombos(jour + "S4", 2019, false);
                            SeanceInfo.Click += (object sender, EventArgs e) => { LinkButton button = sender as LinkButton; button.Visible = false; };
                        }


                        if (cell1.ID == jour + "/2")
                        {
                            int ind = tRow.Cells.Count-1;
                            if ((tRow.Cells[ind].Controls[0] as LinkButton).Text == (cell1.Controls[0] as LinkButton).Text && (cell1.Controls[0] as LinkButton).Text!= "" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine)
                            {
                                tRow.Cells[ind].ColumnSpan = 2;
                                flag = true;

                            }

                        }    
                        if (cell1.ID == jour + "/4")
                        {
                            int ind = tRow.Cells.Count - 1;
                            if ((tRow.Cells[ind].Controls[0] as LinkButton).Text == (cell1.Controls[0] as LinkButton).Text && (cell1.Controls[0] as LinkButton).Text != "" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine)
                            {
                                tRow.Cells[ind].ColumnSpan = 2;
                                flag = true;
                            }

                        }

                        if ((cell1.Controls[0] as LinkButton).Text == ""+Environment.NewLine+""+ Environment.NewLine +""+ Environment.NewLine)
                            cell1.BackColor = System.Drawing.Color.LightGray ;
                          cell1.CssClass = "myCells"; (cell1.Controls[0] as LinkButton).CssClass = "tt";
                        if (flag == false)
                        {
                            tRow.Cells.Add(cell1); cell1 = new TableCell(); SeanceInfo = new LinkButton(); dropForm = new DropDownList();
                        }

                    }
                }
                Table1.Rows.Add(tRow);
                tRow = new TableRow();
                jour++;
            }
        }

已解决---->注释行//*******

void emploi()
        {

            bool flag = false;
            int jour = 1;
            while (jour <= 6)
            {
                switch (jour)
                {
                    case 1: cell1.Text = "Lundi"; break; case 2: cell1.Text = "Mardi"; break; case 3: cell1.Text = "Mercredi"; break; case 4: cell1.Text = "Jeudi"; break;case 5: cell1.Text = "Vendredi"; break;case 6: cell1.Text = "Samedi"; break; 
                }

                tRow.Cells.Add(cell1); cell1 = new TableCell();
                foreach (var v in emp.function_Afficher_Group("TDI202"))
                { flag = false;

                    if (v.Seance.Substring(0, 1) == jour.ToString())
                    {

                        if (v.Seance.Substring(1) == "S1")
                        {

                            cell1.ID = jour + "/" + v.Seance.Substring(2);
                            SeanceInfo.Text = v.Module + Environment.NewLine + v.Salle + Environment.NewLine + v.Formateur + Environment.NewLine;
                            cell1.Controls.Add(SeanceInfo); 
                            fillcombos(jour + "S1",2019,false);
                            SeanceInfo.Click += (object sender, EventArgs e) => {LinkButton button = sender as LinkButton;button.Visible = false;};

                        }
                        if (v.Seance.Substring(1) == "S2")
                        {

                            cell1.ID = jour + "/" + v.Seance.Substring(2);
                            SeanceInfo.Text = v.Module + Environment.NewLine + v.Salle + Environment.NewLine + v.Formateur + Environment.NewLine;
                            cell1.Controls.Add(SeanceInfo);
                            fillcombos(jour + "S2", 2019, false);
                            SeanceInfo.Click += (object sender, EventArgs e) => { LinkButton button = sender as LinkButton; button.Visible = false; };
                        }
                        if (v.Seance.Substring(1) == "S3")
                        {

                            cell1.ID = jour + "/" + v.Seance.Substring(2);
                            SeanceInfo.Text = v.Module + Environment.NewLine + v.Salle + Environment.NewLine + v.Formateur + Environment.NewLine;
                            cell1.Controls.Add(SeanceInfo);
                            fillcombos(jour + "S3", 2019, false);
                            SeanceInfo.Click += (object sender, EventArgs e) => { LinkButton button = sender as LinkButton; button.Visible = false; };
                        }
                        if (v.Seance.Substring(1) == "S4")
                        {

                            cell1.ID = jour + "/" + v.Seance.Substring(2);
                            SeanceInfo.Text = v.Module + Environment.NewLine + v.Salle + Environment.NewLine + v.Formateur + Environment.NewLine;
                            cell1.Controls.Add(SeanceInfo);
                            fillcombos(jour + "S4", 2019, false);
                            SeanceInfo.Click += (object sender, EventArgs e) => { LinkButton button = sender as LinkButton; button.Visible = false; };
                        }


                        if (cell1.ID == jour + "/2")
                        {
                            int ind = tRow.Cells.Count-1;
                            if ((tRow.Cells[ind].Controls[0] as LinkButton).Text == (cell1.Controls[0] as LinkButton).Text && (cell1.Controls[0] as LinkButton).Text!= "" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine)
                            {
                                tRow.Cells[ind].ColumnSpan = 2;
                                flag = true;

                            }

                        }    
                        if (cell1.ID == jour + "/4")
                        {
                            int ind = tRow.Cells.Count - 1;
                            if ((tRow.Cells[ind].Controls[0] as LinkButton).Text == (cell1.Controls[0] as LinkButton).Text && (cell1.Controls[0] as LinkButton).Text != "" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine)
                            {
                                tRow.Cells[ind].ColumnSpan = 2;
                                flag = true;
                            }

                        }

                        if ((cell1.Controls[0] as LinkButton).Text == ""+Environment.NewLine+""+ Environment.NewLine +""+ Environment.NewLine)
                            cell1.BackColor = System.Drawing.Color.LightGray ;
                          cell1.CssClass = "myCells"; (cell1.Controls[0] as LinkButton).CssClass = "tt";
                        if (flag == false)
                        {
                            tRow.Cells.Add(cell1); cell1 = new TableCell(); SeanceInfo = new LinkButton(); dropForm = new DropDownList();
                        }

                    }
                    cell1 = new TableCell(); //*****added this before leaving the foreach
                }
                Table1.Rows.Add(tRow);
                tRow = new TableRow();
                jour++;
            }
        }