locked
How to generate row in gridview ,by input number of rows into textbox? RRS feed

  • Question

  • User-367318540 posted

    i want to generate row into gridview by giving range in textbox which is outside of gridview,for example if i input 50 in textbox then 50 row generate in gridview?

    Saturday, February 8, 2020 1:20 PM

Answers

  • User475983607 posted

    hi mgebhard, can you provide example

    Really?  You should at least try...

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="GridViewDemo.aspx.cs" Inherits="WebFormsDemo.GridViewDemo" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
        
        <div>
            <asp:TextBox ID="Rows" runat="server"></asp:TextBox>
            <asp:Button ID="Add" runat="server" Text="Add" />
        </div>
    
        <hr />
    
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:TemplateField HeaderText="ID">
                    <EditItemTemplate>
                        <asp:label ID="ID" runat="server" Text='<%# Bind("ID") %>'></asp:label>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:label ID="ID" runat="server" Text='<%# Bind("ID") %>'></asp:label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name">
                    <EditItemTemplate>                  
                        <asp:TextBox ID="Name" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:TextBox ID="Name" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </asp:Content>
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using WebFormsDemo.Models;
    
    namespace WebFormsDemo
    {
        public partial class GridViewDemo : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (Page.IsPostBack)
                {
                    int rows = int.Parse(Rows.Text);
                    GridView1.DataSource = PopulateData(rows);
                    GridView1.DataBind();
                }
            }
    
            protected List<Employee> PopulateData(int rows)
            {
                List<Employee> employees = new List<Employee>();  
                for(int i = 0; i< rows; i++)
                {
                    employees.Add(new Employee());
                }
                return employees;
            }
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 8, 2020 3:28 PM
  • User288213138 posted

    Hi akhterr,

    i want to generate row into gridview by giving range in textbox which is outside of gridview,for example if i input 50 in textbox then 50 row generate in gridview?

    According to your description, i made demo for you.

    You can add new rows to the DataTable, then bind the GridView.

    <asp:GridView ID="GridView1" runat="server">               
                </asp:GridView>     
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <asp:Button ID="Button1" runat="server" Text="Add" OnClick="Button1_Click" />
    
    
     DataTable dt = new DataTable();
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {               
                    dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Id"), new DataColumn("Name") });
                    dt.Rows.Add(1, "name1");
                    dt.Rows.Add(2, "name2");
                    dt.Rows.Add(3, "name3");
                    ViewState["data"] = dt;
                    GridView1.DataSource = ViewState["data"];
                    GridView1.DataBind();
                }
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {        
                int num = Convert.ToInt32(TextBox1.Text);
                DataTable workTable = (DataTable)ViewState["data"];
                DataRow workRow;
                for (int i = 0; i <num; i++)
                {
                    workRow = workTable.NewRow();
                    workRow[0] = i;
                    workRow[1] = "Name" + i.ToString();
                    workTable.Rows.Add(workRow);
                }
                GridView1.DataSource = ViewState["data"];
                GridView1.DataBind();
            }

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 10, 2020 3:58 AM

All replies

  • User475983607 posted

    i want to generate row into gridview by giving range in textbox which is outside of gridview,for example if i input 50 in textbox then 50 row generate in gridview?

    Very simple.  Create a List<T> or build a DataTable and use the value submitted in the TextBox to create the items/rows.  Then set the GridView source to the new collection or table and DataBind(). 

    Saturday, February 8, 2020 2:15 PM
  • User-367318540 posted

    hi mgebhard, can you provide example

    Saturday, February 8, 2020 2:51 PM
  • User475983607 posted

    hi mgebhard, can you provide example

    Really?  You should at least try...

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="GridViewDemo.aspx.cs" Inherits="WebFormsDemo.GridViewDemo" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
        
        <div>
            <asp:TextBox ID="Rows" runat="server"></asp:TextBox>
            <asp:Button ID="Add" runat="server" Text="Add" />
        </div>
    
        <hr />
    
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:TemplateField HeaderText="ID">
                    <EditItemTemplate>
                        <asp:label ID="ID" runat="server" Text='<%# Bind("ID") %>'></asp:label>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:label ID="ID" runat="server" Text='<%# Bind("ID") %>'></asp:label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name">
                    <EditItemTemplate>                  
                        <asp:TextBox ID="Name" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:TextBox ID="Name" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </asp:Content>
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using WebFormsDemo.Models;
    
    namespace WebFormsDemo
    {
        public partial class GridViewDemo : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (Page.IsPostBack)
                {
                    int rows = int.Parse(Rows.Text);
                    GridView1.DataSource = PopulateData(rows);
                    GridView1.DataBind();
                }
            }
    
            protected List<Employee> PopulateData(int rows)
            {
                List<Employee> employees = new List<Employee>();  
                for(int i = 0; i< rows; i++)
                {
                    employees.Add(new Employee());
                }
                return employees;
            }
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 8, 2020 3:28 PM
  • User-367318540 posted

    hi mgebhard,

    thanks for help,but error is coming that ,the type or  namespace Employee could not found are you missing assembly?

    Sunday, February 9, 2020 3:33 AM
  • User475983607 posted

    thanks for help,but error is coming that ,the type or  namespace Employee could not found are you missing assembly?

    Employee is just a type.

        public class Employee
        {
            public int ID { get; set; }
            public string Name { get; set; }
        }

    Sunday, February 9, 2020 9:27 AM
  • User288213138 posted

    Hi akhterr,

    i want to generate row into gridview by giving range in textbox which is outside of gridview,for example if i input 50 in textbox then 50 row generate in gridview?

    According to your description, i made demo for you.

    You can add new rows to the DataTable, then bind the GridView.

    <asp:GridView ID="GridView1" runat="server">               
                </asp:GridView>     
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <asp:Button ID="Button1" runat="server" Text="Add" OnClick="Button1_Click" />
    
    
     DataTable dt = new DataTable();
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {               
                    dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Id"), new DataColumn("Name") });
                    dt.Rows.Add(1, "name1");
                    dt.Rows.Add(2, "name2");
                    dt.Rows.Add(3, "name3");
                    ViewState["data"] = dt;
                    GridView1.DataSource = ViewState["data"];
                    GridView1.DataBind();
                }
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {        
                int num = Convert.ToInt32(TextBox1.Text);
                DataTable workTable = (DataTable)ViewState["data"];
                DataRow workRow;
                for (int i = 0; i <num; i++)
                {
                    workRow = workTable.NewRow();
                    workRow[0] = i;
                    workRow[1] = "Name" + i.ToString();
                    workTable.Rows.Add(workRow);
                }
                GridView1.DataSource = ViewState["data"];
                GridView1.DataBind();
            }

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 10, 2020 3:58 AM