浏览器中的表呈现问题

时间:2011-09-07 13:53:40

标签: c# asp.net asp.net-mvc asp.net-mvc-2 datatable

我有以下代码,用于绘制座位可用性的阶段。问题是当页面加载时,默认阶段是根据类别颜色绘制的。但是当我从下拉列表中选择特定组时,它将显示特定组座位可用性的阶段。

问题是我选择小组后会花费太多时间......我怎样才能画出舞台 我的创造阶段的逻辑是最短的时间。

- 我的想法是填充(仅红色)LOGIC下拉选择的索引已更改。

private void CreateDynamicTable()
{
    string str = @"Data Source=SHREE\SQLEXPRESS;Initial Catalog=StageCraftNew;User ID=sa;Password=vivek";
    SqlConnection con = new SqlConnection(str);

    // FOR SEAT ASSIGNMENT (RED COLOR)
    SqlCommand cmdSeat = new SqlCommand("Select FName,LName,SeatNo from Person_Master a,Member_Master b,SeatAssign_Master c where a.Personid=b.Personid and b.Memberid=c.memberid and b.Active='True' and c.Year='" + 2 + "' and GID='" + DropDownList1.SelectedIndex + "'", con);
    SqlDataAdapter daSeat = new SqlDataAdapter(cmdSeat);
    DataSet dsSeat = new DataSet();
    daSeat.Fill(dsSeat);

    // FOR SEAT ALPHABETS (BLUE COLOR)
    SqlCommand cmdSeatMaster = new SqlCommand("select cm.CId,sm.Row,sm.ColorCode,cm.CategoryColor from Category_master cm,Seat_Master sm where cm.CId=sm.CId", con);
    SqlDataAdapter daSeatMaster = new SqlDataAdapter(cmdSeatMaster);
    DataSet dsSeatMaster = new DataSet();
    daSeatMaster.Fill(dsSeatMaster);      


    // FOR STAGE DRAW

    Table tbl = new Table();


    using (SqlConnection conn = new SqlConnection(str))
    {
        using (SqlCommand cmd = new SqlCommand("select * from Stage", conn))
        {
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);

            int tblRows = ds.Tables[0].Rows.Count;
            int tblCols = ds.Tables[0].Columns.Count;
            // Create a Table and set its properties 

            tbl.BorderStyle = BorderStyle.Solid;
            tbl.CellSpacing = 0;
            tbl.Attributes.Add("style","width:850px;height:auto;table-layout:fixed;margin-top:40px;font-size:13px;font-family:verdana");

            // FOR STAGE DRAW
            for (int i = 0; i < tblRows; i++)
            {        
                TableRow tr = new TableRow();               

                string alpha = "";
                for (int j = 1; j < tblCols; j++)
                {
                    TableCell tc = new TableCell();
                    tc.BackColor = System.Drawing.Color.White;
                    Label lbl = new Label();

                    if (ds.Tables[0].Rows[i][j].ToString() == "55" || ds.Tables[0].Rows[i][j].ToString() == "56")
                    {
                        lbl.Text = "&nbsp;&nbsp;&nbsp;";                          
                    }
                    else
                    {
                        lbl.Text = ds.Tables[0].Rows[i][j].ToString();
                    }

                    if (alpha == "")
                    {
                        Regex r = new Regex("^[A-Z]*$");

                        if (r.IsMatch(lbl.Text))
                        {
                            alpha = lbl.Text;                                
                        }
                    }

                    // FOR SEAT ALPHABETS (BLUE COLOR)
                    for (int row = 0; row < dsSeatMaster.Tables[0].Rows.Count; row++)
                    {
                        for (int col = 0; col < dsSeatMaster.Tables[0].Columns.Count; col++)
                        {
                            if (dsSeatMaster.Tables[0].Rows[row]["Row"].ToString() == alpha)
                            {
                                tc.BackColor = System.Drawing.ColorTranslator.FromHtml(dsSeatMaster.Tables[0].Rows[row]["ColorCode"].ToString());
                                tc.Attributes.Add("style", "text-align:center");

                            }               
                        }
                    }

                    // FOR CATEGORY COLOR

                    for (int row = 0; row < dsSeatMaster.Tables[0].Rows.Count; row++)
                    {
                        for (int col = 0; col < dsSeatMaster.Tables[0].Columns.Count; col++)
                        {

                            if (dsSeatMaster.Tables[0].Rows[row]["Row"].ToString() == alpha)
                            {
                                tc.BackColor = System.Drawing.ColorTranslator.FromHtml(dsSeatMaster.Tables[0].Rows[row]["CategoryColor"].ToString());
                                tc.Attributes.Add("style", "text-align:center");
                            }
                        }
                    }


                    // FOR SEAT ASSIGNMENT (RED COLOR)
                    for (int row = 0; row < dsSeat.Tables[0].Rows.Count; row++)
                    {
                        for (int col = 0; col < dsSeat.Tables[0].Columns.Count; col++)
                        {
                            Regex r = new Regex("^[A-Z]*$");

                            if (r.IsMatch(ds.Tables[0].Rows[i][j].ToString()) && ds.Tables[0].Rows[i][j].ToString() != "")
                            {
                                tc.BackColor = System.Drawing.ColorTranslator.FromHtml("#ADD8E6");
                                tc.Attributes.Add("style", "text-align:center");
                            }
                            else if (ds.Tables[0].Rows[i][j].ToString() == "")
                            {
                                tc.BackColor = System.Drawing.Color.White;

                            }
                            else if (alpha + "" + ds.Tables[0].Rows[i][j].ToString() == dsSeat.Tables[0].Rows[row]["SeatNo"].ToString() && alpha + "" + ds.Tables[0].Rows[i][j].ToString() != "")
                            {
                                    tc.Attributes.Add("class", "demo-tip-twitter");
                                    tc.Attributes.Add("style", "cursor:pointer;text-align:center;");                    
                                    tc.ToolTip = "Seat: "+ alpha + "" + ds.Tables[0].Rows[i][j].ToString() + "\n" + dsSeat.Tables[0].Rows[row]["Fname"].ToString() + " " + dsSeat.Tables[0].Rows[row]["Lname"].ToString();
                                    tc.BackColor = System.Drawing.Color.Red;

                            }

                        }
                    }
                    tc.Controls.Add(lbl);
                    tr.Cells.Add(tc);                      
                }

                // Add the TableRow to the Table
                tbl.Rows.Add(tr);
            }
            form1.Controls.Add(tbl);

        }

    }

}

1 个答案:

答案 0 :(得分:0)

你的代码中没有什么是突出的,但我没有注意到你有两个几乎完全相同的循环:

 // FOR SEAT ALPHABETS (BLUE COLOR) 
for (int row = 0; row < dsSeatMaster.Tables[0].Rows.Count; row++) 
{ 
    for (int col = 0; col < dsSeatMaster.Tables[0].Columns.Count; col++) 
    { 
        if (dsSeatMaster.Tables[0].Rows[row]["Row"].ToString() == alpha) 
        { 
            tc.BackColor = System.Drawing.ColorTranslator.FromHtml(dsSeatMaster.Tables[0].Rows[row]["ColorCode"].ToString()); 
            tc.Attributes.Add("style", "text-align:center"); 

        }                
    } 
} 

// FOR CATEGORY COLOR 

for (int row = 0; row < dsSeatMaster.Tables[0].Rows.Count; row++) 
{ 
    for (int col = 0; col < dsSeatMaster.Tables[0].Columns.Count; col++) 
    { 

        if (dsSeatMaster.Tables[0].Rows[row]["Row"].ToString() == alpha) 
        { 
            tc.BackColor = System.Drawing.ColorTranslator.FromHtml(dsSeatMaster.Tables[0].Rows[row]["CategoryColor"].ToString()); 
            tc.Attributes.Add("style", "text-align:center"); 
        } 
    } 
} 

我不确定所需的效果是什么,但看起来你可以删除其中一个循环并将逻辑合为一个。我不知道你的缓慢程度是什么,但这应该有助于加快速度。