2011-09-15 87 views
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(); 
} 

問候代碼

巴爾託什

回答

0

你需要做的列你不想編輯閱讀-only,併爲您想要編輯的人創建一個EditItemTemplate

<asp:BoundField DataField="proName" 
     ReadOnly="true" 
     HeaderText="Project Name" /> 
<asp:TemplateField HeaderText="Progress"> 
    <ItemTemplate> 
     <asp:Label ID="mstCompletitionLabel" Text='<%# Eval("Description") %>' 
      runat="server" /> 
    </ItemTemplate> 
    <EditItemTemplate> 
     <asp:TextBox ID="mstCompletitionTextBox" CssClass="textBox" 
      runat="server" 
      TabIndex="12" 
      Width="175" 
      Text='<%# Eval("mstCompletition") %>' 
      MaxLength="20"> 
     </asp:TextBox> 
     <asp:RequiredFieldValidator ID="mstCompletitionValidator" 
      runat="server" 
      ErrorMessage="Required" 
      Text="*" 
      ControlToValidate="mstCompletitionTextBox" 
      SetFocusOnError="true" 
      Display="Static"> 
     </asp:RequiredFieldValidator>              
    </EditItemTemplate> 
</asp:TemplateField> 
+0

感謝的答案,但我仍然有落後 任何幫助,請與代碼的問題! Bartosz – Bartosz

+0

我自己找到了答案。 – Bartosz

+0

非常感謝你 – Bartosz