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

RadGrid CheckList

5 Answers 259 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Gionata
Top achievements
Rank 1
Gionata asked on 31 Mar 2014, 05:16 PM
Hi everyone!
I have created a simple project containing a RadGrid with CheckListFilter on its columns and i would like to make two questions about some little problems related to this project: 

1 - How can I work on the checklist's window to make some simple operation like adjust its size and enable or disable the scroll? (I tried with filtermenu but it doesn't work). 

2 - Is it possible to control the properties of an element of RadListBoxItem type to be checked or unchecked in the code-behind? I tried to set its "checked" property equals to true in code-behind but it doesn1t work (I don't know if it can be due to a javascript error that is returned to me with ie9 every time I click on the icon to get the checklist: "Microsoft JScript runtime error: Object does not support property or method 'indexOf'" or to some other reason).

Thanks in advance,
Gionata.

5 Answers, 1 is accepted

Sort by
0
Accepted
Vasil
Telerik team
answered on 03 Apr 2014, 03:25 PM
Hi Gionata,

I will prepare an example of this and paste it in the other forum thread that you have started on the same subject:
http://www.telerik.com/forums/filterchecklistenableloadondemand-property

Regards,
Vasil
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Gionata
Top achievements
Rank 1
answered on 04 Apr 2014, 07:57 AM
Ok, thank you, i'll be waiting for this...now i' m a bit stuck on this problem.
0
Gionata
Top achievements
Rank 1
answered on 04 Apr 2014, 10:23 AM
Vasil, i thought another possible solution; since might be also other little problems related to my project, i believe that a useful solution would be to pass you on my project so that you, or someone else, could see how it exactly works and understand why there are some problems. 
Since in the forum I am allowed to upload only image files, where I can pass you the zip file of my project?
My Trial Version will be expired in the newxt few days; howevew the company I work for, will buy your product; so can you keep in touch even if the trial will be expired in the next few days?

Thanks,
Gionata.
0
Vasil
Telerik team
answered on 04 Apr 2014, 01:40 PM
Hi Gionata,

We are reading and if possible answering every forum. So we can still answer your questions even after your trial expires. Since the posts are public, they can be helpful to everyone. Posting your code with good description of what you are trying will indeed help us to show you the correct examples. Preparing general example sometimes takes more time, since we can not know what you know and what exact parts you have problems with.

You can post samples of the code directly here, just make sure to use the Format Code Block, to make the code readable.

Regards,
Vasil
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Gionata
Top achievements
Rank 1
answered on 04 Apr 2014, 04:54 PM
Ok Vasil, that's my code; as you can see the Grid is entirely created programmatically (in the code behind); my purpose is to create the Grid programmatically depending on the content that will be read from  a parameter passed by a WCF...that grid hold a filterchecklist for each column; similarly to what happens for the excel - filters, if a filter is applied to a column, the corresponding items in the checklist_filter of other columns must been disabled while the filtered items must been unchecked; this is done using the auxiliary structure "Status" that holds the status for every element of the Grid (enabled, disbled or filtered in the checklist), and enabling/checking programmatically items of checklist in the code behind.

It doesn't work beacause of the problems i wrote in the last posts (particullary it's impossible to set the "Checked" property for the items) and because filterexpression is always an empty String.

That's all, if there is something else you need to know, please contact me.

Thanks.

<style type="text/css">
  .HoverClass
  {  
      background-color: aqua;
  }
  .DefaultClass
  {
      background-color: white;
  }
  .ClickClass
  {
      background-color: yellow;
  }
  </style>
  <script type="text/javascript">
      function RowCreated(sender, eventArgs) {
          var dataItem = eventArgs.get_gridDataItem();
          for (var i = 0; i < dataItem.get_element().cells.length; i++) {
              dataItem._element.cells[i].onmouseover = function () {
                  this.className = "HoverClass";
              }
              dataItem.get_element().cells[i].onmouseout = function () {
                  var cssName = this.selected ? "ClickClass" : "DefaultClass";
                  this.className = cssName;
                  return;
              }
              dataItem.get_element().cells[i].onclick = function (event) {
                  this.selected = this.selected == true ? false : true;
                  var cssName = this.selected ? "ClickClass" : "DefaultClass";
                  this.className = cssName;
                  return;
              }
          }
      }
      function CheckListShow(sender, eventArgs) { 
      
      }    
  </script>
  <telerik:RadFormDecorator ID="QsfFromDecorator" runat="server" DecoratedControls="All" EnableRoundedCorners="false" />
  <telerik:RadGrid runat="server" ID="RadGrid1" AllowFilteringByColumn="true" FilterType="CheckList" OnNeedDataSource="RadGrid1_NeedDataSource" OnFilterCheckListItemsRequested="RadGrid1_NeedCheckListItems">
    <MasterTableView AutoGenerateColumns="False">
      <Columns />
    </MasterTableView>
    <ClientSettings>
      <ClientEvents OnRowCreated="RowCreated" OnFilterMenuShowing="CheckListShow" />
    </ClientSettings>
  </telerik:RadGrid>

CODE BEHIND:

     public partial class RadGridWebPartUserControl : UserControl
    {
        private const int DISABLED = 0;
        private const int ENABLED = 1;
        private const int FILTER = 2;
        
        private static bool Paging;                                          // Paging(true) - Scrolling(false)
        private static DataTable Data = new DataTable();                     // Content of Grid
        private static DataTable DataFiltered;                               // Content of Grid Filtered
        private static String LastFilterExpression;                          // Last value of Filter Expression
        private static int[,] Status;                                        // Auxiliary structure useful to fill FilterCheckList

        
        // INITIALIZE SCRIPT MANAGER AND DATA-MEMBERS

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
            if (scriptManager == null)
            {
                scriptManager = new RadScriptManager();
                this.Page.Form.Controls.AddAt(0, scriptManager);
            }
            if (Data.Rows.Count == 0)
            {
                DataSet Ds = new DataSet();
                Ds.ReadXml("C:\\Users\\spadmin\\Documents\\Visual Studio 2010\\Projects\\Telerik\\CheckListFiltering (Webpart)\\CheckListFiltering\\App_Data\\data.xml");  // TODO: Read parameter from a WCF
                Data = Ds.Tables[0];
                Paging = true;  // TODO: Read parameter from a WCF
                DataFiltered = Data.Select("").Length != 0 ? Data.Select("", Data.Columns[0].ColumnName + " ASC").CopyToDataTable() : new DataTable();
                LastFilterExpression = "";
                Status = new int[Data.Rows.Count, Data.Columns.Count];
                for (int i = 0; i < Data.Rows.Count; i++)
                    for (int j = 0; j < Data.Columns.Count; j++)
                        Status[i, j] = ENABLED;
            }
        }

        // INITIALIZE AJAX MANAGER

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            RadAjaxManager ajaxManager = RadAjaxManager.GetCurrent(this.Page);
            if (ajaxManager == null)
            {
                ajaxManager = new RadAjaxManager();
                ajaxManager.ID = "RadAjaxManager1";
                Controls.Add(ajaxManager);
                this.Page.Items.Add(typeof(RadAjaxManager), ajaxManager);
            }
        }

        // SETTING OF GRID

        protected void Page_Load(object sender, EventArgs e)
        {
            RadGrid1.Width = 220 * Data.Columns.Count;
            if (Paging)
            {
                RadGrid1.AllowPaging = true;
                RadGrid1.PagerStyle.AlwaysVisible = true;
            }
            else
            {
                RadGrid1.ClientSettings.Scrolling.AllowScroll = true;
                RadGrid1.ClientSettings.Scrolling.ScrollHeight = 300;
            }
            RadGrid1.MasterTableView.Columns.Clear();
            for (int i = 0; i < Data.Columns.Count; i++)
            {
                GridBoundColumn boundColumn = new GridBoundColumn();
                boundColumn.DataField = Data.Columns[i].ColumnName;
                boundColumn.HeaderText = Data.Columns[i].ColumnName;
                boundColumn.FilterCheckListEnableLoadOnDemand = true;
                boundColumn.FilterDelay = 200;
                RadGrid1.MasterTableView.Columns.Add(boundColumn);
            }
        }

        // DATA-FILTERING AND DATA-BINDING 

        protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            String filterExpression = Fix_Expression(RadGrid1.MasterTableView.FilterExpression);  // always empty string, why?
            DataFiltered = Data.Select(filterExpression).Length != 0 ? Data.Select(filterExpression, Data.Columns[0].ColumnName + " ASC").CopyToDataTable() : new DataTable();
            Set_Status(filterExpression);
            LastFilterExpression = filterExpression;
            RadGrid1.DataSource = DataFiltered;
        }

        // BUILD FILTER-CHECKLIST

        protected void RadGrid1_NeedCheckListItems(object sender, GridFilterCheckListItemsRequestedEventArgs e)
        {
            int col_index = Get_Index(e.Column.HeaderText);
            DataRow[] dr = Data.Select("", Data.Columns[0].ColumnName + " ASC");
            for (int i = 0; i < dr.Length; i++)
            {
                RadListBoxItem Item = new RadListBoxItem();
                Item.Text = Convert.ToString(dr[i][col_index]);
                Item.Value = Convert.ToString(dr[i][col_index]);
                Item.Enabled = !Status[i, col_index].Equals(DISABLED) ? true : false;
                Item.Checked = !Status[i, col_index].Equals(FILTER) ? true : false;
                bool Found = false;
                for (int k = 0; k < e.ListBox.Items.Count; k++)
                {
                    if (e.ListBox.Items[k].Text.Equals(Item.Text))
                    {
                        Found = true;
                        break;
                    }
                }
                if (!Found)
                    e.ListBox.Items.Add(Item);
            }
        }

        // UTILITY: MANAGE PROPERLY "filterExpression"

        public String Fix_Expression(String filterExpression)
        {
            String newExpression = "";
            String[] Expression = filterExpression.Split(new String[] { " AND " }, StringSplitOptions.RemoveEmptyEntries);
            for (int k = 0; k < Expression.Length; k++)
            {
                if (Expression[k].Contains("LIKE"))
                    Expression[k] = Expression[k].Replace("'%", "'");
                else
                {
                    DataRow[] dr = Data.Select("", Data.Columns[0].ColumnName + " ASC");
                    String col_name = Expression[k].Substring(Expression[k].IndexOf("[") + 1, Expression[k].IndexOf("]") - Expression[k].IndexOf("[") - 1);
                    int col_index = Get_Index(col_name);
                    for (int i = 0; i < Data.Rows.Count; i++)
                    {
                        if (Status[i, col_index].Equals(DISABLED))
                            Expression[k] = Expression[k].Replace(")", " OR [" + col_name + "] = '" + Convert.ToString(dr[i][col_index]) + "')");
                    }
                }
                newExpression = (k != (Expression.Length - 1)) ? newExpression + Expression[k] + " AND " : newExpression + Expression[k];
            }
            return newExpression;
        }

        // UTILITY: MODIFY "Status" BASED ON "filterExpression"

        public void Set_Status(string filterExpression)
        {
            for (int j = 0; j < Data.Columns.Count; j++)
            {
                if (!filterExpression.Contains(Data.Columns[j].ColumnName))
                {
                    for (int i = 0; i < Data.Rows.Count; i++)
                        Status[i, j] = Status[i, j].Equals(FILTER) ? -1 : Status[i, j];
                }
            }
            String[] Expression = filterExpression.Split(new String[] { " AND " }, StringSplitOptions.RemoveEmptyEntries);
            for (int k = 0; k < Expression.Length; k++)
            {
                DataRow[] dr = Data.Select("", Data.Columns[0].ColumnName + " ASC");
                String col_name = Expression[k].Substring(Expression[k].IndexOf("[") + 1, Expression[k].IndexOf("]") - Expression[k].IndexOf("[") - 1);
                int col_index = Get_Index(col_name);
                if (Expression[k].Contains("LIKE"))
                {
                    for (int i = 0; i < Data.Rows.Count; i++)
                        Status[i, col_index] = Status[i, col_index].Equals(FILTER) ? -1 : Status[i, col_index];
                }
                else
                {
                    for (int i = 0; i < Data.Rows.Count; i++)
                    {
                        if (Expression[k].Contains(Convert.ToString("'" + dr[i][col_index] + "'")))
                            Status[i, col_index] = Status[i, col_index].Equals(FILTER) ? -1 : Status[i, col_index];
                        else
                            Status[i, col_index] = FILTER;
                    }
                }
            }
            for (int i = 0; i < Data.Rows.Count; i++)
                for (int j = 0; j < Data.Columns.Count; j++)
                {
                    if (!Is_Filtered(i))
                        Status[i, j] = ENABLED;
                    if (Is_Filtered(i) && !Status[i, j].Equals(FILTER))
                        Fix_Element(i, j);
                }
        }

        // UTILITY: SUPPORT TO MODIFY "Status"

        public void Fix_Element(int row_index, int col_index)
        {
            bool Disabled = true;
            DataRow[] dr = Data.Select("", Data.Columns[0].ColumnName + " ASC");
            for (int i = 0; i < dr.Length; i++)
            {
                if (i != row_index && Convert.ToString(dr[i][col_index]).Equals(Convert.ToString(dr[row_index][col_index])) && !Is_Filtered(i))
                {
                    Disabled = false;
                    break;
                }
            }
            Status[row_index, col_index] = Disabled ? DISABLED : ENABLED;
        }

        // UTILITY: VERIFY IF A SPECIFIC ROW OF THE GRID IS FILTERED

        public bool Is_Filtered(int index)
        {
            for (int j = 0; j < Data.Columns.Count; j++)
            {
                if (Status[index, j].Equals(FILTER))
                    return true;
            }
            return false;
        }

        // UTILITY: RETURNS INDEX OF THE COLUMN OF TABLE FROM ITS NAME

        public int Get_Index(String name)
        {
            for (int j = 0; j < Data.Columns.Count; j++)
            {
                if (Data.Columns[j].ColumnName.Equals(name))
                    return j;
            }
            return -1;
        }

    }


Tags
Grid
Asked by
Gionata
Top achievements
Rank 1
Answers by
Vasil
Telerik team
Gionata
Top achievements
Rank 1
Share this question
or