This is a migrated thread and some comments may be shown as answers.

Hide Rows in Grid Edit Form

1 Answer 153 Views
Grid
This is a migrated thread and some comments may be shown as answers.
diego
Top achievements
Rank 1
diego asked on 26 May 2016, 12:38 PM

hello 
i have the following problem
i have a radgrid which is totally builded in code behind because it must be bound to different data types
you can see a preview in this page
the user may update a record or insert a new one
when entering in edit mode, some rows in the edit form should be hidden based on the selection of the main item (the INST_MODEL value that cannot be changed in edit mode)
following is the codebehind of the ascx (i omit fo simplicity the part of code that builds customedittemplate)

so, when selecting an item with INST_MODEL = GA_CP-LI-COR LI-7200, for example, the rows with INST_SA[...] labels should be hidden

how can i do it?!

thanks a lot

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Collections;
using System.Data;
using System.Collections.Specialized;
using System.Web.UI.HtmlControls;
 
namespace SitefinityWebApp.EcoControls
{
    public partial class BADMView : System.Web.UI.UserControl
    {
        string conn = System.Configuration.ConfigurationManager.ConnectionStrings["connMulti"].ConnectionString;
        BadmVar bv = null;
        int idVar = 1000;
        int idSite = 0;
        int insertUserId = 0;
        public string I_MODEL="";
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["idutente"] = 127;
            Session["usname"] = "database";
            if (Session["idutente"] != null && Session["idutente"].ToString()!="")
            {
                insertUserId = int.Parse(Session["idutente"].ToString());
            }
        }
 
        protected void Page_Init(object sender, EventArgs e)
        {
            /***********************/
            //if (Request["idvar"] != null && Request["idvar"].ToString() != "0")
            if (Request["idsite"] != null && Request["idsite"].ToString() != "")
            {
                //Response.Write("idvar:: " + Request["idvar"].ToString());
                //idVar = int.Parse(Request["idvar"].ToString());
                //pickUpVars.SelectedValue = Request["idvar"].ToString();
                idSite = int.Parse(Request["idsite"].ToString());
                sitesDDL.SelectedValue = Request["idsite"].ToString();
            }
            else
            {
                return;
            }
            //1. Get theBadm Variable Infos
            SqlConnection cn = new SqlConnection();
            cn.ConnectionString = conn;
            cn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = cn;
            cmd.CommandText = "...";
             
 
            //2. Build the grid
            RadGrid grid = new RadGrid();
            grid.ID = "instGrid";
            grid.DataSourceID = "sqlInst";
            grid.PageSize = 15;
            grid.AllowPaging = true;
            grid.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
            grid.AutoGenerateColumns = false;
            grid.AllowAutomaticUpdates = false;
            grid.AllowAutomaticInserts = false;
            grid.AllowAutomaticDeletes = false;
            grid.AllowMultiRowEdit = true;
            //grid.AllowMultiRowSelection = true;
            grid.Skin = "Bootstrap";
            grid.ItemCommand += new GridCommandEventHandler(instGrid_ItemCommand);
            grid.ItemDataBound += new GridItemEventHandler(instGrid_ItemDataBound);
 
            //2.1 Add Customers table 
            grid.MasterTableView.DataKeyNames = new string[] { "id_badmstorage", "value" };
            if (bv.ID != 1)
            {
                grid.MasterTableView.EditMode = GridEditMode.EditForms;
                grid.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top;
            }
            GridBoundColumn boundColumn = new GridBoundColumn();
            boundColumn.DataField = "id_badmstorage";
            boundColumn.HeaderText = "ID";
            boundColumn.ReadOnly = true;
            grid.MasterTableView.Columns.Add(boundColumn);
 
            //2.2 SET SQL DATA SOURCE...
             
            sqlInst.SelectCommand = "...";
            //3. Add grid columns...
            GridTemplateColumn tempC = null;
            tempC = new GridTemplateColumn();
            tempC.DataField = "value";
            tempC.HeaderText = bv.Name;
            tempC.UniqueName = "value";
            tempC.ItemTemplate = new MyTemplate("value", bv.CvIndex, true);
            tempC.EditItemTemplate = new CustomEditTemplate("value", true, bv.Unit, bv.CvIndex, bv.ID);
             
            grid.MasterTableView.Columns.Add(tempC);
             
            if (bv.QualifierList != null)
            {
                for (int i = 0; i < bv.QualifierList.Count; i++)
                {
                    string temp = (bv.QualifierList[i].QualIndex != "") ? bv.QualifierList[i].QualIndex : bv.QualifierList[i].RealQualName;
                    tempC = new GridTemplateColumn();
                    tempC.DataField = temp;
                    tempC.HeaderText = bv.QualifierList[i].QualName;
                    tempC.UniqueName = bv.QualifierList[i].QualName;
                    tempC.ItemTemplate = new MyTemplate(temp, bv.QualifierList[i].CvIndex, bv.QualifierList[i].Required);
                    tempC.EditItemTemplate = new CustomEditTemplate(temp, bv.QualifierList[i].Required, bv.QualifierList[i].Type, bv.QualifierList[i].CvIndex, bv.ID);
                    tempC.Visible = (i <= 5);
                    grid.MasterTableView.Columns.Add(tempC);
                }
            }
 
            //4. Edit commands
            if (bv.ID != 1)
            {
                GridEditCommandColumn eCol = new GridEditCommandColumn();
                eCol.UniqueName = "EditCommandColumn";
                grid.MasterTableView.Columns.Add(eCol);/**/
            }
 
            //5. delete command
            if (bv.ID != 1)
            {
                /*tempC = new GridTemplateColumn();
                tempC.UniqueName = "DeleteColumn";
                // tempC.HeaderText = "Delete";
                tempC.ItemTemplate = new DeleteTemplate("delete");
                grid.MasterTableView.Columns.Add(tempC);*/
            }
           // grid.ClientSettings.ClientEvents = new GridClientEvents(
            //6. Add the grid to the placeholder
             
            this.PlaceHolder1.Controls.Add(grid);
 
            //AJAX SETTINGS...
            AjaxSetting ajax = new AjaxSetting(grid.ID);
            AjaxUpdatedControl up = new AjaxUpdatedControl();
            up.ControlID = grid.ID;
            ajax.UpdatedControls.Add(up);
            up = new AjaxUpdatedControl();
            ajax.UpdatedControls.Add(up);
            RadAjaxManager1.AjaxSettings.Add(ajax);
        }
 
        private bool compareHTable(Hashtable tnew, Hashtable told)
        {
            foreach (string key in tnew.Keys)
            {
                if (tnew[key] != told[key])
                {
                    return false;
                }
            }
            return true;
        }
 
        public int IdVar
        {
            get { return idVar; }
        }
 
         
 
        protected void instGrid_ItemCommand(object sender, GridCommandEventArgs e)
        {
            string body = "";
            SqlConnection cn = new SqlConnection(conn);
            cn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = cn;
 
            if (e.CommandName == "Update")
            {
                //Perform updates
            }
            else if (e.CommandName == "InitInsert")
            {
 
            }
            else if (e.CommandName == "Edit")
            {
                //Edit code
 
            }
            else if (e.CommandName == "PerformInsert")
            {
                //Insert code
            }
            else if (e.CommandName == "Delete")
            {
                
                GridEditableItem editedItem = e.Item as GridEditableItem;
                string idbm = editedItem.GetDataKeyValue("id_badmstorage").ToString();
                cmd.CommandText = "UPDATE BADMStorage16 SET dataStatus=404, deleteUserId="+insertUserId+", deletedDate='" + DateTime.Now.ToString("yyyy-MM-dd HH:mm") + "' WHERE id_badmstorage=" + idbm;
                cmd.ExecuteNonQuery();
                body += "\nDeleted item for variable "+bv.Name+", record ID="+idbm+" by " + Session["usname"].ToString();
            }
            cn.Close();
             
        }
         
    }

1 Answer, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 31 May 2016, 07:27 AM
Hello Diego,

For such conditional requirement for the edit form you will have to handle the OnItemDataBound event, get reference to the edit item and depending on the model value, change the Visible property of the editors you need to hide:
Hope this helps.


Regards,
Konstantin Dikov
Telerik
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Tags
Grid
Asked by
diego
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Share this question
or