动态的Datagrid类

using  System;
using  System.Data;
using  System.Collections;
using  System.ComponentModel;
using  System.Web;
using  System.Web.SessionState;
using  System.Web.UI;
using  System.Web.UI.WebControls;

namespace  IMS.Class
{
    
/// <summary>
    
/// DynamicDataGrid 的摘要说明。
    
/// </summary>

    public class DynamicDataGrid
    
{
        
//        string DataGridName;
        string DataGridHeader;
        
string PermStr;
        Page P;
        DataTable dt;
        DataGrid dg;
        
string ModifyLinkFileName,DeleteLinkFileName,ViewLinkFlieName;
        
string DataGridHeaderStr;
        
int Type;
        
bool Mflag=false;
        
bool Dflag=false;
        
public DynamicDataGrid()
        
{
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }

        
/// <summary>
        
/// 构造动态DataGrid
        
/// </summary>
        
/// <param name="DG">将改变的DataGrid</param>
        
/// <param name="Headerstr">头标题,用于在界面的datagrid头标题上显示的汉字</param>
        
/// <param name="Header">对应于头标题,取数据库中的各字段</param>
        
/// <param name="PermissionStr">权限(字符串,以逗号分隔开)</param>
        
/// <param name="dtable">绑定的数据源(DataTable)</param>
        
/// <param name="LinkModifyFile">修改转向的页面的链接</param>
        
/// <param name="LinkDeleteFile">删除转向的页面的链接</param>
        
/// <param name="LinkViewFile">浏览转向的页面的链接</param>
        
/// <param name="TypeId">int型</param>

        public DynamicDataGrid(Page p, DataGrid DG,string Headerstr,string Header,string PermissionStr,DataTable dtable,string LinkModifyFile,string LinkDeleteFile,string LinkViewFile,int TypeId)
        
{
            
this.P=p;
            
this.dg=DG;
//            this.dg.DataSource=null;
            this.DataGridHeader=Header;
            
this.DataGridHeaderStr=Headerstr;
            
this.dt=dtable;
            
this.DeleteLinkFileName=LinkDeleteFile;
            
this.ModifyLinkFileName=LinkModifyFile;
            
this.ViewLinkFlieName=LinkViewFile;
            
this.PermStr=PermissionStr;
            
this.Type=TypeId;
            
this.ModifyDataGrid();
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }


        
public DataGrid ModifyDataGrid()
        
{
            
//datagrid样式
            this.dg.Columns.Clear();
            
this.dg.AllowSorting=true;//允许排序
            this.dg.AllowPaging=true;//允许分页
            this.dg.PageSize=20;//每页15项
            this.dg.PagerStyle.Mode=PagerMode.NumericPages;
            
this.dg.PagerStyle.BackColor=System.Drawing.Color.FromName("#AAAADD");
            
this.dg.PagerStyle.HorizontalAlign=HorizontalAlign.Right;
            
this.dg.AutoGenerateColumns=false;
            
this.dg.BackColor=System.Drawing.Color.White;//datagrid背景色设为天蓝色
            this.dg.BorderStyle=BorderStyle.Ridge;//边框样式
            this.dg.BorderWidth=1;//DataGrid的边框宽度
//            this.dg.CaptionAlign=TableCaptionAlign.Bottom;
            this.dg.HorizontalAlign=HorizontalAlign.Center;//datagrid相对于页面居中
            this.dg.ItemStyle.HorizontalAlign=HorizontalAlign.Left;//datagrid中文字居左对齐
            this.dg.CellPadding=5;
            
this.dg.CellSpacing=1;
            
this.dg.DataKeyField="Id";
            
this.dg.HeaderStyle.BackColor=System.Drawing.Color.FromName("#D6DFF7");
            
this.dg.HeaderStyle.HorizontalAlign=HorizontalAlign.Left;//标题居左对齐
            this.dg.HeaderStyle.Font.Bold=true;//标题字体加粗
            this.dg.HeaderStyle.Font.Size=FontUnit.XLarge;
            
this.dg.AlternatingItemStyle.BackColor=System.Drawing.Color.AliceBlue;
            
this.dg.ItemCreated+=new DataGridItemEventHandler(dg_ItemCreated);
            
this.dg.Attributes.Add("style","word-break:keep-all;word-wrap:normal");
            
this.dg.PageIndexChanged+=new DataGridPageChangedEventHandler(dg_PageIndexChanged);
            
//动态创建模板列
            string[] HeaderStr=this.DataGridHeader.Split(',');
            
string[] HeaderStr1=this.DataGridHeaderStr.Split(',');
            
int HeaderCount=HeaderStr.Length;
            
for(int i=0;i<HeaderCount;i++)
            
{
                TemplateColumn Tm
=new TemplateColumn();
                Tm.ItemTemplate
=new ColumnTemplate(HeaderStr[i],this.P);
                Tm.HeaderText
=HeaderStr1[i];
                
this.dg.Columns.Add(Tm);
            }

            
//建立查看、修改和删除列
            if(this.Type==1)
            
{
                HyperLinkColumn hl
=new HyperLinkColumn();
                hl.HeaderText
="浏览";
                hl.Text
="浏览";
                hl.ItemStyle.Width
=30;
                hl.HeaderStyle.Width
=30;
                
this.dg.Columns.Add(hl);

            }

            
else if(this.Type==0)
            
{
                
if(this.PermStr.IndexOf("修改")>-1)
                
{
                    HyperLinkColumn hl
=new HyperLinkColumn();
                    hl.HeaderText
="修改";
                    hl.Text
="修改";
                    hl.ItemStyle.Width
=30;
                    hl.HeaderStyle.Width
=30;
                    
this.dg.Columns.Add(hl);
                    
this.Mflag=true;
                    
if(this.PermStr.IndexOf("删除")>-1)
                    
{
                        HyperLinkColumn hl1
=new HyperLinkColumn();
                        hl1.HeaderText
="删除";
                        hl1.Text
="删除";
                        hl1.ItemStyle.Width
=30;
                        hl1.HeaderStyle.Width
=30;
                        
this.dg.Columns.Add(hl1);
                        
this.Dflag=true;

                    }

                }

                
else if(this.PermStr.IndexOf("删除")>-1)
                
{
                    HyperLinkColumn hl
=new HyperLinkColumn();
                    hl.HeaderText
="删除";
                    hl.Text
="删除";
                    hl.ItemStyle.Width
=30;
                    hl.HeaderStyle.Width
=30;
                    
this.dg.Columns.Add(hl);
                    
this.Dflag=true;
                }

            }

            
else if(this.Type==3)
            
{
                    HyperLinkColumn hl
=new HyperLinkColumn();
                    hl.HeaderText
="浏览";
                    hl.Text
="浏览";
                    hl.ItemStyle.Width
=30;
                    hl.HeaderStyle.Width
=30;
                    
this.dg.Columns.Add(hl);
                    
this.Mflag=true;
                    HyperLinkColumn hd
=new HyperLinkColumn();
                    hd.HeaderText
="删除";
                    hd.Text
="删除";
                    hd.ItemStyle.Width
=30;
                    hd.HeaderStyle.Width
=30;
                    
this.dg.Columns.Add(hd);
                    
this.Dflag=true;
            }

            
while(this.dg.CurrentPageIndex   *   this.dg.PageSize   >   this.dt.Rows.Count)
            
{
                
this.dg.CurrentPageIndex--
            }

            
try
            
{
                
this.dg.DataSource=this.dt;
                
this.dg.DataBind();
                
return this.dg;
            }

            
catch(Exception err)
            
{
                
throw new Exception(err.Message);
            }

        }



        
private void dg_ItemCreated(object sender, DataGridItemEventArgs e)
        
{
            
for(int i = 0; i < e.Item.Cells.Count; i++
            
// e.Item.Cells[x]是当前行中的单元格 
                e.Item.Cells[i].Wrap = false
            }
 

            
if ( e.Item.ItemType == ListItemType.Pager)
            
{
                
//Add a new Label Control
                Label lblPagerText = new Label();
                lblPagerText.Text 
= " 页码 --> " ;
                lblPagerText.ID 
= "lblPagerText1";
                lblPagerText.Font.Bold 
= false;
                lblPagerText.Attributes.Add(
"onmouseover","this.style.fontWeight='bold'");
                lblPagerText.Attributes.Add(
"onmouseout","this.style.fontWeight='normal'");
                
//Finally add label to Pager control collection 
                if ( e.Item.Controls[0].FindControl("lblPagerText1" ) == null)
                
{
                
                    e.Item.Controls[
0].Controls.AddAt(0,lblPagerText);
                }

                
return;
            }
//end if 

            
if(this.dg.CurrentPageIndex   *   this.dg.PageSize   >   this.dt.Rows.Count)
            
{
                
this.dg.CurrentPageIndex--
                
return;
            }


            
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem) 
            

                
int Id=(int)this.dg.DataKeys[e.Item.ItemIndex];
                
bool Flag=false;
                
int QualiTypeId=-1;
                DataChecked GetQuali
=new DataChecked();
                
if(this.DataGridHeaderStr.IndexOf("工作单位")!=-1)
                    QualiTypeId
=GetQuali.GetQualification(Id,"DelegateInfo");
                
else if(this.DataGridHeaderStr.IndexOf("代表资格")!=-1)
                    QualiTypeId
=GetQuali.GetQualification(Id,"QualificationInfo");
                
else if(this.DataGridHeaderStr.IndexOf("履职项目")!=-1)
                    QualiTypeId
=GetQuali.GetQualification(Id,"DelegateDuty");
                
if(QualiTypeId==2)
                
{
                    Flag
=true;
                    e.Item.ForeColor
=System.Drawing.Color.Gray;
                }

                
else if(QualiTypeId==3)
                
{
                    Flag
=true;
                    e.Item.ForeColor
=System.Drawing.Color.Red;
                }

                
                
if(this.Type==1)
                
{
                    ((HyperLink)e.Item.Cells[e.Item.Cells.Count
-1].Controls[0]).NavigateUrl=this.ViewLinkFlieName+"?id="+Id.ToString();
                }

                
else if(this.Type==0)
                
{
                    
if(Flag)
                        
return;
                    
if(this.Mflag&&this.Dflag)
                    
{
                        ((HyperLink)e.Item.Cells[e.Item.Cells.Count
-2].Controls[0]).NavigateUrl=this.ModifyLinkFileName+"?id="+Id.ToString();
                        ((HyperLink)e.Item.Cells[e.Item.Cells.Count
-1].Controls[0]).Attributes.Add("onclick",   "return confirm('您真的要删除吗?')");
                        ((HyperLink)e.Item.Cells[e.Item.Cells.Count
-1].Controls[0]).NavigateUrl=this.DeleteLinkFileName+"?id="+Id.ToString();
                    }

                    
else if(this.Dflag)
                    
{
                        ((HyperLink)e.Item.Cells[e.Item.Cells.Count
-1].Controls[0]).Attributes.Add("onclick",   "return confirm('您真的要删除吗?')");
                        ((HyperLink)e.Item.Cells[e.Item.Cells.Count
-1].Controls[0]).NavigateUrl=this.DeleteLinkFileName+"?id="+Id.ToString();
                    }

                    
else if(this.Mflag)
                    
{
                        ((HyperLink)e.Item.Cells[e.Item.Cells.Count
-1].Controls[0]).NavigateUrl=this.ModifyLinkFileName+"?id="+Id.ToString();
                    }

                }

                
else if(this.Type==3)
                
{
                    
if(Flag)
                        
return;
                    
if(this.Mflag&&this.Dflag)
                    
{
                        ((HyperLink)e.Item.Cells[e.Item.Cells.Count
-2].Controls[0]).NavigateUrl=this.ModifyLinkFileName+"?id="+Id.ToString();
                        ((HyperLink)e.Item.Cells[e.Item.Cells.Count
-1].Controls[0]).Attributes.Add("onclick",   "return confirm('您真的要删除吗?')");
                        ((HyperLink)e.Item.Cells[e.Item.Cells.Count
-1].Controls[0]).NavigateUrl=this.DeleteLinkFileName+"?id="+Id.ToString();
                    }

                    
else if(this.Dflag)
                    
{
                        ((HyperLink)e.Item.Cells[e.Item.Cells.Count
-1].Controls[0]).Attributes.Add("onclick",   "return confirm('您真的要删除吗?')");
                        ((HyperLink)e.Item.Cells[e.Item.Cells.Count
-1].Controls[0]).NavigateUrl=this.DeleteLinkFileName+"?id="+Id.ToString();
                    }

                    
else if(this.Mflag)
                    
{
                        ((HyperLink)e.Item.Cells[e.Item.Cells.Count
-1].Controls[0]).NavigateUrl=this.ModifyLinkFileName+"?id="+Id.ToString();
                    }

                }

                
//end if

                e.Item.Attributes.Add(
"onmouseover","this.style.backgroundColor='seashell'"); 
                
if(e.Item.ItemType == ListItemType.Item) 
                

                    e.Item.Attributes.Add(
"onmouseout""this.style.backgroundColor='white'"); 
                }
 

                
if(e.Item.ItemType ==ListItemType.AlternatingItem) 
                

                    e.Item.Attributes.Add(
"onmouseout""this.style.backgroundColor='AliceBlue'"); 
                }
 


            }
//end if
        }





        
private void dg_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        
{
//            int count = this.dt.Rows.Count;
//            int Page=((count%this.dg.PageSize)>0)?(count/this.dg.PageSize)+1:(count/this.dg.PageSize);
//            this.dg.CurrentPageIndex=(Page>e.NewPageIndex)?e.NewPageIndex:Page;
            this.dg.CurrentPageIndex=e.NewPageIndex;
            
this.dg.DataSource=this.dt;
            
this.dg.DataBind();
        }





    }


    
public class ColumnTemplate : ITemplate 
    

        
//第一个模板列 
        string Str;
        Page p;
        
public ColumnTemplate(string str,Page P)
        
{
            
this.p=P;

            
this.Str=str;
        }

        
public void InstantiateIn(Control container)       
        

            LiteralControl l 
= new LiteralControl(); 
            l.DataBinding 
+= new EventHandler(this.OnDataBinding); 
            
//数据绑定 
            container.Controls.Add(l); 
            
//为模板列加入LiteralControl 
        }
 

        
public void OnDataBinding(object sender, EventArgs e) 
        

            LiteralControl l 
= (LiteralControl) sender;//LiteralControl发送绑定请求 
            DataGridItem container = (DataGridItem) l.NamingContainer; 
            
if(((DataRowView)container.DataItem)[this.Str].ToString()=="")
                l.Text
=@"&nbsp;";
            
else
            
{
                l.Text 
=this.p.Server.HtmlEncode(((DataRowView)container.DataItem)[this.Str].ToString());//绑定字段
            }

        }
 
    }
 
}

动态的datagrid类

你可能感兴趣的:(datagrid)