开发者

asp.net update gridview problem

I have a problem with handling the function to update my gridview. Until now I've been using DataSource control but this time I need to build everything from scratch. I want to be able to update just the very last data column of the gridview. My problems are: 1) When I click edit button all data fields in the row convert into textboxes - I would like only last one to do so 2) When I click update I get "Object reference not set to an instance of an object." error, and it seems that "TextBox tProgress = (TextBox)grdMilestones.Rows[e.RowIndex].Cells[3].FindControl("mstCompletition");" just can't find a correct value because when I substitute it with a number it works fine.

This is my 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="Miles开发者_StackOverflow中文版tone 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>

And a code behind

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

Regards

Bartosz


You need to make the columns you don't want to edit read-only, and create an EditItemTemplate for the one you want to edit:

<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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜