我有一個GridView與80個行中的每一箇中的幾個文本框,下拉菜單和複選框以及綁定到網格的OnRowEdit和OnRowDataBound事件。 這是我做的更新網格:c#很慢的Gridview更新。
- 的ImageButton單擊行切換到編輯模式(7秒),並填充每行的複選框,根據所選行已與他人的關係
- 的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;
}
你使用的是經典的asp.net還是mvc? –
ddl2的數據源是什麼? – TZHX
爲什麼這麼難?嘗試爲您的數據庫提供者使用DataAdapter。 – GrApDev