使用模板ITemplate根据需求动态生成DataGrid
有时需要把数据库中的记录按横向表格显示,这样就需要根据条件进行处理数据再绑定到DataGrid中
public class ColumnTemplate1 : ITemplate
{
string text = "";
public ColumnTemplate1(string text1)
{
this.text = text1;
}
public void InstantiateIn(Control container)
{
LiteralControl l = new LiteralControl();
l.DataBinding += new EventHandler(this.OnDataBinding);
container.Controls.Add(l);
}
public void OnDataBinding(object sender, EventArgs e)
{
LiteralControl l = (LiteralControl)sender;
DataGridItem container = (DataGridItem)l.NamingContainer;
l.Text = ((DataRowView)container.DataItem)[text].ToString();
}
}
public class ColumnTemplate2 : ITemplate
{
string title = "";
string url = "";
public ColumnTemplate2(string name1, string url1)
{
this.title = name1;
this.url = url1;
}
public void InstantiateIn(Control container)
{
HyperLink l = new HyperLink();
l.DataBinding += new EventHandler(this.OnDataBinding);
container.Controls.Add(l);
}
public void OnDataBinding(object sender, EventArgs e)
{
HyperLink l = (HyperLink)sender;
DataGridItem container = (DataGridItem)l.NamingContainer;
l.Text = ((DataRowView)container.DataItem)[title].ToString();
l.NavigateUrl = ((DataRowView)container.DataItem)[url].ToString();
l.Target = "_blank";
}
}
public class ColumnTemplate : ITemplate
{
zhbg.DataAccess da = new zhbg.DataAccess();
string name = "";
string title = "";
string sort = "";
string scr = "";
string xtdm = "";
string mkbh = "";
string dw = "";
public ColumnTemplate(string name1, string title1, string sort1, string scr1, string xtdm1, string mkbh1, string dw1)
{
name = name1;
title = title1;
sort = sort1;
scr = scr1;
xtdm = xtdm1;
mkbh = mkbh1;
dw = dw1;
}
public void InstantiateIn(Control container)
{
System.Web.UI.WebControls.Image l = new System.Web.UI.WebControls.Image();
l.DataBinding += new EventHandler(this.OnDataBinding);
container.Controls.Add(l);
}
public void OnDataBinding(object sender, EventArgs e)
{
System.Web.UI.WebControls.Image l = (System.Web.UI.WebControls.Image)sender;
DataGridItem container = (DataGridItem)l.NamingContainer;
//l.ToolTip = ((DataRowView)container.DataItem)[name].ToString();
string _sel = "select jsr,hz,hzrq from FileInfoWithAcknow where name='" + ((DataRowView)container.DataItem)[name].ToString() + "' and title='" + ((DataRowView)container.DataItem)[title].ToString() + "' and sort='" + ((DataRowView)container.DataItem)[sort].ToString() + "' and scr='" + scr + "' and xtdm='" + xtdm + "' and mkbh='" + mkbh + "' and jsr='" + dw + "'";
SqlDataReader dr = da.reader(_sel);
if (dr.HasRows)
{
while (dr.Read())
{
if (dr["hz"].ToString() == "1")
{
l.ImageUrl = "img/ok.gif";
l.ToolTip = "回执时间:" + dr["hzrq"].ToString();
}
else
{
l.ImageUrl = "IMAGES/02.gif";
l.ToolTip = "没有回执";
}
}
}
else
{
l.ImageUrl = "img/null.gif";
l.ToolTip = "没有此接收单位";
}
dr.Close();
}
}
{
string text = "";
public ColumnTemplate1(string text1)
{
this.text = text1;
}
public void InstantiateIn(Control container)
{
LiteralControl l = new LiteralControl();
l.DataBinding += new EventHandler(this.OnDataBinding);
container.Controls.Add(l);
}
public void OnDataBinding(object sender, EventArgs e)
{
LiteralControl l = (LiteralControl)sender;
DataGridItem container = (DataGridItem)l.NamingContainer;
l.Text = ((DataRowView)container.DataItem)[text].ToString();
}
}
public class ColumnTemplate2 : ITemplate
{
string title = "";
string url = "";
public ColumnTemplate2(string name1, string url1)
{
this.title = name1;
this.url = url1;
}
public void InstantiateIn(Control container)
{
HyperLink l = new HyperLink();
l.DataBinding += new EventHandler(this.OnDataBinding);
container.Controls.Add(l);
}
public void OnDataBinding(object sender, EventArgs e)
{
HyperLink l = (HyperLink)sender;
DataGridItem container = (DataGridItem)l.NamingContainer;
l.Text = ((DataRowView)container.DataItem)[title].ToString();
l.NavigateUrl = ((DataRowView)container.DataItem)[url].ToString();
l.Target = "_blank";
}
}
public class ColumnTemplate : ITemplate
{
zhbg.DataAccess da = new zhbg.DataAccess();
string name = "";
string title = "";
string sort = "";
string scr = "";
string xtdm = "";
string mkbh = "";
string dw = "";
public ColumnTemplate(string name1, string title1, string sort1, string scr1, string xtdm1, string mkbh1, string dw1)
{
name = name1;
title = title1;
sort = sort1;
scr = scr1;
xtdm = xtdm1;
mkbh = mkbh1;
dw = dw1;
}
public void InstantiateIn(Control container)
{
System.Web.UI.WebControls.Image l = new System.Web.UI.WebControls.Image();
l.DataBinding += new EventHandler(this.OnDataBinding);
container.Controls.Add(l);
}
public void OnDataBinding(object sender, EventArgs e)
{
System.Web.UI.WebControls.Image l = (System.Web.UI.WebControls.Image)sender;
DataGridItem container = (DataGridItem)l.NamingContainer;
//l.ToolTip = ((DataRowView)container.DataItem)[name].ToString();
string _sel = "select jsr,hz,hzrq from FileInfoWithAcknow where name='" + ((DataRowView)container.DataItem)[name].ToString() + "' and title='" + ((DataRowView)container.DataItem)[title].ToString() + "' and sort='" + ((DataRowView)container.DataItem)[sort].ToString() + "' and scr='" + scr + "' and xtdm='" + xtdm + "' and mkbh='" + mkbh + "' and jsr='" + dw + "'";
SqlDataReader dr = da.reader(_sel);
if (dr.HasRows)
{
while (dr.Read())
{
if (dr["hz"].ToString() == "1")
{
l.ImageUrl = "img/ok.gif";
l.ToolTip = "回执时间:" + dr["hzrq"].ToString();
}
else
{
l.ImageUrl = "IMAGES/02.gif";
l.ToolTip = "没有回执";
}
}
}
else
{
l.ImageUrl = "img/null.gif";
l.ToolTip = "没有此接收单位";
}
dr.Close();
}
}
void Bind()
{
DataGrid DataGrid1 = new DataGrid();
DataGrid1.CellPadding = 4;
DataGrid1.CellSpacing = 0;
DataGrid1.HeaderStyle.BackColor = Color.FromArgb(93, 123, 157);
DataGrid1.HeaderStyle.ForeColor = Color.White;
DataGrid1.HeaderStyle.Font.Bold = true;
DataGrid1.HeaderStyle.BorderColor = Color.FromArgb(40, 40, 40);
DataGrid1.AutoGenerateColumns = false;
string sel = "select distinct title,name,sort,url from FileInfoWithAcknow where scr='" + da.name(Session["usname"].ToString()) + "' and xtdm='" + xtdm + "' and mkbh='" + mkbh + "' ";
TemplateColumn tm0 = new TemplateColumn();
tm0.HeaderText = "类别";
tm0.ItemTemplate = new zhbg.ColumnTemplate1("sort");
DataGrid1.Columns.Add(tm0);
TemplateColumn tm1 = new TemplateColumn();
tm1.HeaderText = "名称";
tm1.ItemTemplate = new zhbg.ColumnTemplate2("title", "url");
DataGrid1.Columns.Add(tm1);
for (int i = 0; i < jsr.Items.Count; i++)
{
TemplateColumn tm = new TemplateColumn();
tm.HeaderText = jsr.Items[i].Text;
tm.ItemTemplate = new zhbg.ColumnTemplate("name", "title", "sort", da.name(Session["usname"].ToString()), xtdm, mkbh, jsr.Items[i].Text);
DataGrid1.Columns.Add(tm);
}
if (PlaceHolder1.Controls.Count != 0)
{
PlaceHolder1.Controls.RemoveAt(0);
}
PlaceHolder1.Controls.AddAt(0,DataGrid1);
DataSet ds = new DataSet();
ds = da.dataSet(sel);
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}
{
DataGrid DataGrid1 = new DataGrid();
DataGrid1.CellPadding = 4;
DataGrid1.CellSpacing = 0;
DataGrid1.HeaderStyle.BackColor = Color.FromArgb(93, 123, 157);
DataGrid1.HeaderStyle.ForeColor = Color.White;
DataGrid1.HeaderStyle.Font.Bold = true;
DataGrid1.HeaderStyle.BorderColor = Color.FromArgb(40, 40, 40);
DataGrid1.AutoGenerateColumns = false;
string sel = "select distinct title,name,sort,url from FileInfoWithAcknow where scr='" + da.name(Session["usname"].ToString()) + "' and xtdm='" + xtdm + "' and mkbh='" + mkbh + "' ";
TemplateColumn tm0 = new TemplateColumn();
tm0.HeaderText = "类别";
tm0.ItemTemplate = new zhbg.ColumnTemplate1("sort");
DataGrid1.Columns.Add(tm0);
TemplateColumn tm1 = new TemplateColumn();
tm1.HeaderText = "名称";
tm1.ItemTemplate = new zhbg.ColumnTemplate2("title", "url");
DataGrid1.Columns.Add(tm1);
for (int i = 0; i < jsr.Items.Count; i++)
{
TemplateColumn tm = new TemplateColumn();
tm.HeaderText = jsr.Items[i].Text;
tm.ItemTemplate = new zhbg.ColumnTemplate("name", "title", "sort", da.name(Session["usname"].ToString()), xtdm, mkbh, jsr.Items[i].Text);
DataGrid1.Columns.Add(tm);
}
if (PlaceHolder1.Controls.Count != 0)
{
PlaceHolder1.Controls.RemoveAt(0);
}
PlaceHolder1.Controls.AddAt(0,DataGrid1);
DataSet ds = new DataSet();
ds = da.dataSet(sel);
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1687944