How to create a Blank row in gridview
In some situations we need to add a blank row between GridView rows. The following program shows how to add a blank row between gridview rows.
Database We are using SQL DATABASE:-
Default.aspx code:-
C# Source Code:-
Database We are using SQL DATABASE:-
Create table tblRecord ( Id int primary key, Name varchar(200),Branch varchar(50) ) --Insert some record --USER STORE PROCETURE create proc USP_SelecttblRecord As Begin Select * from tblRecord End
Default.aspx code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>
C# Source Code:-
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; public partial class Default7 : System.Web.UI.Page { int id = 0; int rowIndex = 1; SqlConnection con = new SqlConnection("Data source=PS-PC\\SANTOSH; Database=dbSantoshTest; Integrated Security=true"); //connection is created..... protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { loadRecord(); } } protected void loadRecord() //bind gridview..... { con.Open(); SqlCommand cmd = new SqlCommand("USP_SelecttblRecord", con); cmd.CommandType = CommandType.StoredProcedure; SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); int count = ds.Tables[0].Rows.Count; con.Close(); if (ds.Tables[0].Rows.Count > 0) { gvDetail.DataSource = ds; gvDetail.DataBind(); } else { ds.Tables[0].Rows.Add(ds.Tables[0].NewRow()); gvDetail.DataSource = ds; gvDetail.DataBind(); int columncount = gvDetail.Rows[0].Cells.Count; } } protected void gvDetail_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { id = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Id").ToString()); } } protected void gvDetail_RowCreated(object sender, GridViewRowEventArgs e) { bool newRow = false; if ((id > 0) && (DataBinder.Eval(e.Row.DataItem, "id") != null)) { if (id != Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "id").ToString())) newRow = true; } if ((id > 0) && (DataBinder.Eval(e.Row.DataItem, "id") == null)) { newRow = true; rowIndex = 0; } if (newRow) { AddNewRow(sender, e); } } public void AddNewRow(object sender, GridViewRowEventArgs e) { GridView GridView1 = (GridView)sender; GridViewRow NewTotalRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert); NewTotalRow.Font.Bold = true; NewTotalRow.BackColor = System.Drawing.Color.Aqua; TableCell HeaderCell = new TableCell(); HeaderCell.Height = 10; HeaderCell.HorizontalAlign = HorizontalAlign.Center; HeaderCell.ColumnSpan = 4; NewTotalRow.Cells.Add(HeaderCell); GridView1.Controls[0].Controls.AddAt(e.Row.RowIndex + rowIndex, NewTotalRow); rowIndex++; } }