GridView TemplateField not rendering null rows

R

Reg_H

I've come across a strange problem where I have a GridView TemplateField
column that doesn't render out a row where the value is NULL for the cell in
question but a BoundColumn will. This problem is masked if you have other
columns in the gridview which have values in, you'll get all the rows if you
always have at least one column that has something other than null, but when
you just have column(s) that all are empty with a TemplateField my GridView
doesn't render those rows.

This problem is best explained with some sample code which reproduces the
problem. I appreciate it maybe something simple I've overlooked, but I just
can't figure out why the null item rows won't render with a TemplateField.
Any ideas why GridView1 doesn't have the same row count as GridView2?

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test4.aspx.cs"
Inherits="test4" %>

<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
Caption="TemplateField GridView">
</asp:GridView>

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false"
Caption="BoundField GridView">
<Columns>
<asp:BoundField DataField="Total" HeaderText="Total" />
</Columns>
</asp:GridView>

</div>
</form>
</body>
</html>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class test4 : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);

DataTable dt = new DataTable();
dt.Columns.Add("Site");
dt.Columns.Add("Total");

DataRow row;
Random RandomClass = new Random();

for (int n = 0; n < 10; n++)
{
row = dt.NewRow();
row["Site"] = "Site " + n.ToString();

row["Total"] = RandomClass.Next();
dt.Rows.Add(row);
}

//now, add some intentional null rows

for (int n = 0; n < 10; n++)
{
row = dt.NewRow();
row["Site"] = "XSite " + n.ToString();
row["Total"] = DBNull.Value;
dt.Rows.Add(row);
}

GridView1.DataSource = dt;

TemplateField tf = new TemplateField();
ColTemplate ct = new ColTemplate(ListItemType.Item, "Total");
tf.ItemTemplate = ct;
tf.HeaderText = "Site";
GridView1.Columns.Add(tf);
GridView1.DataBind();

GridView2.DataSource = dt;
GridView2.DataBind();
}

protected void Page_Load(object sender, EventArgs e)
{

}
}

#region Template

public class ColTemplate : ITemplate
{
ListItemType templateType;
string columnName;

public ColTemplate(ListItemType templateType, string columnName)
{
this.templateType = templateType;
this.columnName = columnName;
}

public void InstantiateIn(System.Web.UI.Control container)
{
switch (templateType)
{
case ListItemType.Item:
LinkButton lb = new LinkButton();
lb.DataBinding += new EventHandler(lb_DataBinding);
container.Controls.Add(lb);
break;
}
}


void lb_DataBinding(object sender, EventArgs e)
{
LinkButton lb = (LinkButton)sender;
GridViewRow container = (GridViewRow)lb.NamingContainer;
if (container.DataItem is DataRowView)
{
DataRowView dvr = container.DataItem as DataRowView;
if (dvr.Row.Table.Columns.Contains(columnName))
{
lb.Text = DataBinder.Eval(container.DataItem, columnName,
"{0:N0}").ToString();
}
}
}
};

#endregion
 
R

Reg_H

Answered at:
http://forums.asp.net/p/1346640/2745125.aspx#2745125

Reg_H said:
I've come across a strange problem where I have a GridView TemplateField
column that doesn't render out a row where the value is NULL for the cell in
question but a BoundColumn will. This problem is masked if you have other
columns in the gridview which have values in, you'll get all the rows if you
always have at least one column that has something other than null, but when
you just have column(s) that all are empty with a TemplateField my GridView
doesn't render those rows.

This problem is best explained with some sample code which reproduces the
problem. I appreciate it maybe something simple I've overlooked, but I just
can't figure out why the null item rows won't render with a TemplateField.
Any ideas why GridView1 doesn't have the same row count as GridView2?

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test4.aspx.cs"
Inherits="test4" %>

<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
Caption="TemplateField GridView">
</asp:GridView>

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false"
Caption="BoundField GridView">
<Columns>
<asp:BoundField DataField="Total" HeaderText="Total" />
</Columns>
</asp:GridView>

</div>
</form>
</body>
</html>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class test4 : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);

DataTable dt = new DataTable();
dt.Columns.Add("Site");
dt.Columns.Add("Total");

DataRow row;
Random RandomClass = new Random();

for (int n = 0; n < 10; n++)
{
row = dt.NewRow();
row["Site"] = "Site " + n.ToString();

row["Total"] = RandomClass.Next();
dt.Rows.Add(row);
}

//now, add some intentional null rows

for (int n = 0; n < 10; n++)
{
row = dt.NewRow();
row["Site"] = "XSite " + n.ToString();
row["Total"] = DBNull.Value;
dt.Rows.Add(row);
}

GridView1.DataSource = dt;

TemplateField tf = new TemplateField();
ColTemplate ct = new ColTemplate(ListItemType.Item, "Total");
tf.ItemTemplate = ct;
tf.HeaderText = "Site";
GridView1.Columns.Add(tf);
GridView1.DataBind();

GridView2.DataSource = dt;
GridView2.DataBind();
}

protected void Page_Load(object sender, EventArgs e)
{

}
}

#region Template

public class ColTemplate : ITemplate
{
ListItemType templateType;
string columnName;

public ColTemplate(ListItemType templateType, string columnName)
{
this.templateType = templateType;
this.columnName = columnName;
}

public void InstantiateIn(System.Web.UI.Control container)
{
switch (templateType)
{
case ListItemType.Item:
LinkButton lb = new LinkButton();
lb.DataBinding += new EventHandler(lb_DataBinding);
container.Controls.Add(lb);
break;
}
}


void lb_DataBinding(object sender, EventArgs e)
{
LinkButton lb = (LinkButton)sender;
GridViewRow container = (GridViewRow)lb.NamingContainer;
if (container.DataItem is DataRowView)
{
DataRowView dvr = container.DataItem as DataRowView;
if (dvr.Row.Table.Columns.Contains(columnName))
{
lb.Text = DataBinder.Eval(container.DataItem, columnName,
"{0:N0}").ToString();
}
}
}
};

#endregion
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top