Mohemmad K
Mohemmad K

Reputation: 839

How to define number of rows of GridView at runtime in code behind using C#

I need to develop such a program in which the GridView's rows should be decided at run time.

i.e. I have a table in database called dealer capacity.

Dealer_ID       Capacity
D0001                5

Now when the Dealer D00001 is selected from combo box the number of rows in grid view should be 5. I want to use the template field also.

My code for GridView is:

<asp:GridView ID="grdlicence" runat="server" DataKeyNames="Version_id" GridLines="None" BorderStyle="Solid" AutoGenerateColumns="false" AllowSorting="true"     
        CssClass="mGrid table" 
        PagerStyle-CssClass="pgr" 
        AlternatingRowStyle-CssClass="alt" >
             <Columns>
                 <asp:BoundField DataField="Version_name" ItemStyle-CssClass="uppercase" ItemStyle-Width="150px"   HeaderText="Version" HeaderStyle-HorizontalAlign="Left" />

                <asp:BoundField DataField="Version_id" Visible="false" HeaderText="Version" HeaderStyle-HorizontalAlign="Left" />

              <asp:TemplateField HeaderText="Price">
                <ItemTemplate>
                       <center><asp:TextBox ID="txtprice" CssClass="alignments TextStyle" MaxLength="5"  runat="server" ></asp:TextBox></center>
                </ItemTemplate>
              </asp:TemplateField>

              <asp:TemplateField HeaderText="Licence Id">
                <ItemTemplate>
                       <center><asp:TextBox ID="txtlicenceid" CssClass="alignments uppercase"  runat="server" ></asp:TextBox></center>
                </ItemTemplate>
              </asp:TemplateField>


              <asp:TemplateField HeaderText="Purchase Date">
                <ItemTemplate>
                       <center><asp:TextBox ID="txtpurchasedate"  onfocus="showCalendarControl(this);"  CssClass="alignments TextStyle"   runat="server"></asp:TextBox></center>
                </ItemTemplate>
              </asp:TemplateField>


              <asp:TemplateField HeaderText="Expiry Date">
                <ItemTemplate>
                       <center><asp:TextBox ID="txtexpirydate"  onfocus="showCalendarControl(this);"  CssClass="alignments TextStyle"   runat="server"></asp:TextBox></center>
                </ItemTemplate>
              </asp:TemplateField>

                 <asp:TemplateField HeaderText="Upload File">
                <ItemTemplate>
                     <center><asp:FileUpload ID="fileUpload" runat="server"  /></center> 
                </ItemTemplate>
              </asp:TemplateField>


           </Columns>

           </asp:GridView>

Upvotes: 0

Views: 1669

Answers (2)

Chamika Sandamal
Chamika Sandamal

Reputation: 24312

You can use the linq Take() and pass the number as parameter.


Updated according to the comment, use following code.

grdlicence.DataSourse= ds.Take(5);
grdlicence.DataBind();

Upvotes: 1

Habib
Habib

Reputation: 223277

You need to define PageSize for your GridView and remember to set AllowPaging to true for the GridView

GridView.PageSize Property

Gets or sets the number of records to display on a page in a GridView control.

The default is 10.

You may see this article: GridView Paging Sample in ASP.NET

Upvotes: 1

Related Questions