如何在Gridview中修复加载速度

时间:2019-04-08 06:45:28

标签: c# mysql asp.net

每次单击添加行按钮后,我都将三个下拉列表绑定并在网格视图中添加行按钮,加载速度非常慢。

我已经在mysql服务器上尝试过

private void AddNewRowToGrid()
    {
        int rowIndex = 0;
        if (ViewState["DataTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["DataTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    //extract the TextBox values
                    DropDownList ddlpurpose = (DropDownList)travel_grid.Rows[rowIndex].Cells[1].FindControl("ddl_purpose");
                    TextBox txtnooftimes = (TextBox)travel_grid.Rows[rowIndex].Cells[2].FindControl("txt_nooftimes");
                    DropDownList ddlcontinent = (DropDownList)travel_grid.Rows[rowIndex].Cells[1].FindControl("ddl_continent");
                    DropDownList ddlcountry = (DropDownList)travel_grid.Rows[rowIndex].Cells[1].FindControl("ddl_country");
                    DropDownList ddlcity = (DropDownList)travel_grid.Rows[rowIndex].Cells[1].FindControl("ddl_city");

                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["sno"] = i + 1;

                    dtCurrentTable.Rows[i - 1]["TravelPurpose"] = ddlpurpose.SelectedValue;
                    dtCurrentTable.Rows[i - 1]["NoOfTimesTravel"] = txtnooftimes.Text;
                    dtCurrentTable.Rows[i - 1]["Continent"] = ddlcontinent.SelectedValue;
                    dtCurrentTable.Rows[i - 1]["Country"] = ddlcountry.SelectedValue;
                    dtCurrentTable.Rows[i - 1]["City"] = ddlcity.SelectedValue;



                   rowIndex++;
                }
                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["DataTable"] = dtCurrentTable;
                travel_grid.DataSource = dtCurrentTable;
                travel_grid.DataBind();

               bind_continent();
               bind_country();
               bind_city();

           }
        }
        else
        {
            Response.Write("ViewState is null");
        }
        //Set Previous Data on Postbacks
        SetPreviousData();
    }


每次单击添加行按钮或基于其他下拉列表后,每次在gridview中绑定下拉列表都会获得更多的加载时间。


   public void bind_country()
   {
       foreach (GridViewRow row in travel_grid.Rows)
       {

           DropDownList ddl_country = (row.Cells[1].FindControl("ddl_country") as DropDownList);


           string cd = "Select * from country";
           MySqlDataAdapter adp = new MySqlDataAdapter(cd, connection);
           DataTable dta = new DataTable();
           adp.Fill(dta);

           ddl_country.DataSource = dta;
           ddl_country.DataTextField = "country_name";
           ddl_country.DataValueField = "country_code";
           ddl_country.DataBind();
           ddl_country.Items.Insert(0, new ListItem("Select Country", "0"));
       }
   }


0 个答案:

没有答案