locked
unable to edit the Grid View RRS feed

  • Question

  • User1051635487 posted

    Hi All,

    When I click on edit button I am expecting it to change like a text box and let me edit the content. But it is not happening. Can you see my code and let me know What I am missing?

    <asp:GridView ID="gvSearch" runat="server" AutoGenerateColumns="False" AllowPaging="True"
    AllowSorting="true" Width="650px" PageSize="20" OnRowCommand="gvSearch_RowCommand" OnRowDataBound="gvSearch_RowDataBound"
     OnRowEditing="gvSearch_RowEditing"  OnRowUpdating="gvSearch_RowUpdating"
    >

    <Columns>
     
            
               <asp:TemplateField HeaderText="User Id" SortExpression="User Id">
            
                <HeaderStyle Wrap="False"  Font-Bold="True" HorizontalAlign="Center"
                     VerticalAlign="Top" />
                <ItemStyle Wrap="False" HorizontalAlign="Left" />
                     <ItemTemplate>       
                        <asp:Label ID="lblusrId" runat="server"  Text='<%# Eval("UserId")%> '></asp:Label>
                      </ItemTemplate>
            
            </asp:TemplateField>
            
            <asp:TemplateField HeaderText="First Name" SortExpression="FirstName">
            
                <HeaderStyle Wrap="False"  Font-Bold="True" HorizontalAlign="Center"
                     VerticalAlign="Top" />
                <ItemStyle Wrap="False" HorizontalAlign="Left" />
                     <ItemTemplate>       
                        <asp:Label ID="lblFname" runat="server"  Text='<%# Eval("FirstName")%> '></asp:Label>
                      </ItemTemplate>
            
            </asp:TemplateField>
            
              <asp:TemplateField HeaderText="Last Name" SortExpression="LastName">
            
                <HeaderStyle Wrap="False"  Font-Bold="True" HorizontalAlign="Center"
                     VerticalAlign="Top" />
                <ItemStyle Wrap="False" HorizontalAlign="Left" />
                     <ItemTemplate>       
                        <asp:Label ID="lblLname" runat="server"  Text='<%# Eval("LastName")%>'></asp:Label>
                      </ItemTemplate>
            
            </asp:TemplateField>
            
              <asp:TemplateField HeaderText="MiddleName" SortExpression="CreatedOn">
            
                <HeaderStyle Wrap="False"  Font-Bold="True" HorizontalAlign="Center"
                     VerticalAlign="Top" />
                <ItemStyle Wrap="False" HorizontalAlign="Left" />
                     <ItemTemplate>       
                        <asp:Label ID="lblMname" runat="server"  Text='<%# Eval("MiddleName")%>'></asp:Label>
                      </ItemTemplate>
            
            </asp:TemplateField>
            
                   <asp:TemplateField HeaderText="Employee Id" SortExpression="EmployeeId">
            
                <HeaderStyle Wrap="False"  Font-Bold="True" HorizontalAlign="Center"
                     VerticalAlign="Top" />
                <ItemStyle Wrap="False" HorizontalAlign="Left" />
                     <ItemTemplate>       
                        <asp:Label ID="lblEmpId" runat="server"  Text='<%# Eval("EmployeeId")%>'></asp:Label>
                      </ItemTemplate>
            
            </asp:TemplateField>
            
                  
            
                    <asp:TemplateField HeaderText="User Status" SortExpression="NetId">
            
                <HeaderStyle Wrap="False"  Font-Bold="True" HorizontalAlign="Center"
                     VerticalAlign="Top" />
                <ItemStyle Wrap="False" HorizontalAlign="Left" />
                     <ItemTemplate>       
                        <asp:Label ID="lblUsrStatus" runat="server"  Text='<%# Eval("UserStatus")%>'></asp:Label>
                      </ItemTemplate>
            
            </asp:TemplateField>
            
            
        
            
                <asp:TemplateField HeaderText="View User Details" SortExpression="View">


                     <HeaderStyle Wrap="False" Font-Bold="True" HorizontalAlign="Center"
                     VerticalAlign="Top" />
                     <ItemStyle Wrap="True" HorizontalAlign="Left" />
                    <ItemTemplate>
                    
                    <asp:LinkButton ID="lbView"  Text="View Details" runat="server" CommandName="View" CommandArgument='<%#Eval("FirstName")+";"+Eval("LastName")+";"+Eval("UserId")%>' ></asp:LinkButton>
                    </ItemTemplate>
               </asp:TemplateField>   
               
            <asp:TemplateField HeaderText="View User Details" SortExpression="View">
                     <HeaderStyle Wrap="False" Font-Bold="True" HorizontalAlign="Center"
                     VerticalAlign="Top" />
                     <ItemStyle Wrap="True" HorizontalAlign="Left" />
                    <ItemTemplate>
                    
                    <asp:LinkButton ID="lbledit"  Text="Edit" runat="server" CommandName="Edit" CommandArgument='<%#Eval("FirstName")+";"+Eval("LastName")+";"+Eval("UserId")%>' ></asp:LinkButton>
                    </ItemTemplate>
               </asp:TemplateField>   
                    
            
    </Columns>
    </asp:GridView>


    protected void gvSearch_RowCommand(object source, GridViewCommandEventArgs e)
            {
                if (e.CommandName == "View")
                {
                    string arg = Convert.ToString(e.CommandArgument);

                  //  Response.Redirect("~/CFEmpDetails.aspx?arg=" + arg);

                }

                if (e.CommandName == "Edit")
                {
                   


                }
            }
            protected void gvSearch_RowDataBound(Object sender, GridViewRowEventArgs e)
            {

                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                }
            }

            protected void gvSearch_RowEditing(object sender, GridViewEditEventArgs e)
            {
            }
            protected void gvSearch_RowUpdating(object sender, GridViewUpdateEventArgs e)
            {
               

            }




    Wednesday, March 24, 2010 10:50 AM

Answers

All replies

  • User626880745 posted

    When I click on edit button I am expecting it to change like a text box and let me edit the content

    that would happen IF you were using an EditItemTemplate.

    also since you're doing the databinding manually (vs. a datasource contol) you'd have to set up the grid in editmode with the editindex and binding to the datasource again in the RowEditing event.

    follow through this example: http://www.aspdotnetcodes.com/GridView_Insert_Edit_Update_Delete.aspx

    Wednesday, March 24, 2010 11:35 AM
  • User1051635487 posted

    This article is good but it is not helping in this context. Can anyone tell me what am I missing? 

    Wednesday, March 24, 2010 12:35 PM
  • User626880745 posted

    I sent you to a link as you could then compare your code; you're missing more than one thing.

    essentially, the EditItemTemplate and binding to it (but after you've set the EditIndex). have a look (I think this is more clear): http://www.codeproject.com/KB/webforms/editable_gridview_control.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 24, 2010 12:48 PM
  • User1051635487 posted

    Hi Pete,

    By reading through the link you gave me I was able to get the edit mode of the grid view.

    Now I added 2 link buttons 1. edit and 2. save

    I am able to edit the fields but when I hit on Save I am getting the old data only. I am not able to get the new edited data.

    any help on that Part? 

    Wednesday, March 24, 2010 10:59 PM
  • User626880745 posted

    Hi Pete,

    By reading through the link you gave me I was able to get the edit mode of the grid view.


    Kris,

    IF you're original issue is resolved go ahead and mark the post(s) that helped you.

    On the current issue, yes, you need to make sure that you binding your data under a !IPostBack check

    ..page_load (object sender .....
    {

        if (!IsPostBack)
       {
        GridView1.DataSource = somedatatable;
        GridView1.DataBind();
       }


    * the policy is to open new threads for any/every issue that is not connected to the original post. it would also help you getting quicker responses from others. *

    Wednesday, March 24, 2010 11:08 PM
  • User1052662409 posted

    may this help you

    http://forums.asp.net/p/1533578/3722612.aspx#3722612

    thanks

    Wednesday, March 24, 2010 11:43 PM