Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a C# .net application that is using a Gridview. I am having an issue trying to determine how to accomplish this task or if it is possible.

I need my Gridview (on page_load) to display the number of rows based on the number in a variable. For example,if my variable CutOff= 4, I only need 4 rows to display in my gridview. I don't want any data to display, just the ability for the user to enter information. Below is my gridview code. Any tips?

XML
<asp:GridView ID="gvAddEmail" runat="server" Width="100%" CssClass="table table-striped table-bordered table-responsive" Caption="Enter Email"  ShowFooter="true" AutoGenerateColumns="False" CellPadding="0" AllowPaging="True" >
                           <Columns>
                              <asp:TemplateField HeaderText="Email Address(s)" HeaderStyle-CssClass="visible-xs" ItemStyle-CssClass="visible-xs" >
                                  <ItemTemplate>
                                      <%# Eval ("VisitorEmail") %>
                                  </ItemTemplate>
                                  <FooterTemplate>
                                      <asp:TextBox ID="txtVEmail" runat="server" ></asp:TextBox>
                                  </FooterTemplate>
                              </asp:TemplateField>


                           </Columns>
                           <AlternatingRowStyle BackColor="#ccffcc" />
                           <EmptyDataTemplate>
                               <tr>
                                   <th scope="col">Email</th>

                               </tr>
                               <tr>
                                   <td>
                                      <asp:TextBox ID="txtVEmail" runat="server"></asp:TextBox>
                                   </td>

                                 <!--  <td>
                                       <asp:Button ID="btnSubmitVI" runat="server" Text="Submit Visitor Information" OnClick="InsertVisitorInfo" CommandName="EmptyDataTemplate" />
                                   </td> -->
                               </tr>
                           </EmptyDataTemplate>
                       </asp:GridView>
Posted
Updated 27-Aug-15 9:04am
v3

Assuming you have a class you are using to fill the list with:
C#
public class Data
{
    public string Name { get; set; }
    public int Age { get; set; }
}


You can automatically create the prerqeuisite rows by initializing a bound source with empty class instances like this:
C#
    List<data> InitialData = new List<data>()
    {
        new Data(),
        new Data(),
        new Data(),
        new Data()
    };
    GridView1.DataSource = InitialData;
    GridView1.DataBind();
</data></data>


or, using a count parameter in a method:
C#
private void InitGrid(int NumberOfRows)
{
    List<Data> InitialData = new List<Data>();
    for (int i=0;i<NumberOfRows;i++)
    {
        InitialData.Add(new Data());
    }

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

}


This would be an example using page_load:
C#
protected void Page_Load(object sender, EventArgs e)
{
    InitGrid();
}

private void InitGrid()
{
    int NumberOfRows = GetLocation();
    List<Data> InitialData = new List<Data>();
    for (int i = 0; i < NumberOfRows; i++)
    {
        InitialData.Add(new Data());
    }

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

}
private int GetLocation()
{
    return 4;
}

Hope this gives you some ideas,
Juan de Villiers
 
Share this answer
 
v3
Comments
Member 11940827 27-Aug-15 15:02pm    
Thanks for the suggestion. I tried the 2nd example but no rows are displaying. I have a function that I am calling in my Page_Load called GetLocation(); That function is returning the value that I need. For example, the value should = 4. Therefore, I need 4 rows to display with the column I have setup in my Gridview. I will edit my question to add my gridview code.
I have added this code below. I am not getting any errors and have verified that I am getting the correct value (4) returned. However, the gridview is only display the Email row one time and not 4. Am I missing a step? Thank you for you rhelp.

C#
protected void Page_Load(object sender, EventArgs e)
    {
        

        InitGrid();

        if (!IsPostBack)
        {
            PanelSignIn.Visible = false;

        }
    }




public class Data
    {

        public string VisitorEmail { get; set; }

    }


    private void InitGrid()
    {
        int NumberOfRows = GetLocation();
        Response.Write(NumberOfRows); //debugging displays 4 correctly
        List<data> InitialData = new List<data>();
        for (int i = 0; i &lt; NumberOfRows; i++)
        {
            InitialData.Add(new Data());
        }


        gvAddEmail.DataSource = InitialData;
        gvAddEmail.DataBind();

    }
</data></data>
 
Share this answer
 
v3
Comments
Member 11940827 27-Aug-15 18:59pm    
I needed to add AutoGenerateColumns="true". However, I am displaying 4 rows (which is correct). The only issue now is that I need the a template field (textbox) to display in every row. The way I have it now in the .aspx page is only displaying 1 textbox.

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