c#非常慢的Gridview更新。

时间:2016-12-02 08:21:48

标签: c# asp.net gridview

我有一个Gridview,其中包含80行中的几个TextBox,Dropdown Menus和Checkbox,以及与网格相关的OnRowEdit和OnRowDataBound事件。 这就是我更新网格的方法:

  1. ImageButton单击行切换到EditMode(7秒)并根据所选行与其他行的关系填充每行的复选框
  2. ImageButton点击该行保存已编辑的数据(13秒)
    • 行:click事件收集控制数据并将行数据保存到SQL
    • 列:遍历所有行,获取复选框的状态并将其保存到SQL
  3. 我是一个自学者,很有可能会有所改善。如果有人请看一看,并指出关键的耗时操作是什么以及如何加快速度。 - 谢谢,马丁

    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 个答案:

没有答案