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

[Solved] How to programmatically create a template column with multiple sub-columns and also add filtering and sorting for all the sub-columns?

3 Answers 270 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Oliver
Top achievements
Rank 1
Oliver asked on 05 Jan 2010, 07:54 PM

Dear All,

I am very new to RadGrid. So please let me know how I can get the following task done. I need to programmatically create an grid with multiple column types. A few columns need to have mulitple sub-columns inside. So as far as my understanding goes, I need to create template column to do this job. So I did the following code and it seems working. However, i do not know how to get the filtering and sorting working with these columns though. Your help will be highly appreciated!

Here is what I have done. The current problem is the the combombox that I added to the filter control does not keep its value after postback. Please let me know what is missing. Thanks.

//the following is My template column which is extended   
//from the GridTemplateColumn  
 
    public class ComparisonColumn : GridTemplateColumn  
    {  
        private string _VColName;  
        private string _TColName;  
        private string _DColName;  
        private string _HColName;  
        private string _FilterCol;  
        RadComboBox combo;  
 
 
        public ComparisonColumn(string HeaderColName)  
            : base()  
        {  
 
            _HColName = HeaderColName;  
            _VColName = HeaderColName;  
            _TColName = HeaderColName + "_T";  
            _DColName = HeaderColName + "_D";  
            DataField = _VColName;  
        }  
 
         //I added a combobox in the filter control cell. It has 3 items "C", "T"   
        //and "D". Each item represent a sub-column in this column.   
        //So user will select a sub-column to filter on by select a value in  
// this dropdown list and then enter filter value into the filter textbox   
//and do the filtering.  
 
        protected override void SetupFilterControls(TableCell cell)  
        {  
 
            base.SetupFilterControls(cell);  
 
            cell.VerticalAlign = VerticalAlign.Bottom;  
            combo = new RadComboBox();  
            combo.ID = ("RadComboBox1" + this.UniqueName);  
            RadComboBoxItem c = new RadComboBoxItem("C");  
            combo.Items.Add(c);  
            RadComboBoxItem t = new RadComboBoxItem("T");  
            combo.Items.Add(t);  
            RadComboBoxItem d = new RadComboBoxItem("D");  
            combo.Items.Add(d);  
            combo.Width = Unit.Percentage(20);  
 
            TextBox txBox = (TextBox)cell.Controls[0];  
            txBox.TextChanged += this.textBox_TextValueChanged;  
            cell.Controls.AddAt(2, combo);  
            combo.SelectedIndexChanged += this.list_SelectedIndexChanged;  
 
            cell.Wrap = false;  
        }  
 
          
 
 
        private void list_SelectedIndexChanged(object sender, EventArgs e)  
        {  
            RadComboBox o = (RadComboBox)sender;  
            _FilterCol = o.Text;  
            switch (_FilterCol)  
            {  
                case "C": DataField = _VColName; break;  
                case "T": DataField = _TColName; break;  
                case "D": DataField = _DColName; break;  
                default:  
                    DataField = _VColName;  
                    break;  
 
            }  
        }  
 
 
        private void textBox_TextValueChanged(object sender, EventArgs e)  
        {  
 
            GridFilteringItem filterItem = (GridFilteringItem)((TextBox)sender).NamingContainer;  
            _FilterCol = combo.Text;  
            if (_FilterCol == null || _FilterCol == "C" || _FilterCol == "")  
            {  
                DataField = _VColName;  
            }  
            else if (_FilterCol == "T")  
            {  
                DataField = _TColName;  
            }  
            else if (_FilterCol == "D")  
            {  
                DataField = _DColName;  
            }  
        }  
 
   
        //I was hopping to keep the selection in the combo box after postback   
        //(sorting). However, The following attempt does not work  
 
        protected override void SetCurrentFilterValueToControl(TableCell cell)  
        {  
            base.SetCurrentFilterValueToControl(cell);  
            RadComboBox combo = (RadComboBox)cell.Controls[2];  
            if ((_FilterCol != string.Empty))  
            {  
                combo.Text = _FilterCol;  
            }  
        }  
    }  
 
 
//the header template  
    public class ComparisonHeaderTemplate : ITemplate  
    {  
        private string _headerTxt;  
        private string _VColName;  
        private string _TColName;  
        private string _DColName;  
        public ComparisonHeaderTemplate(string headerTxt, string dataFldText)  
        {  
            _headerTxt = headerTxt;  
            _VColName = dataFldText;  
            _TColName = dataFldText + "_T";  
            _DColName = dataFldText + "_D";  
        }  
 
        public void InstantiateIn(System.Web.UI.Control container)  
        {  
            Table table = new Table();  
            TableRow row1 = new TableRow();  
            TableRow row2 = new TableRow();  
            TableCell cell11 = new TableCell();  
            TableCell cell21 = new TableCell();  
            TableCell cell22 = new TableCell();  
            TableCell cell23 = new TableCell();  
            table.Width = Unit.Percentage(100);  
            cell11.ColumnSpan = 3;  
            cell11.Width = Unit.Percentage(100);  
            cell21.Width = Unit.Percentage(33.3);  
            cell22.Width = Unit.Percentage(33.3);  
            cell23.Width = Unit.Percentage(33.4);  
 
            row1.Cells.Add(cell11);  
            row2.Cells.Add(cell21);  
            row2.Cells.Add(cell22);  
            row2.Cells.Add(cell23);  
            table.Rows.Add(row1);  
            table.Rows.Add(row2);  
 
            cell11.Text = _headerTxt;  
            cell21.Text = "Current";  
            cell22.Text = "Threshold";  
            cell23.Text = "Difference";  
            table.HorizontalAlign = HorizontalAlign.Center;  
            table.GridLines = GridLines.None;  
            container.Controls.Add(table);  
            //container.Controls.Add(new LiteralControl("<br />"));  
        }  
 
 
    }  
 
    public class ComparisonItemTemplate : ITemplate  
    {  
        protected LiteralControl lControlV;  
        protected LiteralControl lControlT;  
        protected LiteralControl lControlD;  
        private string colnameV;  
        private string colnameT;  
        private string colnameD;  
 
        public ComparisonItemTemplate(string cName)  
        {  
            colnameV = cName;  
            colnameT = cName + "_T";  
            colnameD = cName + "_D";  
        }  
 
        public void InstantiateIn(System.Web.UI.Control container)  
        {  
            lControlV = new LiteralControl();  
            lControlV.ID = colnameV;  
            lControlV.DataBinding += new EventHandler(lControl_DataBinding);  
            lControlT = new LiteralControl();  
            lControlT.ID = colnameT;  
            lControlT.DataBinding += new EventHandler(lControl_DataBinding);  
            lControlD = new LiteralControl();  
            lControlD.ID = colnameD;  
            lControlD.DataBinding += new EventHandler(lControl_DataBinding);  
            Table table = new Table();  
            TableRow row1 = new TableRow();  
            //TableRow row2 = new TableRow();  
            TableCell cell11 = new TableCell();  
            TableCell cell12 = new TableCell();  
            TableCell cell13 = new TableCell();  
            table.Width = Unit.Percentage(100);  
            table.CellSpacing = 1;  
 
            cell11.Width = Unit.Percentage(33.3);  
            cell12.Width = Unit.Percentage(33.3);  
            cell13.Width = Unit.Percentage(33.4);  
            //TableCell cell22 = new TableCell();  
            cell11.BackColor = Color.BlanchedAlmond;  
            cell12.BackColor = Color.Wheat;  
            cell13.BackColor = Color.LightSalmon;  
            row1.Cells.Add(cell11);  
            row1.Cells.Add(cell12);  
            row1.Cells.Add(cell13);  
            //row2.Cells.Add(cell22);  
            table.Rows.Add(row1);  
            //table.Rows.Add(row2);  
            cell11.Controls.Add(lControlV);  
            cell12.Controls.Add(lControlT);  
            cell13.Controls.Add(lControlD);  
 
            container.Controls.Add(table);  
        }  
 
 
        public void lControl_DataBinding(object sender, EventArgs e)  
        {  
            LiteralControl l = (LiteralControl)sender;  
            GridDataItem container = (GridDataItem)l.NamingContainer;  
            String colName = l.ID;  
            l.Text = ((DataRowView)container.DataItem)[colName].ToString() + "<br />";  
        }  
    }  
 
 

3 Answers, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 07 Jan 2010, 09:42 AM
Hello Oliver,

In order to achieve your goal you can define your own filter template. It should inherit from ITemplate and you can add the desired controls there. Then you need to assign the GridTemplateColumn FilterTemplate property accordingly.
Additionally, for the filter button you can use a button with CssClass set to "rgFilter" and to make it show the grid filtering menu when clicked, try handling its OnClientClick event as below:

filterBtn.OnClientClick = "$find(\"<RadGrid.ClientID>\")._showFilterMenu(\"<RadGrid.ClientID>\", \"<DataField>\", event); return false;";

For more information on RadGrid FilterTemplates, please review this demo.

Let me know if any issues arise.

Regards,
Iana
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Oliver
Top achievements
Rank 1
answered on 08 Jan 2010, 07:29 PM
 I did what you said. Instead of overridding the 

SetupFilterControls(TableCell cell)

 

 

 

function of MyTemplateColumn, I created MyTempalteFilter to add my desired filter controls. I added 3 filter textBox one for each subcolumn and 3 filter buttons, one for each sub-column. I would like the FilterTextBox and the FilterButton work together and behave just like the default for BoundColumns. Here I am having the following questions?

How to add the filtering functionality to the FilterTextBoxes so when user hits "Enter", it filters the data? 

The OnClientClick code that you gave out here for the filterButton is not quite right. The correct one is the following. 

filterBtn.OnClientClick = "$find(\"<RadGrid.ClientID>\")._showFilterMenu(\"<RadGrid.MasterTableView.ClientID>\", \"<DataField>\", event); return false;";


Thanks.

0
Iana Tsolova
Telerik team
answered on 11 Jan 2010, 11:03 AM
Hi Oliver,

Please find the answer to your question in the other support thread you have open on the same subject.

Sincerely yours,
Iana
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
Oliver
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
Oliver
Top achievements
Rank 1
Share this question
or