【发布时间】:2011-09-15 09:01:29
【问题描述】:
我在处理更新网格视图的功能时遇到问题。到目前为止,我一直在使用 DataSource 控件,但这次我需要从头开始构建所有内容。我希望能够仅更新 gridview 的最后一个数据列。我的问题是: 1)当我单击编辑按钮时,行中的所有数据字段都转换为文本框 - 我只希望最后一个这样做 2)当我单击更新时,我得到“对象引用未设置为对象的实例”。错误,似乎“TextBox tProgress = (TextBox)grdMilestones.Rows[e.RowIndex].Cells[3].FindControl("mstCompletition");”只是找不到正确的值,因为当我用数字替换它时它工作正常。
这是我的网格视图
<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();
}
问候
巴托斯
【问题讨论】: