Wednesday 2 December 2020

Add Dynamic Template filed and controls in Grid-view footer-Row asp.net

 ////////// Html
<div style="width: 100%; overflow: auto;">
            <button type="button" onclick="ClearRows()">Add</button>
            <asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
                runat="server" AutoGenerateColumns="true" OnRowDataBound="OnRowDataBound" ShowFooter="true">
                <Columns>
                    <asp:TemplateField>
                        <%--<HeaderTemplate>
                            <button type="button" onclick="ClearRows()">Add</button>
                        </HeaderTemplate>--%>
                        <ItemTemplate>
                            <asp:LinkButton runat="server" ID="MM" Text="Edit" OnClick="CLick_EditCLick">                       
                            </asp:LinkButton>
                        </ItemTemplate>
                        <%-- <FooterTemplate>
                            <asp:Button runat="server" ID="jjj" Text="CLick Me" OnClick="Click_ClickME" />
                        </FooterTemplate>--%>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
           <asp:Button runat="server" ID="jjj" Text="CLick Me" OnClick="Click_ClickME" />
     </div>
////////////// cs
protected void Page_Load(object sender, EventArgs e){
        if (!this.IsPostBack){}
        Bind_Gridview();
    }
 
    public void Populate_DynamicFooter(GridView gv)
    {
        DataTable dt = Populate_Datatable();
        for (int i = 0; i < dt.Columns.Count; i++)
        {
            if (i > 0){
                TextBox txtb = new TextBox
                {
                    ID = "txt" + dt.Columns[i].ColumnName,
                    Text = Convert.ToString(dt.Rows[0][i])
                };
                gv.FooterRow.Cells[i].Controls.Add(txtb);
            }
        }
    }
    public void Populate_DynamicFooter1(GridViewRow gr)
    {
        for (int i = 0; i < gr.Cells.Count; i++)
        {
            if (i > 0)
            {
                TextBox txtb = new TextBox
                {
                    ID = "txt_" + i.ToString(),
                    Text = ""
                };
                gr.Cells[i].Controls.Add(txtb);
            }
        }  
    }
 
   
    public void Bind_Gridview()
    {
        DataTable dt = Populate_Datatable();
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    public DataTable Populate_Datatable()
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[8] {
                        new DataColumn("Name", typeof(string)),
                        new DataColumn("Age",typeof(string)),
                        new DataColumn("Gender",typeof(string)),
                        new DataColumn("Country",typeof(string)) ,
                        new DataColumn("State",typeof(string)),
                        new DataColumn("District",typeof(string)),
                        new DataColumn("Block",typeof(string)),
                        new DataColumn("Village",typeof(string)),
 
 
        });
 
        dt.Rows.Add("Jai Hind Jai Bharat", "32", "Male", "India", "UP", "", "", "");
        dt.Rows.Add(2, "", "", "", "India", "UP", "", "");
        dt.Rows.Add(3, "", "", "", "India", "UP", "", "");
        dt.Rows.Add(4, "", "", "", "India", "UP", "", "");
        dt.Rows.Add(5, "", "", "", "India", "UP", "", "");
        return dt;
 
    }
 
    protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Populate_DynamicFooter1( e.Row);
        }
    }
 
    protected void Click_ClickME(object sender, EventArgs e)
    {
        string ll = "";
        for (int i = 0; i < GridView1.FooterRow.Cells.Count; i++)
        {
            if (i > 0)
            {
                TextBox txtb = ((TextBox)GridView1.FooterRow.Cells[i].FindControl("txt_" + i.ToString()));
                if (txtb != null)
                {
                    ll = ll + (ll.Length > 0 ? "," : "") + txtb.Text;
                }
            }
        }
        ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + ll + "')", true);
    }
 
    protected void CLick_EditCLick(object sender, EventArgs e)
    {
        LinkButton b = (sender) as LinkButton;
        GridViewRow gr = ((GridViewRow)b.NamingContainer);
        int idx = gr.RowIndex;
        for (int i = 0; i < GridView1.FooterRow.Cells.Count; i++)
        {
          string stst=  GridView1.Rows[idx].Cells[i].Text;
            TextBox txtb = ((TextBox)GridView1.FooterRow.Cells[i].FindControl("txt_" + i.ToString()));
            if (txtb != null)
            {
                txtb.Text = stst;
            }
        }
    }


///////// Result




No comments:

Post a Comment

Excel Sort values in ascending order using function TEXTJOIN

 Excel ::  Text ::  1,3,5,2,9,5,11 Result :: 1,2,3,5,5,9,11 Formula ::     TEXTJOIN ( ",",1,SORT(MID(SUBSTITUTE( A1 ,","...