2016-12-02 121 views
0

我有一個GridView與80個行中的每一箇中的幾個文本框,下拉菜單和複選框以及綁定到網格的OnRowEdit和OnRowDataBound事件。 這是我做的更新網格:c#很慢的Gridview更新。

  1. 的ImageButton單擊行切換到編輯模式(7秒),並填充每行的複選框,根據所選行已與他人的關係
  2. 的ImageButton單擊行中保存編輯後的數據(13秒),
    • 行:單擊事件收集的控制數據和行數據保存到SQL
    • 列:在所有行中循環,得到一個複選框的狀態並將其保存到SQL

我是一個自學成才的並且有騙子來改善最有可能的事情。如果有人請看看並指出關鍵的耗時操作以及如何加快速度。 - 謝謝,馬丁

OnRowEdit:

protected void OnRowEditing(object sender, GridViewEditEventArgs e) 
    { 
     Gridview_Milestones.SelectedIndex = -1; 
     int rowIndex = e.NewEditIndex; 
     SelectedMSRow.Text = Convert.ToString(rowIndex); 
     SelectedMSRowValue.Text = Convert.ToString(Gridview_Milestones.DataKeys[rowIndex].Value); 
     CheckBox CheckedOrNot = Gridview_Milestones.Rows[rowIndex].FindControl("checkboxCustomerRequired") as CheckBox; 
     LabelCRD.Text = Convert.ToString(CheckedOrNot.Checked); 
    } 

OnRowDataBound:

protected void OnRowDataBoundMS(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      CheckBox CBTr = e.Row.FindControl("CheckboxTriggers") as CheckBox; 
      if ((e.Row.RowState & DataControlRowState.Edit) > 0) 
      { 
       if (LabelCRD.Text == "True") 
       { 
        TextBox TBresult1 = e.Row.FindControl("TextBoxDefaultDays") as TextBox; 
        TBresult1.Visible = false; 
       } 
       else 
       { 
        TextBox TBresult1 = e.Row.FindControl("TextBoxDefaultDays") as TextBox; 
        TBresult1.Visible = true; 
       } 
       DropDownList DDL1 = e.Row.FindControl("DDOwnerGroup") as DropDownList; 
       DropDownList DDL2 = e.Row.FindControl("DDOwner") as DropDownList; 
       Label LBL1 = e.Row.FindControl("LabelDefaultOwnerGroupEdit") as Label; 
       Label LBL2 = e.Row.FindControl("LabelDefaultOwnerEdit") as Label; 
       DDL1.SelectedValue = Convert.ToString(LBL1.Text); 
       DDL2.DataBind(); 
       DDL2.SelectedValue = Convert.ToString(LBL2.Text); 
       CBTr.Visible = false; 
      } 
      if (!string.IsNullOrEmpty(SelectedMSRow.Text)) 
      {      
       DataRowView rowView = (DataRowView)e.Row.DataItem; 
       int myDataKey = Convert.ToInt32(rowView["ID"]); 
       CBTr.Checked = SetCheckBoxTrigger(myDataKey); 
      } 
     } 
    } 

保存行數據:

protected void UpdateMilesStones(Object sender, EventArgs e) 
    { 
     UpdateTriggers(); 
     int rowIndex = Convert.ToInt32(SelectedMSRow.Text); 
     Label DataKey = Gridview_Milestones.Rows[rowIndex].FindControl("LabelEditID") as Label; 
     DropDownList DDL1 = Gridview_Milestones.Rows[rowIndex].FindControl("DDOwnerGroup") as DropDownList; 
     CheckBox CKB1 = Gridview_Milestones.Rows[rowIndex].FindControl("checkboxCustomerRequiredEdit") as Checkbox; 
     TextBox TB1 = Gridview_Milestones.Rows[rowIndex].FindControl("TextBoxDefaultDays") as TextBox; 
     SqlConnection objConn = new SqlConnection("XXXXX"); 
     SqlCommand objCommand = new SqlCommand(@"Update ...", objConn); 
     objCommand.Parameters.Add(".....") 
     objConn.Open(); 
     objCommand.ExecuteNonQuery(); 
     objConn.Close(); 
     Gridview_Milestones.DataBind(); 
     Gridview_Milestones.EditIndex = -1;    
    } 

保存列數據:

protected void UpdateTriggers() 
    { 
     DeleteOldTriggers(); 
     foreach (GridViewRow row in Gridview_Milestones.Rows) 
     { 
      string DataKey = Gridview_Milestones.DataKeys[row.RowIndex].Value.ToString(); 
      if (((CheckBox)row.FindControl("CheckboxTriggers")).Checked) 
      { 
       SqlConnection objConn = new SqlConnection("XXXXXX"); 
       SqlCommand objCommand = new SqlCommand(@"Insert into EPC_Triggers (MilestoneID, triggeredBy) Values (@IDMS,@IDRow)", objConn); 
       objCommand.Parameters.Add("@IDMS", SqlDbType.Int).Value = Convert.ToInt32(SelectedMSRowValue.Text); 
       objCommand.Parameters.Add("@IDRow", SqlDbType.Int).Value = Convert.ToInt32(DataKey); 
       objConn.Open(); 
       objCommand.ExecuteNonQuery(); 
       objConn.Close(); 
      } 
     } 
     Gridview_Milestones.Columns[13].Visible = false; 
    } 
+0

你使用的是經典的asp.net還是mvc? –

+0

ddl2的數據源是什麼? – TZHX

+0

爲什麼這麼難?嘗試爲您的數據庫提供者使用DataAdapter。 – GrApDev

回答

1

以下是可能的更改。

1)打開SQL連接三次(updatetrigger,rowdatabound和UpdateMilesStones)。請避免這一點。只嘗試在一個操作中完成。

2)使用使用塊SQL連接check this link

3)您也可以使用索引來代替的FindControl如果列訂單將被固定。

4)你也可以將數據保存在內存/緩存中,最後你將所有數據保存在SQL中。

請試試這些東西。它肯定會提高性能。

+0

很多輸入!謝謝。 1)實際上,SQL連接更經常地構建:i)填充每行需要一個連接的複選框ii)刪除舊值iii)在保存期間爲每一個檢查的行(!)iv)保存GridRow數據...也許填充由SQLDataSource提供的DropDownLists也是重要的。所以你告訴我把所有進入或者去往SQL連接的數據放到一個存儲爲會話參數的DataTable中?2)我會嘗試3)我認爲我在任何地方都可以使用索引,但是我會再次檢查它4)這與點1) – Barnabeck

+0

將所有更新操作打包到單個SQL連接中改進了很多。但我找不到任何明確指出Rows [index] .Cells [0] .Controls [1]在Rows [index] .cells [0] .FindControl(「TOTAL」)上的性能的參考。 – Barnabeck

+0

關於索引,可能忽略不計的性能改進,但顯然它會比findcontrol更快,因爲它會遍歷所有控件。如果你看起來不錯,你能否接受答案? –