Search This Blog

Thursday 11 February 2016

How to add Textbox Dynamically in GridView Header & Row by input values in textbox in ASP.Net

Step 1:- Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Demo.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <script type="text/javascript">
        function Validate() {
            var txtNoofColumn = document.getElementById("txtNoofColumn");
            var txtNoofRows = document.getElementById("txtNoofRows");

            if (txtNoofColumn.value === "") {
                alert("Please enter no of columns.");
                document.getElementById("txtNoofColumn").focus();
                return false;
            }

            if (txtNoofRows.value === "") {
                alert("Please enter no of rows.");
                document.getElementById("txtNoofRows").focus();
                return false;
            }
            return true;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Panel ID="Panel1" runat="server">
                <fieldset>
                    <legend>Generate Table:</legend>
                    <table>
                        <tr>
                            <td class="label">No of Column*</td>
                            <td class="labelDescription">
                                <asp:TextBox ID="txtNoofColumn" Style="text-alignright" runat="server" ClientIDMode="Static">
                                </asp:TextBox>
                            </td>
                            <td class="label">No of Rows*</td>
                            <td class="labelDescription">
                                <asp:TextBox ID="txtNoofRows" Style="text-alignright" runat="server" ClientIDMode="Static">
                                </asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td class="labelDescription" colspan="4">
                                <asp:Button ID="btnGenerate" runat="server" Text="Generate" Font-Bold="true" OnClick="btnGenerate_Click" />
                            </td>
                        </tr>
                        <tr>
                            <td colspan="4">
                                <asp:GridView ID="GridView1" runat="server" ClientIDMode="Static">
                                </asp:GridView>
                            </td>
                        </tr>
                    </table>
                </fieldset>

            </asp:Panel>
        </div>
    </form>
</body>
</html>

Step 2:- Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Demo
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                btnGenerate.Attributes.Add("onclick""return Validate();");
            }
        }

        protected void btnGenerate_Click(object sender, EventArgs e)
        {
            Generate();
        }

        protected void Generate()
        {
            DataTable dt = new DataTable();

            int col = Convert.ToInt32(txtNoofColumn.Text);
            int row = Convert.ToInt32(txtNoofRows.Text);

            for (int i = 0; i < col; i++)
            {
                dt.Columns.Add(new DataColumn(""typeof(string)));
            }

            for (int i = 0; i < row; i++)
            {
                dt.Rows.Add();
            }

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

            int cellCount = this.GridView1.Rows[0].Cells.Count;
            int rowsCount = this.GridView1.Rows.Count;

            TextBox textBox;

            for (int i = 0; i < cellCount; i++)
            {
                textBox = new TextBox();
                textBox.ID = "TextBox_" + i.ToString();
                textBox.Attributes.Add("runat""server");
                textBox.Width = 60;
                textBox.BackColor = System.Drawing.Color.Wheat;
                //textBox.Style.Add("text-align", "right");
                GridView1.HeaderRow.Cells[i].Controls.Add(textBox);
            }

            foreach (GridViewRow gridViewRow in GridView1.Rows)
            {
                for (int i = 0; i < cellCount; i++)
                {
                    textBox = new TextBox();
                    textBox.ID = "TextBox_" + i.ToString();
                    textBox.Attributes.Add("runat""server");
                    textBox.Width = 60;
                    //textBox.Style.Add("text-align", "right");
                    gridViewRow.Cells[i].Controls.Add(textBox);
                }
            }
        }
    }
}


Step 3:- Output


No comments:

Post a Comment