Resources » .NET programming » ASP.NET/Web Applications

How to add a blank row after every n rows of GridView.


Posted Date: 23-Mar-2009  Last Updated:   Category: ASP.NET/Web Applications    
Author: Member Level: Gold    Points: 10



Introduction:

In this article i will explain how to add a blank row after every n rows of a gridview:


Implementation:

To achieve this functionality we will have a gridview in design view .

In the code behind we have this code.Here we have used 5th row to display blank row.



& lt asp:GridView ID="gdview" runat="server" AutoGenerateColumns="False"& gt
& lt Columns& gt
& lt asp:BoundField HeaderText="Category Name" DataField="catname" & gt
& lt ItemStyle Height="20px" Width="150px" /& gt
& lt /asp:BoundField& gt


& lt /Columns& gt


& lt /asp:GridView& gt





protected void Page_Load(object sender, EventArgs e)
{

if (!Page.IsPostBack)
{
bindgrid();

}

}

public void bindgrid()
{
SqlConnection conn = new SqlConnection("Data Source='localhost';Initial Catalog='Northwind';Integrated Security=SSPI;Persist Security Info=False ");


SqlDataAdapter da = new SqlDataAdapter("", conn);
da.SelectCommand = new SqlCommand("select CategoryName,CategoryID from Categories", conn);
DataSet ds = new DataSet();
da.Fill(ds, "data");
DataTable dtable = new DataTable();
dtable.Columns.Add("catname");
int k = 5;//here istead of 5 you can put 'n'th row.
for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
DataRow r = dtable.NewRow();
if ((i+1) == k)
{
r[0] = "";
k = k + 5;
}
else
{
r[0] = ds.Tables[0].Rows[i].ItemArray[0].ToString();
}
dtable.Rows.Add(r);

}

gdview.DataSource = dtable;
gdview.DataBind();


}



Explanation:

Here we have assigned categories table in the northwind database to
the datasource. We have used dtable to assign an empty row after n records ie
5 in this case.we have looped the rows untill we reach our specific row and
assigned a blank row in that area.Finally we have binded this datatable to the gridview


Did you like this resource? Share it with your friends and show your love!

Responses to "How to add a blank row after every n rows of GridView."
Author: Manindra Kumar Upadhyay    29 Apr 2010Member Level: Gold   Points : 1
try this link:
http://manindra.net/post/2010/04/30/Show-Empty-Grid-View-Code-in-AspNet.aspx



Author: Kunal    30 Apr 2010Member Level: Bronze   Points : 1
what about the performance of this code?
if i have 100 rows to bound then..
is it go slow or not?




Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    tuan
    More...
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India