0
我有處理函數來更新我的GridView的問題。到目前爲止,我一直在使用DataSource控件,但是這次我需要從頭開始構建所有的東西。我希望能夠更新gridview的最後一個數據列。我的問題是: 1)當我點擊編輯按鈕,行中的所有數據字段轉換成文本框 - 我只希望最後一個這樣做 2)當我點擊更新時,我得到「對象引用未設置爲實例目的。」錯誤,似乎「TextBox tProgress =(TextBox)grdMilestones.Rows [e.RowIndex] .Cells [3] .FindControl(」mstCompletition「);」只是找不到一個正確的值,因爲當我用一個數字代替它時,它工作正常。asp.net更新gridview問題
這是我的GridView
<asp:GridView ID="grdMilestones" runat="server" Width="940px" DataKeyNames="mstNo"
OnRowEditing="grdMilestones_RowEditing"
OnRowCancelingEdit="grdMilestones_RowCancelingEdit"
OnRowUpdating="grdMilestones_RowUpdating" AutoGenerateColumns="False"
CssClass="milestonesGrid">
<Columns>
<asp:BoundField DataField="proName" HeaderText="Project Name" />
<asp:BoundField DataField="mstDescription" HeaderText="Milestone Description" />
<asp:BoundField DataField="mstPersResp" HeaderText="Milestone Leader" />
<asp:BoundField DataField="mstCompletition" HeaderText="Progress" DataFormatString{0:F1}%" />
<asp:CommandField ButtonType="Button" ShowEditButton="true" EditText="Update progress" />
</Columns>
</asp:GridView>
背後
protected void grdMilestones_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = Int32.Parse(grdMilestones.DataKeys[e.RowIndex].Value.ToString());
TextBox tProgress = (TextBox)grdMilestones.Rows[e.RowIndex].Cells[3].FindControl("mstCompletition");
int prog = Convert.ToInt32(tProgress.Text.Trim());
//ds.Rows[row.DataItemIndex]["mstCompletition"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
string updateSQL = "UPDATE milestonesPM SET mstCompletition = @mstCompletition WHERE mstNo = @mstNo";
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(updateSQL, con);
cmd.Parameters.Add("@mstNo", SqlDbType.Int).Value = id;
cmd.Parameters.Add("@mstCompletition", SqlDbType.SmallInt).Value = prog;
con.Open();
cmd.ExecuteNonQuery();
grdMilestones.EditIndex = -1;
grdMilestones.DataBind();
con.Close();
fillGrid();
}
問候代碼
巴爾託什
感謝的答案,但我仍然有落後 任何幫助,請與代碼的問題! Bartosz – Bartosz
我自己找到了答案。 – Bartosz
非常感謝你 – Bartosz