gridview动态添加列的问题

时间:2021-02-20 22:39:30

相信大家也和我一样遇到过这种问题,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);

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