首页 > Asp.net开发 > 动态绑定Gridview带模板列

动态绑定Gridview带模板列

后台代码:

/// <summary>
/// 绑定gridview查询数据
/// </summary>
public void BindGridViewData()
{
GetWebconfigInfo();
InitDataInfo();

try
{
GridView gridView = gvEquData;
gridView.Columns.Clear();
gridView.AutoGenerateColumns = false;
string[] selectFields = string.IsNullOrEmpty(shows) ? null : shows.Split(',');

BoundField b = new BoundField();
b.HeaderText = "序号";
gridView.Columns.Add(b);

BoundField bf = new BoundField();
bf.HeaderText = "设备连接状态";
bf.DataField = "linkStatu";//固定列
gridView.Columns.Add(bf);

string[] names = QuarrysClass.All.Split(',');
string newName;
if (selectFields == null)
return;
foreach (string name in selectFields)
{
newName = name.Trim('@').ToLower();
string colName = resources[newName] == null ? string.Empty : resources[newName].ToString();
if (QuarrysClass.CheckFlag.ToLower().IndexOf("@" + newName + "@") != -1) //绑定复选框列
{
TemplateField tf = new TemplateField();
if (resources[newName] == null)
{
continue;
}
tf.HeaderTemplate = new GridViewItemTemplate(DataControlRowType.Header, newName, colName, "CheckBox", id);
tf.ItemTemplate = new GridViewItemTemplate(DataControlRowType.DataRow, newName, colName, "CheckBox", id);
gridView.Columns.Add(tf);
}
else
{
if (QuarrysClass.Converts.ToLower().Contains(newName)) //转换显示格式
{
TemplateField tf = new TemplateField();
tf.HeaderTemplate = new GridViewItemTemplate(DataControlRowType.Header, newName, colName, "", id);
tf.ItemTemplate = new GridViewItemTemplate(DataControlRowType.DataRow, newName, colName, "Convert", id);
gridView.Columns.Add(tf);
}
else //普通列
{
bf = new BoundField();
bf.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
bf.HeaderText = resources[newName] == null ? string.Empty : colName;
bf.DataField = newName;
bf.SortExpression = bf.DataField;
gridView.Columns.Add(bf);
}
}
}

QuarrysClass.BusNO = txtBusNO == null ? string.Empty : txtBusNO.Text.Trim();
QuarrysClass.DeviceNO = txtDeviceNO == null ? string.Empty : txtDeviceNO.Text.Trim();
QuarrysClass.LineNO = txtLineNO == null ? string.Empty : txtLineNO.Text.Trim();
QuarrysClass.Resources = resources;
EquSearchBll.equBll.setGridView(gridView, shows);
}
catch (Exception)
{

}

}

 

using System;

using System.Data;
using System.Configuration;
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 Xiongdi.Common.CommonHelper;
using Xiongdi.BizRul;
using GPRS.Admin;

namespace GPRS
{
public class GridViewItemTemplate : ITemplate
{
private string cType;  //控件对象的字符串,以此来判断具体创建哪个控件
private DataControlRowType templateType;  //当前行的模板 (Header,item)
private string colName;  //控件要显示的字符,或是绑定数据源的字段列名
private string colId; //绑定字段
private int isChecked;
private int isCheckedAll;
private int  dataType=Convert.ToInt32(QuarrysClass.DataType); //数据库类型
private string id; //主键

/// <summary>
/// 是否回发
/// </summary>
public bool IsAutoPostBack
{
get;
set;
}

public string Id
{
get
{
return id;
}
set
{
id = value;
}
}

public string IdValue
{
get;
set;
}

public int IsChecked
{
get
{
return isChecked;
}
set
{
isChecked = value;
}
}
/// <summary>
/// 是否全部选中
/// </summary>
public int IsCheckedAll
{
get
{
return isCheckedAll;
}
set
{
isCheckedAll = value;
}
}

/// <summary>
/// 控件模板
/// </summary>
/// <param name="rtype">RowType</param>
/// <param name="colId">字段ID</param>
/// <param name="name">字段名称</param>
/// <param name="cType">模板字段类型</param>
/// <param name="id">主键</param>
public GridViewItemTemplate(DataControlRowType rtype, string colId,string name,string cType,string id)
{
IsAutoPostBack = true;
this.colId = colId;
colName = name;
templateType = rtype;
this.cType = cType;
this.Id = id;
}

private void TexBoxClicking(Object sender, EventArgs e)
{
CheckBox cbx = (CheckBox)sender;
GridViewRow container = (GridViewRow)cbx.NamingContainer;
//关键位置
//使用DataBinder.Eval绑定数据
//ProName,MyTemplate的属性.在创建MyTemplate实例时,为此属性赋值(数据源字段)
cbx.Attributes.Add("onclick", "alert('" + DataBinder.Eval(container.DataItem, colId).ToString() + "');");
}

public void InstantiateIn(System.Web.UI.Control container)
{
if (templateType == DataControlRowType.Header)
{
if (cType == "CheckBox")
{
CheckBox cbxAll = new CheckBox();
//cbxAll.AutoPostBack = IsAutoPostBack;
//cbxAll.Attributes.Add("onclick", "javascript:return confirm('确定要更新本列数据吗?');");
//cbxAll.CheckedChanged += new EventHandler(cbxAll_CheckedChanged);
cbxAll.Checked = Convert.ToBoolean(EquStatusSearch.ht[colId]);
cbxAll.ID = colId;
container.Controls.Add(cbxAll);
}
Literal ltl = new Literal();
ltl.Text = colName;
container.Controls.Add(ltl);
}
else if (templateType == DataControlRowType.DataRow)
{
if (cType == "CheckBox")
{
//HiddenField hdf = new HiddenField();
//hdf.ID = "hidf" + colId;
//hdf.DataBinding += new EventHandler(this.HiddenFieldxDataBinding);
//container.Controls.Add(hdf);

CheckBox cbx = new CheckBox();
//cbx.AutoPostBack = IsAutoPostBack;
//cbx.CheckedChanged += new EventHandler(cbx_CheckedChanged);
cbx.DataBinding += new EventHandler(cbx_DataBinding);

container.Controls.Add(cbx);
}
else if (cType == "Convert")
{
Literal lbl = new Literal();
lbl.ID = "lbl" + colId;
lbl.DataBinding += new EventHandler(lbl_DataBinding);
container.Controls.Add(lbl);
}
}
}

void cbx_CheckedChanged(object sender, EventArgs e)
{
CheckBox cbx = (CheckBox)sender;
IsChecked = cbx.Checked ? 1 : 0;
string strWhere = string.Format(" {0}='{1}'", this.Id, IdValue);
EquSearchBll.equBll.UpdateAllChecked(colId, IsCheckedAll, strWhere);
}

void cbxAll_CheckedChanged(object sender, EventArgs e)
{
CheckBox cbxAll= (CheckBox)sender;
IsCheckedAll = cbxAll.Checked ? 1 : 0;
cbxAll.Checked = !cbxAll.Checked;
EquSearchBll.equBll.UpdateAllChecked(colId, IsCheckedAll,string.Empty);
}

void lbl_DataBinding(object sender, EventArgs e)
{
Label lbl = (Label)sender;
GridViewRow row = (GridViewRow)lbl.NamingContainer;
if (!string.IsNullOrEmpty(colId))
{
lbl.Text =CommonClass.ConvertDateTime(DataBinder.Eval(row.DataItem, colId));
}
}

//private void HiddenFieldxDataBinding(Object sender, EventArgs e)
//{
//    HiddenField hidf = (HiddenField)sender;
//    GridViewRow row = (GridViewRow)hidf.NamingContainer;
//    hidf.Value = DataBinder.Eval(row.DataItem, Id).ToString();
//    IdValue = hidf.Value;
//}

void cbx_DataBinding(object sender, EventArgs e)
{
CheckBox cbx = (CheckBox)sender;
GridViewRow row = (GridViewRow)cbx.NamingContainer;
string str=DataBinder.Eval(row.DataItem, colId)==null?string.Empty:DataBinder.Eval(row.DataItem, colId).ToString();
string id = DataBinder.Eval(row.DataItem, colId) == null ? string.Empty : DataBinder.Eval(row.DataItem, Id).ToString();
cbx.ID = "cbx_" + id + "-" + colId;
if (dataType == (int)EnumDataType.ACCESS)
{
if (str.ToLower() == "true")
{
cbx.Checked = true;
}
else
{
cbx.Checked = false;
}
}
else
{
if (str =="1")
{
cbx.Checked = true;
}
else
{
cbx.Checked = false;
}
}
}
}
}


本文固定链接: http://www.devba.com/index.php/archives/2526.html | 开发吧

报歉!评论已关闭.