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

In our application Facing issue(creatin dynamic column with radcomboboxes in rad grid )

7 Answers 53 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Naga
Top achievements
Rank 1
Naga asked on 25 Sep 2013, 05:36 AM
Iam Facing issue(creating dynamic column with filtertemplate in radcomboboxes in rad grid )
  
We are using radgrid.We are generating dynamic columns with combobox in filter template .We are generating Column dynamically.
  
Issue is Combobox selected changed event is not woring. 
  
i am doing this programmatically using ITEMPLATE interface by inheriting in a class and calling InstantiateIn method using Creating combox control.

7 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 1
answered on 25 Sep 2013, 11:15 AM
Hi Naga,

I guess you are trying to create a filter template with RadComboBox.Please try the following code snippet.

ASPX:
<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
</telerik:RadScriptManager>
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="PlaceHolder1">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="PlaceHolder1" />
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

C#:
DataTable dt;
   RadGrid RadGrid1;
   private void Page_Init(object sender, System.EventArgs e)
   {
       RadGrid1 = new RadGrid();
       RadGrid1.ID = "RadGrid1";
       RadGrid1.AutoGenerateColumns = false;
       RadGrid1.AllowPaging = true;
       RadGrid1.AllowSorting = true;
       RadGrid1.AllowFilteringByColumn = true;
       RadGrid1.EnableLinqExpressions = false;
       RadGrid1.PagerStyle.AlwaysVisible = true;
       RadGrid1.NeedDataSource += new GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);         
 
       dt = GetDataTable("SELECT Country FROM Customers");
       string ColumnName = "Country";
       GridBoundColumn templateColumn;
       templateColumn = new GridBoundColumn();
       templateColumn.FilterTemplate = new MyTemplate(ColumnName,RadGrid1);
       templateColumn.DataField = ColumnName;
       templateColumn.HeaderText = ColumnName;
       templateColumn.UniqueName = ColumnName;       
       this.RadGrid1.MasterTableView.Columns.Add(templateColumn);       
 
       GridBoundColumn boundColumn;
       boundColumn = new GridBoundColumn();
       RadGrid1.MasterTableView.Columns.Add(boundColumn);
       boundColumn.DataField = "CompanyName";
       boundColumn.HeaderText = "CompanyName";
       boundColumn = new GridBoundColumn();
       RadGrid1.MasterTableView.Columns.Add(boundColumn);
       boundColumn.DataField = "ContactName";
       boundColumn.HeaderText = "Contact Name";
       PlaceHolder1.Controls.Add(RadGrid1);
   }   
  
   private class MyTemplate : ITemplate
   {
       DataTable dt;
       protected RadComboBox combo;
       private string colname;
       RadGrid radgrid;
       public MyTemplate(string cName,RadGrid grid)
       {
           colname = cName;
           radgrid = grid;
       }
       public void InstantiateIn(System.Web.UI.Control container)
       {
           combo = new RadComboBox();
           combo.ID = "lControl";
           combo.DataBinding += new EventHandler(lControl_DataBinding);
           container.Controls.Add(combo);
           combo.AutoPostBack = true;        
           combo.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(l_SelectedIndexChanged);
       }
 
       public void lControl_DataBinding(object sender, EventArgs e)
       {
           RadComboBox l = (RadComboBox)sender;
           GridFilteringItem container = (GridFilteringItem)l.NamingContainer;          
           l.DataTextField = colname;
           l.DataValueField = colname;
           dt = GetDataTable("SELECT distinct Country FROM Customers");        
           l.DataSource = dt;          
       }
       
       void l_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
       {
           RadComboBox combo = sender as RadComboBox;
           string filterExpression;
           filterExpression = "([Country] LIKE '" + e.Value + "')";        
           radgrid.MasterTableView.FilterExpression = filterExpression;
           radgrid.MasterTableView.Rebind(); 
       }
   }
   
   protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
   {
       dt = GetDataTable("SELECT CompanyName, ContactName, Country FROM Customers");
       this.RadGrid1.DataSource = dt;
   }
  
   public static DataTable GetDataTable(string query)
   {
       string ConnString = ConfigurationManager.ConnectionStrings["Northwind_newConnectionString3"].ConnectionString;
       SqlConnection conn = new SqlConnection(ConnString);
       SqlDataAdapter adapter = new SqlDataAdapter();
       adapter.SelectCommand = new SqlCommand(query, conn);
 
       DataTable myDataTable = new DataTable();
 
       conn.Open();
       try
       {
           adapter.Fill(myDataTable);
       }
       finally
       {
           conn.Close();
       }
       return myDataTable;
   }

Thanks,
Princy
0
Naga
Top achievements
Rank 1
answered on 25 Sep 2013, 11:22 AM
I already tried this sample,but not working.
  
In my scenario,Radgrid i added in design,I need to create column with filetr template with combobox.Butselected changed event is not firing,RadComboItemDataBound event is firing.
0
Naga
Top achievements
Rank 1
answered on 25 Sep 2013, 11:49 AM
This below code i used to creating  custom column
  
 CustomFilteringColumn boundColumn = new CustomFilteringColumn();
  
                            boundColumn.DataField = dtpeople.Columns[i].ColumnName;
                            boundColumn.HeaderText = dtpeople.Columns[i].Caption;
                            boundColumn.UniqueName = dtpeople.Columns[i].ColumnName;
                            radGridViewUsers.MasterTableView.Columns.Add(boundColumn);
  
  
  
 public class CustomFilteringColumn1 : GridBoundColumn
    {
        protected override void SetupFilterControls(TableCell cell)
        {
  
            base.SetupFilterControls(cell);
            cell.Controls.RemoveAt(0);
  
            RadComboBox RadCombo = new Telerik.Web.UI.RadComboBox();
            RadCombo.ID = this.DataField;
            RadCombo.AppendDataBoundItems = true;
            RadCombo.AllowCustomText = true;
            RadCombo.CheckBoxes = true;
            //RadCombo.AutoPostBack = true;
            RadCombo.EnableCheckAllItemsCheckBox = true;
            RadCombo.CheckedItemsTexts.Equals("DisplayAllInInput");
            RadCombo.MarkFirstMatch = true;
            RadCombo.HighlightTemplatedItems = true;
            RadCombo.EmptyMessage = "All";
            RadCombo.NoWrap = false;
            RadCombo.CssClass = "target-users-popup-dropdown";
            RadCombo.DropDownCssClass = "target-users-popup-dropdown-1";
            RadCombo.Localization.AllItemsCheckedString = "All";
            RadCombo.Localization.CheckAllString = "All";
            RadCombo.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(RadCombo_SelectedIndexChanged);
            RadCombo.PreRender += new EventHandler(RadCombo_PreRender);
            RadCombo.ItemDataBound += new RadComboBoxItemEventHandler(RadCombo_ItemDataBound);
            RadCombo.DataValueField = this.DataField;
            RadCombo.DataTextField = this.DataField;
  
            cell.Controls.AddAt(0, RadCombo);
            cell.Controls.RemoveAt(1);
          
           
             
        }
  
        void RadCombo_ItemDataBound(object sender, RadComboBoxItemEventArgs e)
        {
            if (e.Item.Text == "")
            {
                e.Item.Text = "<<Empty>>";
  
            }
        }
  
        void RadCombo_PreRender(object sender, EventArgs e)
        {
          //  throw new NotImplementedException();
        }
  
        void RadCombo_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
             
           // throw new NotImplementedException();
            ((GridFilteringItem)(((RadComboBox)sender).Parent.Parent)).FireCommandEvent("Filter", new Pair());
        }
  
    }
0
Naga
Top achievements
Rank 1
answered on 25 Sep 2013, 11:51 AM
This below code is used to creating columns,but combobox selected changed event not firing
  
CustomFilteringColumn boundColumn = new CustomFilteringColumn();
  
                            boundColumn.DataField = dtpeople.Columns[i].ColumnName;
                            boundColumn.HeaderText = dtpeople.Columns[i].Caption;
                            boundColumn.UniqueName = dtpeople.Columns[i].ColumnName;
                            radGridViewUsers.MasterTableView.Columns.Add(boundColumn);
  
  
  
  
 public class CustomFilteringColumn1 : GridBoundColumn
    {
        protected override void SetupFilterControls(TableCell cell)
        {
  
            base.SetupFilterControls(cell);
            cell.Controls.RemoveAt(0);
  
            RadComboBox RadCombo = new Telerik.Web.UI.RadComboBox();
            RadCombo.ID = this.DataField;
            RadCombo.AppendDataBoundItems = true;
            RadCombo.AllowCustomText = true;
            RadCombo.CheckBoxes = true;
            //RadCombo.AutoPostBack = true;
            RadCombo.EnableCheckAllItemsCheckBox = true;
            RadCombo.CheckedItemsTexts.Equals("DisplayAllInInput");
            RadCombo.MarkFirstMatch = true;
            RadCombo.HighlightTemplatedItems = true;
            RadCombo.EmptyMessage = "All";
            RadCombo.NoWrap = false;
            RadCombo.CssClass = "target-users-popup-dropdown";
            RadCombo.DropDownCssClass = "target-users-popup-dropdown-1";
            RadCombo.Localization.AllItemsCheckedString = "All";
            RadCombo.Localization.CheckAllString = "All";
            RadCombo.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(RadCombo_SelectedIndexChanged);
            RadCombo.PreRender += new EventHandler(RadCombo_PreRender);
            RadCombo.ItemDataBound += new RadComboBoxItemEventHandler(RadCombo_ItemDataBound);
            RadCombo.DataValueField = this.DataField;
            RadCombo.DataTextField = this.DataField;
  
            cell.Controls.AddAt(0, RadCombo);
            cell.Controls.RemoveAt(1);
          
           
             
        }
  
        void RadCombo_ItemDataBound(object sender, RadComboBoxItemEventArgs e)
        {
            if (e.Item.Text == "")
            {
                e.Item.Text = "<<Empty>>";
  
            }
        }
  
        void RadCombo_PreRender(object sender, EventArgs e)
        {
          //  throw new NotImplementedException();
        }
  
        void RadCombo_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
             
           // throw new NotImplementedException();
            ((GridFilteringItem)(((RadComboBox)sender).Parent.Parent)).FireCommandEvent("Filter", new Pair());
        }
  
    }
0
Naga
Top achievements
Rank 1
answered on 26 Sep 2013, 02:19 AM
Thanks a lot,this reply very much helpful to me.If i creating radgrid dynamically its working fine.But I want to create dynamically column only.

My problem is filtering time automatically columns creating.





0
Princy
Top achievements
Rank 1
answered on 26 Sep 2013, 05:44 AM
Hi Naga,

When creating template columns programmatically, the grid must be generated completely in the code-behind using the Page_Init event.
Since you are creating only the column dynamically,you are facing the issue of columns being created again.So please create your grid in the code behind.

C#:
RadGrid RadGrid1;
 
private void Page_Init(object sender, System.EventArgs e)
{
    RadGrid1 = new RadGrid();
    RadGrid1.ID = "RadGrid1";
    RadGrid1.AutoGenerateColumns = false;
    RadGrid1.AllowPaging = true;
    RadGrid1.AllowSorting = true;
    RadGrid1.AllowFilteringByColumn = true;
    RadGrid1.EnableLinqExpressions = false;
    RadGrid1.PagerStyle.AlwaysVisible = true;
    RadGrid1.NeedDataSource += new GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);
 
    string ColumnName = "Country";
    GridBoundColumn templateColumn;
    templateColumn = new GridBoundColumn();
    templateColumn.FilterTemplate = new MyTemplate(ColumnName, RadGrid1);
    RadGrid1.MasterTableView.Columns.Add(templateColumn);
    templateColumn.DataField = ColumnName;
    templateColumn.HeaderText = ColumnName;
    templateColumn.UniqueName = ColumnName;      
 
    GridBoundColumn boundColumn;
    boundColumn = new GridBoundColumn();
    RadGrid1.MasterTableView.Columns.Add(boundColumn);
    boundColumn.DataField = "CompanyName";
    boundColumn.HeaderText = "CompanyName";      
 
    boundColumn = new GridBoundColumn();
    RadGrid1.MasterTableView.Columns.Add(boundColumn);
    boundColumn.DataField = "ContactName";
    boundColumn.HeaderText = "Contact Name";
    PlaceHolder1.Controls.Add(RadGrid1);      
}

Thanks,
Princy
0
Naga
Top achievements
Rank 1
answered on 26 Sep 2013, 05:59 AM
I tried same.Its working fine.Thank you for giving reply.
Tags
Grid
Asked by
Naga
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Naga
Top achievements
Rank 1
Share this question
or