相信大家也和我一样遇到过这种问题,gridview在生成列的时候当列不确定怎么办?下面分享一下自己的解决方法。

举个列子说明一下。

普通列的添加比较简单。

BoundField bf = new BoundField();
            bf.HeaderText = "表头名称";
            bf.DataField = "数据源对应字段名称";

gridview.Columns.Add(bf)

那像TemplateField模板列怎么办?这时候需要自己继承模板接口来重写

public class GridViewCreate:ITemplate
{
    Dictionary<string, string> controlName;
    Dictionary<string,CommandEventHandler> controlEvent;
    Dictionary<string,string>controlImgUrl;
    Dictionary<string, string> controlData;
    DataControlRowType rType;
    /// <summary>
    /// 处理itemplage
    /// T要创建模板的类型
    /// cName为模板控件的名字,键为控件的id值为控件的类型 目前只处理checkbox和imgbutton
    /// cEvent为模块控件对应的事件键为控件id值为控件事件的名字,这里的键要和cName中的键相对应
    /// cImageUrl是图片按钮对应的图片路径键id 值路径
    /// </summary>
    /// <param name="cName"></param>
    /// <param name="cEvent"></param>
    public GridViewCreate(DataControlRowType T, Dictionary<string, string> cName, Dictionary<string, CommandEventHandler> cEvent, Dictionary<string, string> cImgUrl)
    {
        rType = T;
        controlEvent = cEvent;
        controlName = cName;
        controlImgUrl = cImgUrl;
        controlData = cData;
    }
    /// <summary>
    /// 处理header
    /// </summary>
    /// <param name="T"></param>
    public GridViewCreate(DataControlRowType T)
    {
        rType = T;
    }
    #region ITemplate 成员

public void InstantiateIn(Control container)
    {
        if (rType == DataControlRowType.Header)
        {
            HtmlInputCheckBox cb = new HtmlInputCheckBox();
            cb.ID = "chkAll";
            cb.Attributes.Add("runat","server");
            cb.Attributes.Add("onclick", "javascript:SelectAllCheckboxes(this);");
            container.Controls.Add(cb);
        }
        else if (rType == DataControlRowType.DataRow)
        {
            if (controlName != null)
            {
                foreach (string key in controlName.Keys)
                {
                    switch (controlName[key])
                    {
                        case "checkbox":
                            CheckBox cb = new CheckBox();
                            cb.ID = key;
                            container.Controls.Add(cb);
                            break;
                        case "imgbutton":
                            ImageButton ib = new ImageButton();
                            ib.ID = key;
                            ib.Width = 20;
                            ib.Height = 20;
                            ib.EnableViewState = true;
                            if (controlImgUrl != null)
                            {
                                ib.ImageUrl = controlImgUrl[key];//给图片按钮添加图片
                            }
                            if (controlEvent != null)
                            {
                                ib.Command += controlEvent[key];//添加点击事件
                            }
                            container.Controls.Add(ib);
                            break;
                    }
                }
            }
        }
    }

#endregion
}

利用自己重写的类添加模板列

TemplateField tfbutton = new TemplateField();
                Dictionary<string, string> dcbutton = new Dictionary<string, string>();//图片按钮数据集
                dcbutton.Add("btnEdit", "imgbutton");
                Dictionary<string, CommandEventHandler> dcbtnEvent = new Dictionary<string, CommandEventHandler>();//按钮对应的事件集合
                dcbtnEvent.Add("btnEdit", new CommandEventHandler(btnEdit_Click));
                Dictionary<string, string> diImg = new Dictionary<string, string>();//按钮对应的图片集合
                diImg.Add("btnEdit", "~/image/2modify.png");
                tfbutton.ItemTemplate = new GridViewCreate(DataControlRowType.DataRow, dcbutton, dcbtnEvent, diImg);
                tfbutton.HeaderText = "操作";
                gridview.Columns.Add(tfbutton);

有什么不对的地方请多多指教。

最新文章

  1. 使用jenkins配置.net mvc网站进行持续集成二
  2. C#------连接SQLServer和MySQL字符串
  3. 那些年一起用过的iOS开发利器之Code Pilot
  4. unity5.3 安卓广告插件打包出错的理解
  5. IOS框架和服务
  6. [转] Oracle analyze table 使用总结
  7. .Net 程序集 签名工具sn.exe 密钥对SNK文件 最基本的用法
  8. csdn博客刷粉代码
  9. AOP Concepts
  10. 聊聊String
  11. 垃圾收集器(GC)
  12. Dubbo 源码分析系列之一环境搭建
  13. CSS3:透明度
  14. python绝对路径相对路径函数
  15. Python3编写网络爬虫03-正则表达式的使用
  16. js模拟自动点击事件
  17. 校验 CentOS 7 镜像文件
  18. pfSense 2.4.3 发布,包含重要的安全修复补丁
  19. python错误处理/调试/单元测试/文档测试
  20. A - Chips

热门文章

  1. Connect mysql on Linux from Windows
  2. To Build A Dev Env On Linux(Ubuntu)
  3. Asp.Net Mvc - 在OnResultExecut* 拦截Action返回的HTML
  4. SharePoint 2013 弹窗效果之URL打开方式(一)
  5. bzoj1136: [POI2009]Arc
  6. Android 5.0 技术新趋势
  7. keil uVision4一些使用总结(汉字注释,C关键字等)
  8. Linxu安装Lamp环境
  9. BZOJ 1071 [SCOI2007]组队
  10. 1710: [Usaco2007 Open]Cheappal 廉价回文