ASPxGridView dynamically creates a table column editing template

Source: Internet
Author: User

The DevExpress ASPxGridview control is used in the project. Every time I configure it, I always feel awkward. So I thought of using code to automatically configure it.

So with this Code:

Foreach (ZiyuWeb. entity. sys_UIConfig item in sConfigs) {switch (item. controlName) {case "ASPxDropDown_ASPxCheckListBox": {DevExpress. web. ASPxGridView. gridViewDataDropDownEditColumn col = new GridViewDataDropDownEditColumn (); // create a table column; col. fieldName = item. fieldName; // name of the data source field associated with the table column col. caption = item. fieldDescribe; // The Field displays the title col. name = "col _" + item. fieldName; // column name col. visibleIndex = item. orderByID; // Display the order col. width = item. width; // display Width col. visible = item. userisvisvisible; // whether col is visible. readOnly = item. userReadOnly; // read-only/col. editItemTemplate = new ZiyuWeb. ASPxCheckListBox_DropDownTemplate ("", item. queryStr, col. name); // create an editing template for this field, if it is a standard DevExpress. web. ASPxEditor does not need to create or edit a template. However, if it is DropDown or a self-written control, it must be created on its own. Mygridview. Columns. Add (col); // Add to View; break ;}...... }}

With the table created by the above Code, the ASPxGridview control can be perfectly presented with a line of code by setting the configuration file name and data source.

ZiyuWeb. WebFunc. ZiyuDevHelper. configGridView (ASPxGridView1, "Sys_UIConfig", true, null );

 

Create EditItemTemplate.

    class MyTemplate_AspxCheckBoxList : ITemplate    {        int _iwidth = 80;        public int Iwidth        {            get { return _iwidth; }            set { _iwidth = value; }        }        ZiyuWeb.Entity.Sys_UIConfig uiconfig;        public MyTemplate_AspxCheckBoxList(ZiyuWeb.Entity.Sys_UIConfig myuiconfig)        {            uiconfig = myuiconfig;            Iwidth = uiconfig.Width;        }        public void InstantiateIn(Control container)        {            DevExpress.Web.ASPxEditors.ASPxCheckBoxList alistbox = new ASPxCheckBoxList();            alistbox.TextField = uiconfig.FieldName;            alistbox.ValueField = uiconfig.FieldName;            if (uiconfig.QueryStr != null)            {                if (uiconfig.QueryStr.Length > 0)                {                    if (uiconfig.QueryStr.IndexOf("select") >= 0)                    {                        System.Collections.ArrayList alist = MyHelper4Web.MyStringHelper.getTableNamefromSql(uiconfig.QueryStr);                        XCode.DataAccessLayer.DAL mydal = XCode.DataAccessLayer.DAL.Create("common");                        System.Data.DataTable dt = mydal.Select(uiconfig.QueryStr, alist[0].ToString()).Tables[0];                        if (dt.Columns.Count == 1)                        {                            for (int i = 0; i < dt.Rows.Count; i++)                            {                                alistbox.Items.Add(dt.Rows[i][0].ToString());                            }                        }                    }                    else                    {                        System.Collections.ArrayList alist =                        MyHelper4Web.MyConvertHelper.ConvertStringToArrayList(uiconfig.QueryStr, ";");                        for (int i = 0; i < alist.Count; i++)                        {                            alistbox.Items.Add(alist[i].ToString());                        }                    }                }            }            alistbox.ID = "myCon_" + uiconfig.FieldName;            alistbox.ClientInstanceName = "myCon_" + uiconfig.FieldName;            alistbox.RepeatColumns = 3;            alistbox.TextWrap = false;            alistbox.RepeatDirection = RepeatDirection.Horizontal;            alistbox.RepeatLayout = RepeatLayout.Flow;            string rowValue = (container as GridViewEditItemTemplateContainer).Text;            string[] rowValueItems = rowValue.Split(';');            List<string> rowValueItemsAsList = new List<string>();            rowValueItemsAsList.AddRange(rowValueItems);            foreach (ListEditItem item in alistbox.Items)                if (rowValueItemsAsList.Contains(item.Value.ToString()))                    item.Selected = true;            container.Controls.Add(alistbox);        }    }

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.