更新gridview行不会保存数据

时间:2013-01-22 01:27:41

标签: asp.net

更新gridview时遇到问题!一切似乎都在起作用。但是当我点击完成编辑时,数据将无法保存!

但是当我点击编辑正确的字段时,提示我输入新值,但它不会保存!

这是我的asp

<asp:GridView ID="GridView1" runat="server" CssClass="report" 
    AutoGenerateColumns="False" onrowediting="GridView1_RowEditing" 
    DataKeyNames="TimeID" onrowupdating="GridView1_RowUpdating" 
    onrowcommand="GridView1_RowCommand" 
    onrowcancelingedit="GridView1_RowCancelingEdit">

    <Columns> 



        <asp:BoundField DataField="date" Visible="true" ReadOnly="true" HeaderText="Date" />
        <asp:BoundField DataField="Description" HeaderText="Stage Description" ReadOnly="True" />


       <asp:TemplateField HeaderText="Start Time">
            <ItemTemplate>
                <asp:Label ID="Label7" runat="server"  Text='<%# ConvertToShotTime(Eval("StartTime")) %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtStartTime" ValidationGroup="1" Width="90px"  class="TimeEntry" runat="server"  Text='<%# ConvertToShotTime(Eval("StartTime")) %>'></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator6" ControlToValidate="txtStartTime" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
           </EditItemTemplate>
        </asp:TemplateField>




        <asp:TemplateField HeaderText="End Time">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server"  Text='<%# ConvertToShotTime(Eval("EndTime")) %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtEndTime" class="TimeEntry"  ValidationGroup="1" Width="90px" runat="server"  Text='<%# ConvertToShotTime(Eval("EndTime")) %>'></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator66" ControlToValidate="txtEndTime" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
            </EditItemTemplate>
        </asp:TemplateField>



        <asp:BoundField DataField="TimeInHours" HeaderText="Time Time (Hours)" ReadOnly="True" />

        <asp:CommandField ShowEditButton="true" ShowCancelButton="true" 
            ButtonType="Image" EditImageUrl="~/images/edit_record.jpg" 
            CancelImageUrl="~/images/edit_no.jpg" 
            UpdateImageUrl="~/images/update_record.jpg" />

        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
              <asp:ImageButton ID="lnbCopy" runat="server" AlternateText="Delete" 
                    CommandName="DeleteRecord"  CommandArgument='<%# Bind("TimeID") %>'  OnClientClick="return confirm('Are you sure you want to delete this row?');"  ImageUrl="~/images/delete_record.jpg" />
            </ItemTemplate>

           <EditItemTemplate>
                <asp:Label ID="lblTimeID" runat="server" Text='<%# Eval("TimeID") %>'></asp:Label>
            </EditItemTemplate>
        </asp:TemplateField>









    </Columns>


</asp:GridView>

这是我在代码背后所做的事情

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) {
    GridView1.EditIndex = e.NewEditIndex;
    loadTable();
}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) {
    GridView1.EditIndex = -1;
    loadTable();
}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) {

    GridViewRow row = GridView1.Rows[e.RowIndex] as GridViewRow;


    var StartTime = row.FindControl("txtStartTime") as TextBox;
    var EndTime = row.FindControl("txtEndTime") as TextBox;
    var id = row.FindControl("lblTimeID") as Label;

    SqlConnection conn = new SqlConnection(conStr.GetConnectionString("myServer1"));

    SqlCommand comm = new SqlCommand();

    comm.CommandText = "UPDATE CDSTimeSheet SET StartTime = @StartTime, EndTime = @EndTime ,timeElapsed = datediff(minute,@startTime , @EndTime), timeInSeconds = datediff(second,@startTime , @EndTime) WHERE TimeID = @id";
    comm.Connection = conn;

    comm.Parameters.AddWithValue("@id", id.Text);
    comm.Parameters.AddWithValue("@StartTime", StartTime.Text);
    comm.Parameters.AddWithValue("@EndTime", EndTime.Text);

    conn.Open();
    comm.ExecuteNonQuery();
    conn.Close();
    GridView1.EditIndex = -1;
    loadTable();
}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

听起来很像你在更新基础数据后没有在你的网格上调用DataBind。执行更新后(可能在我看到的loadTable方法中),请调用

GridView1.DataBind();

那应该刷新网格中的数据。


发表评论后,我发现您没有使用NewValues参数中的GridViewUpdateEventArgs集合来获取实际传入的更新字段值。试试这个:

String StartTime = e.NewValues["StartTime"].ToString();
String EndTime = e.NewValues["EndTime"].ToString();
String id = e.Keys[0].ToString(); 

请注意,这些变量现在是字符串,而不是TextBox s,因此当您将它们添加为参数时,不需要在它们上添加“.Text”。