Adding Empty Rows in GridView


This is a simple article describing about how to add empty template in GridView.

Add Data in GridView


1: Initialize a object of DataTable.
2: Initialize a object of DataRow
3: Initialize a object of DataColumns
4: Adding DataColumns to DataTable dt
5: Adding values in DataColumns
6: Binding GridView

Initialize a object of DataTable.

1: Initialize a object of DataTable.

// 1: Initialize a object of DataTable.
DataTable dt = new DataTable();

2: Initialize a object of DataRow

//2: Initialize a object of DataRow
DataRow drow;

3: Initialize a object of DataColumns

//3: Initialize a object of DataColumns
DataColumn col1 = new DataColumn("col1", typeof(string));
DataColumn col2 = new DataColumn("col2", typeof(string));
DataColumn col3 = new DataColumn("col3", typeof(string));
DataColumn col4 = new DataColumn("col4", typeof(string));

4: Adding DataColumns to DataTable dt

//4: Adding DataColumns to DataTable dt
dt.Columns.Add(col1);
dt.Columns.Add(col2);
dt.Columns.Add(col3);
dt.Columns.Add(col4);

5: Adding values in DataColumns

//5: Adding values in DataColumns
for(int i=0;i<10;i++)
{
drow = dt.NewRow();
dt.Rows.Add(drow);
dt.Rows[i][col1] = i.ToString();
dt.Rows[i][col2] = "Column" + i.ToString();
dt.Rows[i][col3] = "Column" + i.ToString();
dt.Rows[i][col4] = "Column" + i.ToString();
}

Binding GridView

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




HTML



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
&nbsp;<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDataBound="GridView1_RowDataBound">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
</div>
</form>
</body>

</html>



now how to add empty template in GridView

take GridView1_RowDataBound event and add below code snippet


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//This is a Test Condition
if (e.Row.RowIndex % 2==0)
{
GridViewRow g = new GridViewRow(-1,-1, DataControlRowType.DataRow, DataControlRowState.Normal);
for (int i = 0; i < e.Row.Cells.Count; i++)
{
//creating table Cells
TableCell tCell = new TableCell();
tCell.Text = " ";
//Add Cells
g.Cells.Add(tCell);
//Set Color for Empty Cell
g.BackColor = System.Drawing.Color.Red;
}
Table tbl = e.Row.Parent as Table;
//add rows
tbl.Rows.Add(g);
}
}


whole cs file


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;

using System.Data;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
string x = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataRow drow;
DataColumn col1 = new DataColumn("col1", typeof(string));
DataColumn col2 = new DataColumn("col2", typeof(string));
DataColumn col3 = new DataColumn("col3", typeof(string));
DataColumn col4 = new DataColumn("col4", typeof(string));

dt.Columns.Add(col1);
dt.Columns.Add(col2);
dt.Columns.Add(col3);
dt.Columns.Add(col4);

for(int i=0;i<10;i++)
{
drow = dt.NewRow();
dt.Rows.Add(drow);
dt.Rows[i][col1] = i.ToString();
dt.Rows[i][col2] = "Column" + i.ToString();
dt.Rows[i][col3] = "Column" + i.ToString();
dt.Rows[i][col4] = "Column" + i.ToString();
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//This is a Test Condition
if (e.Row.RowIndex % 2==0)
{
GridViewRow g = new GridViewRow(-1,-1, DataControlRowType.DataRow, DataControlRowState.Normal);
for (int i = 0; i < e.Row.Cells.Count; i++)
{
//creating table Cells
TableCell tCell = new TableCell();
tCell.Text = " ";
//Add Cells
g.Cells.Add(tCell);
//Set Color for Empty Cell
g.BackColor = System.Drawing.Color.Red;
}
Table tbl = e.Row.Parent as Table;
//add rows
tbl.Rows.Add(g);
}
}
}


Attachments

  • Adding Empty Rows in GridView (38474-221531-addEmptRowInGridView.rar)
  • Related Articles

    What is GridView control and how to bind GridView to data?

    What is a GridView control in asp.net? Want to learn how to connect GridView to a database? This article will teach you more about the databound control GridView, properties and events of GridView and binding a GridView control to data.

    How to add Buttons in Data GridView?

    Want to add buttons to the GridView control in Asp.net? Need to execute a set of statements when the user clicks on the button in the GridView? Learn how to add controls like button to the GridView control and write C# code for user activities performed like Clicking of Buttons in the GridView.

    More articles: Gridview add rows Gridview GridView CSS

    Comments

    Guest Author: ruinme09 Jan 2013

    How is this an empty row if you're assigning values???



  • 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:
    Email: