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

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


Comments

Author: Manindra Kumar Upadhyay29 Apr 2010 Member Level: Gold   Points : 1

try this link:
http://manindra.net/post/2010/04/30/Show-Empty-Grid-View-Code-in-AspNet.aspx

Author: Kunal30 Apr 2010 Member 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?