Click here to Skip to main content
15,917,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a textbox , button and gridview. User will enter the number in textbox and press ok buton. That much number of rows should get added to gridview. I gogled a lot but not able to find a solution.
Any help..
Thanks in advance.
Posted

Hi,
Below is the solution for your question


DataTable dt = new DataTable();
dt.Columns.Add("Id", System.Type.GetType("System.Int32"));
dt.Columns.Add("Name");

dt.Rows.Add(new object[] { 1, "aaa" });
dt.Rows.Add(new object[] { 2, "bbb" });
dt.Rows.Add(new object[] { 3, "ccc" });
dt.Rows.Add(new object[] { 4, "ddd" });
dt.Rows.Add(new object[] { 5, "eee" });
dt.Rows.Add(new object[] { 6, "fff" });
dt.Rows.Add(new object[] { 7, "ggg" });

int pageNum = 1;
int pageSize = Convert.ToInt32(TextBox1.Text.Trim());
DataTable dtPage = dt.Rows.Cast<system.data.datarow>().Skip((pageNum - 1) * pageSize).Take(pageSize).CopyToDataTable();

Gridview1.DataSource = dtPage;
Gridview1.DataBind();
 
Share this answer
 
Aspx
ASP.NET
<div>
            <table style="width:40%;">
                <tr>
                    <td>
                        <asp:TextBox runat="server" ID="txtRowCount"></asp:TextBox>
                    </td>
                    <td >
                        <asp:Button ID="btnGenerate" runat="server" Text="Generate Rows" OnClick="btnGenerate_Click" />
                    </td>
                </tr>
                
                <tr>
                    <td colspan="2">
                        <br />
                        <br />

                        <asp:GridView runat="server" ID="gvEmp" AutoGenerateColumns="false">
                            <Columns>
                                <asp:TemplateField HeaderText="Title">
                                    <ItemTemplate>
                                        <asp:TextBox runat="server" ID="txt" Text='<%# Bind("Title") %>'></asp:TextBox>
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>
                    </td>
                </tr>
            </table>

        </div>




Code Behind


C#
protected void Page_Load(object sender, EventArgs e)
       {
           if (!IsPostBack)
               GenerateRows(2);
       }

       protected void btnGenerate_Click(object sender, EventArgs e)
       {
           GenerateRows(Convert.ToInt32(txtRowCount.Text));
       }
     void GenerateRows( int intRowCout)
       {

           DataRow workRow;
           DataTable workTable = new DataTable();
           workTable.Columns.Add("Title", typeof(System.String));
           for (int i = 1; i <= intRowCout; i++)
           {
               workRow = workTable.NewRow();
               workRow["Title"] = string.Empty;
               workTable.Rows.Add(workRow);
           }

           gvEmp.DataSource = workTable;
           gvEmp.DataBind();
       }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900