简体   繁体   中英

c# Very slow Gridview update.

I have a Gridview with several TextBoxes, Dropdown Menus and Checkboxes in each of the 80 rows and a OnRowEdit and OnRowDataBound Event tied to the grid. This is what I do to update the grid:

  1. ImageButton Click in the row switches to EditMode (7 seconds) and populates the checkbox for each row, according to the relation the selected row has with the others
  2. ImageButton Click in the row saves the edited Data (13 seconds)
    • Row: The click event collects the control data and saves row data to SQL
    • Column: Loop through all rows, get the state of a checkbox and save it to SQL

I'm an autodidact and there are most likely things that con be improve. If somebody please could have a look and point out what the critical time consuming operation is and how to speed things up. - Thanks, Martin

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);
            }
        }
    }

Saving Row Data:

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;            
    }

Saving Column Data:

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;
    }

Here are possible changes.

1) Opening SQL connection thrice (updatetrigger, rowdatabound and UpdateMilesStones). Please avoid that. Try to do in one operation only.

2) Use using block for SQL Connection check this link

3) You can also use index instead of findcontrol if column orders are going to be fixed.

4) You can also keep data in memory/cache and at the end you will save all the data in SQL.

Please try these things. It will definitely improve the performance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM