动态生成模板——DataGrid

模板类:

public class DataGridTemplate : ITemplate
 {
  //创建模板列

  public void InstantiateIn(Control container )      
  {
   LiteralControl lc = new LiteralControl();
   lc.DataBinding += new EventHandler(this.OnDataBinding);
   //数据绑定
   container.Controls.Add(lc);
   //为模板列加入LiteralControl
  }
 
  public void OnDataBinding(object sender, EventArgs e)
  {
   LiteralControl lc = (LiteralControl) sender;//LiteralControl发送绑定请求
   DataGridItem container = (DataGridItem) lc.NamingContainer;
    
   lc.Text = ((DataRowView)container.DataItem)[ColumnName].ToString();//绑定FormNO字段
  }
  private string _ColumnName;
  public  String  ColumnName
  {
   get
   {
    return _ColumnName;
   }
   set
   {
    _ColumnName = value;
   }
  }
 }

调用模板类:
public void AddTemplate(String Name,String NameSubject)
  {
   TemplateColumn TC=new TemplateColumn();
   DataGridTemplate myTemplate=new DataGridTemplate();
   myTemplate.ColumnName=Name;
   TC.ItemTemplate=myTemplate;
   TC.HeaderText=NameSubject; 
   TC.HeaderStyle.BackColor= ColorTranslator.FromHtml("#336699"); 
   //TC.ItemStyle.BackColor = ColorTranslator.FromHtml("#FFF778"); 
   TC.ItemStyle.CssClass="cn"; 
   TC.HeaderStyle.CssClass="cn";
   DataGrid1.Columns.Add(TC);
  }

添加打印Button:
public void AddButtonColum(string commandname,string text)
  {
   ButtonColumn BC=new ButtonColumn();
   BC.ButtonType=ButtonColumnType.PushButton;
   BC.CommandName=commandname;
   BC.Text=text;
   DataGrid1.Columns.Add(BC);
  }

添加LinkButton:
public void AddLinkButtonColum(string commandname,string text)
  {
   ButtonColumn BC=new ButtonColumn(); 
   BC.ButtonType=ButtonColumnType.LinkButton;
   BC.CommandName=commandname;
   BC.Text=text;
   DataGrid1.Columns.Add(BC); 

  }


添加HyperLink:
public void AddHyperLinkColum(string commandname,string text)
  {
   HyperLinkColumn HC=new HyperLinkColumn();
   HC.NavigateUrl="http://www.home";
   HC.Text=text;
   DataGrid1.Columns.Add(HC); 

  }

动态添加模板:

public DataSet getDataSet()
  {
   String _value;
   String _sql;
   string _wheresql="";
   string _fromtime,_totime;

   _value=ViewName.Text.Trim();
   DataSet ds=new DataSet();
   string strConnection=BRC.Library.Helper.ConfigHelper.ConfigCore.GetSysConfigValue("ConnectionString");
   SqlConnection cn=new SqlConnection(strConnection);

   _fromtime=FromTime.Text.Trim();
   _totime=ToTime.Text.Trim();
   if(_fromtime!="")
   {
    _wheresql =" and p.ApplyDate>='"+_fromtime+"'";       
   }
   if(_totime!="")
   {
    if(_wheresql=="")
    {
     _wheresql =" and p.ApplyDate<='"+_totime+"'";
    }
    else
    {
     _wheresql +=" and p.ApplyDate<='"+_totime+"'";
    }
   
   
   }
 
   AddTemplate("OrderID","序号");
   //AddTemplate("FormNO","表单号");
   switch(_value)
   {
    case "基本数据视图":
     AddTemplate("物料编号","物料编号");
     AddTemplate("中文描述","中文描述");
     AddTemplate("英文描述","英文描述");
     AddTemplate("物料类型","物料类型");
     AddTemplate("工厂编号","工厂编号");
     AddTemplate("基本单位","基本单位");
     AddTemplate("物料组","物料组");
     AddTemplate("产品组","产品组");
     AddTemplate("零件毛重","零件毛重");
     AddTemplate("零件净重","零件净重");
     AddButtonColum("print","打印");
     _sql="select a.MaterielCode as 物料编号 ,a.DescribeCN as 中文描述,a.DescribeEN as 英文描述,a.MaterielType as 物料类型,a.PlantCode as 工厂编号,";
     _sql+=" a.BaseUnit as 基本单位,a.MaterielGroup as 物料组,";
     _sql+=" a.Division as 产品组,a.GrossWeight as 零件毛重,a.NetWeight as 零件净重 ";
     _sql+=" from BW_basic_info a,BW_Form_info b,BW_proposer_info p where b.InFlow='0' and a.FormNO=b.FormNO and a.FormNO=p.FormNO "+_wheresql;
     break;
    case "采购视图":
     AddTemplate("物料编号","物料编号");
     AddTemplate("中文描述","中文描述");
     AddTemplate("英文描述","英文描述");
     AddTemplate("物料类型","物料类型");
     AddTemplate("工厂编号","工厂编号");
     AddTemplate("采购组","采购组");
     AddTemplate("配额管理","配额管理");
     _sql="select a.MaterielCode as 物料编号,a.DescribeCN as 中文描述,a.DescribeEN as 英文描述,";
     _sql+=" a.MaterielType as 物料类型,a.PlantCode as 工厂编号,c.PurGroup as 采购组,c.QuotaUsage as 配额管理 ";
     _sql+=" from BW_basic_info a,BW_Form_info b,BW_pur_view c,BW_proposer_info p where b.InFlow='0' and a.FormNO=b.FormNO and c.FormNO=b.FormNO and b.FormNO=p.FormNO "+_wheresql;
     break;
    case "质量视图":
     AddTemplate("物料编号","物料编号");
     AddTemplate("中文描述","中文描述");
     AddTemplate("英文描述","英文描述");
     AddTemplate("物料类型","物料类型");
     AddTemplate("工厂编号","工厂编号");
     AddTemplate("检验类型","检验类型");
     AddTemplate("跳检判定","跳检判定");
     AddButtonColum("print","打印");
     _sql="select a.MaterielCode as 物料编号,a.DescribeCN as 中文描述,a.DescribeEN as 英文描述,a.MaterielType as 物料类型 ,";
     _sql+=" a.PlantCode as 工厂编号,c.Inspection as 检验类型,c.AutomaticUD as 跳检判定 ";
     _sql+=" from BW_basic_info a,BW_Form_info b,BW_quality_view c,BW_proposer_info p where b.InFlow='0' and a.FormNO=b.FormNO and c.FormNO=b.FormNO and b.FormNO=p.FormNO "+_wheresql;
     break;
    case "仓储视图":
     AddTemplate("物料编号","物料编号");
     AddTemplate("中文描述","中文描述");
     AddTemplate("英文描述","英文描述");
     AddTemplate("物料类型","物料类型");
     AddTemplate("工厂编号","工厂编号");
     AddTemplate("WM库号","WM库号");
     AddTemplate("存储类型","存储类型");
     AddTemplate("库存地点","库存地点");
     AddTemplate("库存仓位","库存仓位");
     AddTemplate("托盘装载数量","托盘装载数量");
     AddTemplate("托盘单位","托盘单位");
     AddTemplate("存储单位类型","存储单位类型");
     AddButtonColum("print","打印");
     _sql="select a.MaterielCode as 物料编号,a.DescribeCN as 中文描述,a.DescribeEN as 英文描述,";
     _sql+=" a.MaterielType as 物料类型 ,a.PlantCode as 工厂编号,c.WarehouseNum as WM库号,c.StorageType as 存储类型,";
     _sql+=" c.StorageLocation as 库存地点, c.StorageBin as 库存仓位,c.LoadingEquipQty1 as 托盘装载数量,c.UnitOfMeasure1 as 托盘单位,c.StorageUnitType1 as 存储单位类型";
     _sql+=" from BW_basic_info a,BW_Form_info b,BW_storage_view c,BW_proposer_info p where b.InFlow='0' and a.FormNO=b.FormNO and c.FormNO=b.FormNO and b.FormNO=p.FormNO "+_wheresql;     
     break;
    case "MRP视图":
     AddTemplate("物料编号","物料编号");
     AddTemplate("中文描述","中文描述");
     AddTemplate("英文描述","英文描述");
     AddTemplate("物料类型","物料类型");
     AddTemplate("工厂编号","工厂编号");
     AddTemplate("MRP类型","MRP类型");
     AddTemplate("MRP控制者","MRP控制者");
     AddTemplate("批量数据","批量数据");
     AddTemplate("获得类型","获得类型");
     AddTemplate("批次确认","批次确认");
     AddTemplate("材料反冲","材料反冲");
     AddTemplate("计划边际码","计划边际码");
     AddTemplate("计划策略","计划策略");
     AddButtonColum("print","打印");
     _sql="select a.MaterielCode as 物料编号,a.DescribeCN as 中文描述,a.DescribeEN as 英文描述,";
     _sql+=" a.MaterielType as 物料类型 ,a.PlantCode as 工厂编号,c.MrpType as MRP类型,c.MRPController as MRP控制者,";
     _sql+=" c.LotSize as 批量数据,c.ProcurementType as 获得类型,c.BatchCheck as 批次确认,c.Backflush as 材料反冲,";
     _sql+=" c.SchedmarginKey as 计划边际码,c.PlanStrategyGroup as 计划策略 from BW_basic_info a,BW_Form_info b,BW_MRP_view c,BW_proposer_info p where b.InFlow='0' and a.FormNO=b.FormNO and c.FormNO=b.FormNO and b.FormNO=p.FormNO "+_wheresql;
     break;
    case "会计视图":
     AddTemplate("物料编号","物料编号");
     AddTemplate("中文描述","中文描述");
     AddTemplate("英文描述","英文描述");
     AddTemplate("物料类型","物料类型");
     AddTemplate("工厂编号","工厂编号");
     AddTemplate("估价类别","估价类别");
     AddTemplate("价格控制","价格控制");
     AddTemplate("标准价格","标准价格");
     AddTemplate("价格单位","价格单位");  
     AddTemplate("外购价格类型","外购价格类型");
     AddTemplate("外购估价类别","外购估价类别");
     AddTemplate("外购价格控制","外购价格控制");
     AddTemplate("外购标准价格","外购标准价格");
     AddTemplate("外购价格单位","外购价格单位");
     AddTemplate("自制价格类型","自制价格类型");
     AddTemplate("自制估价类别","自制估价类别");
     AddTemplate("自制价格控制","自制价格控制");
     AddTemplate("自制标准价格","自制标准价格");
     AddTemplate("自制价格单位","自制价格单位");
     //AddButtonColum("print","打印");
     _sql="select a.MaterielCode as 物料编号,a.DescribeCN as 中文描述,a.DescribeEN as 英文描述,";
     _sql+=" a.MaterielType as 物料类型 ,a.PlantCode as 工厂编号,c.Valuation_class as 估价类别,c.PriceControl as 价格控制,";
     _sql+=" c.StandardPrice as 标准价格,c.PriceUnit as 价格单位,c.OutType as 外购价格类型,c.OutClass as 外购估价类别,";
     _sql+=" c.OutControl as 外购价格控制,c.OutPrice as 外购标准价格,c.OutUnit as 外购价格单位,c.SelfType as 自制价格类型,";
     _sql+=" c.SelfClass as 自制估价类别,c.SelfControl as 自制价格控制,c.SelfPrice as 自制标准价格,c.SelfPriceUnit as 自制价格单位 ";
     _sql+=" from BW_basic_info a,BW_Form_info b,BW_bursar_view c ,BW_proposer_info p where b.InFlow='0' and a.FormNO=b.FormNO and c.FormNO=b.FormNO and b.FormNO=p.FormNO "+_wheresql;
     break;
    case "销售视图":
     AddTemplate("物料编号","物料编号");
     AddTemplate("中文描述","中文描述");
     AddTemplate("英文描述","英文描述");
     AddTemplate("物料类型","物料类型");
     AddTemplate("工厂编号","工厂编号");
     AddTemplate("销售组织","销售组织");
     AddTemplate("分销渠道","分销渠道");
     AddTemplate("装运仓库","装运仓库");
     AddTemplate("销售税码","销售税码");  
     AddTemplate("装运组","装运组");
     //AddButtonColum("print","打印");
     _sql="select a.MaterielCode as 物料编号,a.DescribeCN as 中文描述,a.DescribeEN as 英文描述,";
     _sql+=" a.MaterielType as 物料类型 ,a.PlantCode as 工厂编号,c.SalesOrg as 销售组织,c.DistrChannel as 分销渠道,";
     _sql+=" c.DeliveringPlant as 装运仓库,c.TaxClassification as 销售税码,c.LoadingGrp as 装运组 from BW_basic_info a,BW_Form_info b,BW_sales_view c,BW_proposer_info p where b.InFlow='0' and a.FormNO=b.FormNO and c.FormNO=b.FormNO and b.FormNO=p.FormNO "+_wheresql;
     break;
    default :
     _sql="";
     break;

   } 
   if(_sql!="")
   {
    SqlDataAdapter ad=new SqlDataAdapter(_sql,cn);
    ad.Fill(ds);
    return ds;
   }
   else
   {
    return null;
   }
    
  }

posted on 2007-12-11 12:25  heart-in-sky  阅读(248)  评论(0编辑  收藏  举报