Pages

Men

rh

3/15/2013

How to delete a dynamic row in Gridview using C# with Asp.Net.

How to delete a dynamic row in Gridview  using C# with Asp.Net.

Aspx Page  :

<asp:GridView ID="gdvSupplierAccess" runat="server" ShowFooter="true" Width="650px"
    CellPadding="4" CssClass="GRD" AutoGenerateColumns="false">
    <AlternatingRowStyle CssClass="GRDALTERNATEITEM" />
    <Columns>
        <asp:BoundField DataField="RowNumber" HeaderText="RowNumber" HeaderStyle-Width="210px"
            Visible="false" SortExpression="UserID" />
        <asp:TemplateField HeaderText="Select" HeaderStyle-Width="50px">
            <ItemTemplate>
                <asp:UpdatePanel ID="UpdSelect" runat="server">
                    <ContentTemplate>
                        <asp:CheckBox ID="ChkSelect" runat="server" CssClass="CheckBox" />
                        <asp:TextBox ID="txtRowNo" runat="server" Visible="false" Text='<%# Bind("RowNumber") %>'></asp:TextBox>
                    </ContentTemplate>
                </asp:UpdatePanel>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Product1" HeaderStyle-Width="210px">
            <ItemTemplate>
                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <ContentTemplate>
                        <asp:DropDownList ID="ddlProduct1" runat="server" CssClass="DROPDOWN">
                            <asp:ListItem Value="-1">Select</asp:ListItem>
                        </asp:DropDownList>
                    </ContentTemplate>
                </asp:UpdatePanel>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Product2" HeaderStyle-Width="210px">
            <ItemTemplate>
                <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                    <ContentTemplate>
                        <asp:DropDownList ID="ddlProduct2" runat="server" CssClass="DROPDOWN">
                            <asp:ListItem Value="-1">Select</asp:ListItem>
                        </asp:DropDownList>
                    </ContentTemplate>
                </asp:UpdatePanel>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Product3" HeaderStyle-Width="210px">
            <ItemTemplate>
                <asp:UpdatePanel ID="UpdatePanel3" runat="server">
                    <ContentTemplate>
                        <asp:DropDownList ID="ddlProduct3" runat="server" CssClass="DROPDOWN">
                            <asp:ListItem Value="-1">Select</asp:ListItem>
                        </asp:DropDownList>
                    </ContentTemplate>
                </asp:UpdatePanel>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Button ID="btnAddNewRow" runat="server" Text="Add Row" OnClick=" btnAddNewRow_Click"
                    CssClass="button" />
                &nbsp;&nbsp;&nbsp;
                <asp:Button ID="btnDeleteRow" runat="server" Text="Delete" OnClick="btnDeleteRow_Click"
                    CssClass="button" />
            </FooterTemplate>
        </asp:TemplateField>
    </Columns>
    <EditRowStyle CssClass="GRDITEM" />
    <FooterStyle CssClass="GRDFOOTER" />
    <HeaderStyle CssClass="GRDHEADER" />
    <PagerStyle CssClass="GRDPAGER" />
    <SelectedRowStyle CssClass="GRDSELECTEDITEM" />
</asp:GridView>
 


 C# Code :-


 protected void btnDeleteRow_Click(object sender, EventArgs e)
        {
           try
           {
                DeleteRow();
           }
           catch (Exception ex)
           {
             
           }
        }
        protected void DeleteRow()
        {
            String strRowNumber;
            try
            {
            if (ViewState["CurrentTable"] != null)
            {
                DataTable dt = (DataTable)ViewState["CurrentTable"];

                if (dt.Rows.Count > 1)
                {
                    foreach (GridViewRow row in gdv.Rows)
                    {
                        CheckBox chkSelect = (CheckBox)row.Cells[0].FindControl("ChkSelect");
                        if (chkSelect.Checked == true)
                        {
                            TextBox txtRowNo = (TextBox)row.Cells[0].FindControl("txtRowNo");
                            strRowNumber = txtRowNo.Text;
                            for (int k = 0; k < dt.Rows.Count; k++)
                            {
                                DataRow dr = dt.Rows[k];
                                if (dr["RowNumber"].ToString() == strRowNumber.ToString())
                                {
                                    dr.Delete();
                                    dt.AcceptChanges();
                                    ViewState["CurrentTable"] = "";
                                    ViewState["CurrentTable"] = dt;
                                  
                                }
                            }
                        }

                        gdv.DataSource = dt;
                        gdv.DataBind();

                    }
                  
                    BindGridData();

                }
            }
            }
            catch (Exception ex)
            {
                lblMessage.Text = "";
                lblMessage.Text = objUtility.LogErrorIntoDB(ex);
                Response.Redirect("CustomErrorPage.aspx", false);
            }
        }

        protected void BindGridData()
        {
            int rowIndex = 0;
            try
            {
            if (ViewState["CurrentTable"] != null)
            {
                DataTable dt = (DataTable)ViewState["CurrentTable"];
                int introw = dt.Rows.Count;
                if (dt.Rows.Count > 0)
                {

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        DropDownList ddlProduct1 = (DropDownList)gdvProduct1Access.Rows[rowIndex].Cells[1].FindControl("ddlProduct1");
                        DropDownList ddlProduct2 = (DropDownList)gdvProduct1Access.Rows[rowIndex].Cells[2].FindControl("ddlProduct2");
                        DropDownList ddlProduct3 = (DropDownList)gdvProduct1Access.Rows[rowIndex].Cells[3].FindControl("ddlProduct3");
                        BindProduct1Info(ddlProduct1);
                        BindProduct2AreaInfo(ddlProduct2);
                        BindProduct3Info(ddlProduct);
                        if (i < dt.Rows.Count)
                        {
                            ddlProduct1.ClearSelection();
                            ddlProduct1.Items.FindByText(dt.Rows[i]["Product1Name"].ToString()).Selected = true;
                            ddlProduct2.ClearSelection();
                            ddlProduct2.Items.FindByText(dt.Rows[i]["Product2Area"].ToString()).Selected = true;
                            ddlProduct3.ClearSelection();
                            ddlProduct3.Items.FindByText(dt.Rows[i]["Product3"].ToString()).Selected = true;
                        }
                        rowIndex++;

                    }
                   
                }


            }
            }
            catch (Exception ex)
            {
              
            }
        }





No comments :

Post a Comment