locked
Editing Child-Nested GridView within a main GridView in c# RRS feed

  • Question

  • User1151703306 posted

    Hello there, I need your help.

    I have inserted a child GridView (Nested GridView) inside a main GridView.

    This is the tutorial

    Now I trying to edit the row on child GridView.

    I have two problems :

    1. The first problem is when I change edit row for new edit row on main GridView, the last row remains open on child GridView and not closing for editing only the new row;
    2. The second problem is that for edit row on child GridView requires two clicks.

    This is the code of RowEditing

    How to do resolve this ?

    Thank you in advance for help.

    protected void gv1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView gv2 = (GridView)gv1.Rows[e.NewEditIndex].FindControl("GridView2");
        gv2.EditIndex = -1;
        gv1.EditIndex = e.NewEditIndex;
        BindData();
    }

    Thursday, September 19, 2019 10:20 AM

Answers

  • User288213138 posted

    Hi Golia,

    The first problem is when I change edit row for new edit row on main GridView, the last row remains open on child GridView and not closing for editing only the new row;

    This problem i had answered in this thread:https://forums.asp.net/p/2159585/6278673.aspx?p=True&t=637045448660286055

    The second problem is that for edit row on child GridView requires two clicks.

    The reason you need to click 2 times is because nothing was done in the GridView2_RowEditing event.

    The code:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand"
     OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing">
                    <Columns>
                        <asp:CommandField ButtonType="Link" ShowEditButton="true" />
                        <asp:TemplateField HeaderText="Details">
                            <ItemTemplate>
                                
                                <asp:Button ID="btn_Show" Text="Details" runat="server" CommandName="Details" CommandArgument='<%#Container.DataItemIndex%>' />
                                <asp:Button ID="Cancel" Text="Cancel" runat="server" CommandName="Cancel" CommandArgument='<%#Container.DataItemIndex%>' Visible="false" />
                                <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" OnRowEditing="GridView2_RowEditing" OnRowCancelingEdit="GridView2_RowCancelingEdit">
                                    <Columns>
                                        <asp:TemplateField HeaderText="Edit" ItemStyle-HorizontalAlign="Center">
                                            <ItemTemplate>
                                                Edit:
                                                <asp:ImageButton ID="imgbtnEdit" runat="server" CommandName="Edit" ImageUrl="~/Images/plus.PNG" />
                                            </ItemTemplate>
    
                                            <EditItemTemplate>
                                                Updata:<asp:ImageButton ID="imgbtnUpdate" runat="server" CommandName="Update" ImageUrl="~/Images/plus.PNG" />
                                                Cancle:<asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/Images/minus.PNG" />
                                            </EditItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Id">
                                            <ItemTemplate>
                                                <asp:Label ID="lblId" Text='<%# Eval("Id") %>' runat="server"></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField ItemStyle-Width="100px" HeaderText="Name">
                                            <ItemTemplate>
                                                <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name")%>'></asp:Label>
                                            </ItemTemplate>
                                            <EditItemTemplate>
                                                <asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name")%>'></asp:TextBox>
                                           <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtName"
                                            SetFocusOnError="true" ErrorMessage="txtName : Required"></asp:RequiredFieldValidator>
    
                                            </EditItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField ItemStyle-Width="100px" HeaderText="Address">
                                            <ItemTemplate>
                                                <asp:Label ID="lblAddress" runat="server" Text='<%# Eval("Address")%>'></asp:Label>
                                            </ItemTemplate>
                                            <EditItemTemplate>
                                                <asp:TextBox ID="txtAddress" runat="server" Text='<%# Bind("Address")%>'></asp:TextBox>
                                            </EditItemTemplate>
                                        </asp:TemplateField>
    
                                    </Columns>
                                </asp:GridView>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Department_Id">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Dept_Id") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Dept_Id") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Department_Name">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Dept_Name") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("Dept_Name") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>                  
                    </Columns>
                </asp:GridView>
    
    string connStr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;                      
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    GridView1.DataSource = GetData("select * from Departments");
                    GridView1.DataBind();
                }
            }    
            private static DataTable GetData(string query)
            {
                string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
                using (SqlConnection con = new SqlConnection(strConnString))
                {
                    using (SqlCommand cmd = new SqlCommand())
                    {
                        cmd.CommandText = query;
                        using (SqlDataAdapter sda = new SqlDataAdapter())
                        {
                            cmd.Connection = con;
                            sda.SelectCommand = cmd;
                            using (DataSet ds = new DataSet())
                            {
                                DataTable dt = new DataTable();
                                sda.Fill(dt);
                                return dt;
                            }
                        }
                    }
                }
            }
            protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                int rowindex = Convert.ToInt32(e.CommandArgument.ToString());
                GridView g2 = (GridView)GridView1.Rows[rowindex].FindControl("GridView2");
                Label lbl = (Label)GridView1.Rows[rowindex].FindControl("Label2");
                GridView1.Rows[rowindex].FindControl("Cancel").Visible = false;
                if (e.CommandName == "Details")
                {
                    GridView1.Rows[rowindex].FindControl("Cancel").Visible = true;
                    GridView1.Rows[rowindex].FindControl("btn_Show").Visible = false;              
                    g2.DataSource = GetData("select * from Employees");
                    g2.DataBind();
                    g2.Visible = true;
                }
                else
                {
                    g2.Visible = false;
                    GridView1.Rows[rowindex].FindControl("btn_Show").Visible = true;
                }
            }
    
            protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
            {
                GridView g2 = (GridView)sender;
                g2.EditIndex = e.NewEditIndex;
                g2.DataSource = GetData("select * from Employees");
                g2.DataBind();
            }
    
            protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
            {
                GridView g2 = (GridView)sender;
                g2.EditIndex = -1;
                g2.DataSource = GetData("select * from Employees");
                g2.DataBind();                
            }
    
    
            protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
            {
                //GridView1.EditIndex = -1;
                //GridView1.DataSource = GetData("select * from Departments");
                //GridView1.DataBind();
            }
    
            protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
            {
                GridView gv2 = (GridView)GridView1.Rows[e.NewEditIndex].FindControl("GridView2");
                gv2.EditIndex = -1;
                GridView1.EditIndex = e.NewEditIndex;
                GridView1.DataSource = GetData("select * from Departments");
                GridView1.DataBind();
            }

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 20, 2019 10:01 AM