gridview更新/插入列

时间:2013-12-30 10:24:44

标签: c# asp.net

我正在编辑和更新gridview列,但它没有获取值。并且它没有显示错误。

请告诉我这个建议。怎么做更新。如果点击编辑就启用。当我输入值时,它不会更新数据库。

if (ViewState["LeaveManagementID"] == null)

        {

                foreach (GridViewRow row in gvLeaves.Rows)
                {

                    EP_LeaveManagementRow leaveUpdateRow = db.EP_LeaveManagementCollection.GetRow("LeaveManagementID=" + gvLeaves.DataKeys[e.RowIndex].Value);

                    if (leaveUpdateRow != null)
                    {
                        leaveUpdateRow.NoOfDays = int.Parse(lblNoOfDays.Text.ToString());


                        db.EP_LeaveManagementCollection.Update(leaveUpdateRow);
                        ViewState["LeaveManagementID"] = leaveUpdateRow.LeaveManagementID;

                        mvLeaveManagement.SetActiveView(vwLeavesDetails);

ASPX

   <asp:TemplateField HeaderText="Comments">
                                            <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle"></ItemStyle>
                                            <HeaderStyle Width="15%" HorizontalAlign="Left" VerticalAlign="Middle"></HeaderStyle>
                                             <ItemTemplate>
                                                        <asp:Label ID="lblComments" Visible="false" runat="server" ></asp:Label>

                                                    </ItemTemplate>
                                            <EditItemTemplate>
                                            <asp:TextBox ID="txtComments" runat="server" MaxLength="50" TextMode="MultiLine"  Text='<%#Bind("LeaveCommants") %>'></asp:TextBox>
                                            </EditItemTemplate>

                                        </asp:TemplateField>


 <asp:TemplateField HeaderText="Edit/Update" HeaderStyle-Width="25%">
                                                <ItemTemplate>

                                                    <asp:LinkButton ID="lkbEdit" Text="Edit" runat="server" CommandName="Edit"  />
                                                    <%-- <asp:LinkButton ID="lkbUpdate" Text="Update" runat="server" CommandName="Update"  />--%>


                                                </ItemTemplate>
                                               <EditItemTemplate>

                                                    <asp:LinkButton ID="btnUpdate" Text="Update" runat="server" CommandName="Update" />
                                                    <asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" />

                                                </EditItemTemplate>
                                            </asp:TemplateField>

1 个答案:

答案 0 :(得分:1)

如果您想使用GridView插入删除并将数据更新到数据库,请使用以下链接: - http://csharpdotnetfreak.blogspot.com/2009/05/gridview-sqldatasource-insert-edit.html