RadGrid columns contain wrong data

2 posts, 0 answers
  1. Alex
    Alex avatar
    23 posts
    Member since:
    Dec 2012

    Posted 19 Mar 2013 Link to this post

    I am using an example provided in this post http://www.telerik.com/community/forums/aspnet-ajax/grid/how-to-use-gridtemplatecolumn-in-radgrid-for-google-like-filtering.aspx which was attached as a zip file called "templatecolumnwithfiltering2.zip". I've re-created it using our SQL Server table data, however, I added some more columns and what I am getting is concatenated data of all the columns in each column. I have added a screenshot of what I am getting. Each column should only hold one type of data. The columns should be "Client Name", "Billing Name" and "Client Number", respectively, but each column has all the data jumbled up in each column instead of in separate columns.
    Here is my code:
    <asp:ScriptManager ID="scriptManager" runat="server">
            </asp:ScriptManager>
            <asp:PlaceHolder ID="holder" Visible="true" runat="server"></asp:PlaceHolder>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                SelectCommand="SELECT * FROM [table]"></asp:SqlDataSource>
            <telerik:RadAjaxPanel ID="ajaxPanel" EnableAJAX="false" runat="server">
     
            </telerik:RadAjaxPanel>


    protected void Page_Init(object sender, EventArgs e)
           {
               RadGrid grid = new RadGrid();
     
               grid.ID = "RadGrid1";
               grid.MasterTableView.DataSourceID = "SqlDataSource1";
               grid.MasterTableView.DataKeyNames = new string[] { "Record_ID" };
               grid.Skin = "Default";
               grid.PageSize = 10;
               grid.AllowPaging = true;
               grid.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
               grid.AutoGenerateColumns = false;
               grid.AllowFilteringByColumn = true;
     
     
     
               GridBoundColumn boundColumn = new GridBoundColumn();
               grid.MasterTableView.Columns.Add(boundColumn);
               boundColumn.DataField = "Record_ID";
               boundColumn.HeaderText = "Record_ID ID";
               boundColumn.UniqueName = "Record_ID";
     
     
               NewTemplateColumn templateColumn = new NewTemplateColumn();
               templateColumn.ItemTemplate = new TemplateColumn();
               grid.MasterTableView.Columns.Add(templateColumn);
               templateColumn.DataField = "Client_Name";
               templateColumn.HeaderText = "Client Name";
               templateColumn.UniqueName = "Client_Name";
     
               NewTemplateColumn templateColumn1 = new NewTemplateColumn();
               templateColumn1.ItemTemplate = new TemplateColumn();
               grid.MasterTableView.Columns.Add(templateColumn1);
               templateColumn1.DataField = "Billing_Atty";
               templateColumn1.HeaderText = "Billing Attorney";
               templateColumn1.UniqueName = "Billing_Atty";
     
               NewTemplateColumn templateColumn2 = new NewTemplateColumn();
               templateColumn2.ItemTemplate = new TemplateColumn();
               grid.MasterTableView.Columns.Add(templateColumn2);
               templateColumn2.DataField = "Client_Number";
               templateColumn2.HeaderText = "Client Number";
               templateColumn2.UniqueName = "Client_Number";
     
               ajaxPanel.Controls.Add(grid);
     
               grid.DataBind();
     
           }
     
     
     
       }
     
       public partial class NewTemplateColumn : GridTemplateColumn
       {
           private SqlDataSource comboDataSource = new SqlDataSource(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString(), "select Client_Name FROM Matter_Subs");
           //RadGrid will call this method when it initializes the controls inside the filtering item cells
           protected override void SetupFilterControls(TableCell cell)
           {
               base.SetupFilterControls(cell);
               cell.Controls.RemoveAt(0);
               RadComboBox combo = new RadComboBox();
               combo.ID = ("RadComboBox1" + this.UniqueName);
               combo.ShowToggleImage = false;
               //combo.Skin = "Office2007";
               combo.EnableLoadOnDemand = true;
               combo.AutoPostBack = true;
               combo.MarkFirstMatch = true;
               combo.Height = Unit.Pixel(100);
               combo.ItemsRequested += this.list_ItemsRequested;
               combo.SelectedIndexChanged += this.list_SelectedIndexChanged;
               cell.Controls.AddAt(0, combo);
               cell.Controls.RemoveAt(1);
               RadComboBox combo1 = new RadComboBox();
               combo1.ID = ("RadComboBox2" + this.UniqueName);
               combo1.ShowToggleImage = false;
               //combo.Skin = "Office2007";
               combo1.EnableLoadOnDemand = true;
               combo1.AutoPostBack = true;
               combo1.MarkFirstMatch = true;
               combo1.Height = Unit.Pixel(100);
               combo1.ItemsRequested += this.list1_ItemsRequested;
               combo1.SelectedIndexChanged += this.list1_SelectedIndexChanged;
               cell.Controls.AddAt(0, combo1);
               cell.Controls.RemoveAt(1);
               RadComboBox combo2 = new RadComboBox();
               combo2.ID = ("RadComboBox3" + this.UniqueName);
               combo2.ShowToggleImage = false;
               //combo.Skin = "Office2007";
               combo2.EnableLoadOnDemand = true;
               combo2.AutoPostBack = true;
               combo2.MarkFirstMatch = true;
               combo2.Height = Unit.Pixel(100);
               combo2.ItemsRequested += this.list2_ItemsRequested;
               combo2.SelectedIndexChanged += this.list1_SelectedIndexChanged;
               cell.Controls.AddAt(0, combo2);
               cell.Controls.RemoveAt(1);
           }
     
           //RadGrid will cal this method when the value should be set to the filtering input control(s)
           protected override void SetCurrentFilterValueToControl(TableCell cell)
           {
               base.SetCurrentFilterValueToControl(cell);
               RadComboBox combo = (RadComboBox)cell.Controls[0];
               if ((this.CurrentFilterValue != string.Empty))
               {
                   combo.Text = this.CurrentFilterValue;
               }
               RadComboBox combo1 = (RadComboBox)cell.Controls[0];
               if ((this.CurrentFilterValue != string.Empty))
               {
                   combo1.Text = this.CurrentFilterValue;
               }
               RadComboBox combo2 = (RadComboBox)cell.Controls[0];
               if ((this.CurrentFilterValue != string.Empty))
               {
                   combo2.Text = this.CurrentFilterValue;
               }
           }
     
           //RadGrid will cal this method when the filtering value should be extracted from the filtering input control(s)
           protected override string GetCurrentFilterValueFromControl(TableCell cell)
           {
               RadComboBox combo = (RadComboBox)cell.Controls[0];
               return combo.Text;
           }
           private void list_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
           {
               ((RadComboBox)o).DataValueField = "Client_Name";
               ((RadComboBox)o).DataTextField = "Client_Name";
               comboDataSource.SelectCommand = "SELECT DISTINCT Client_Name FROM table WHERE Client_Name LIKE '" + e.Text + "%'";
               ((RadComboBox)o).DataSource = comboDataSource.Select(new DataSourceSelectArguments());
               ((RadComboBox)o).DataBind();
           }
     
           private void list_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
           {
               GridFilteringItem filterItem = (GridFilteringItem)((RadComboBox)o).NamingContainer;
               if ((this.UniqueName == "Client_Name"))
               {
                   //this is filtering for integer column type
                   filterItem.FireCommandEvent("Filter", new Pair("EqualTo", this.UniqueName));
               }
               //filtering for string column type
               filterItem.FireCommandEvent("Filter", new Pair("Contains", this.UniqueName));
           }
     
           private void list1_ItemsRequested(object o1, RadComboBoxItemsRequestedEventArgs e1)
           {
               ((RadComboBox)o1).DataValueField = "Billing_Atty";
               ((RadComboBox)o1).DataTextField = "Billing_Atty";
               comboDataSource.SelectCommand = "SELECT DISTINCT Billing_Atty FROM table WHERE Billing_Atty LIKE '" + e1.Text + "%'";
               ((RadComboBox)o1).DataSource = comboDataSource.Select(new DataSourceSelectArguments());
               ((RadComboBox)o1).DataBind();
           }
     
           private void list1_SelectedIndexChanged(object o1, RadComboBoxSelectedIndexChangedEventArgs e1)
           {
               GridFilteringItem filterItem = (GridFilteringItem)((RadComboBox)o1).NamingContainer;
               if ((this.UniqueName == "Billing_Atty"))
               {
                   //this is filtering for integer column type
                   filterItem.FireCommandEvent("Filter", new Pair("EqualTo", this.UniqueName));
               }
               //filtering for string column type
               filterItem.FireCommandEvent("Filter", new Pair("Contains", this.UniqueName));
           }
     
           private void list2_ItemsRequested(object o2, RadComboBoxItemsRequestedEventArgs e2)
           {
               ((RadComboBox)o2).DataValueField = "Client_Number";
               ((RadComboBox)o2).DataTextField = "Client_Number";
               comboDataSource.SelectCommand = "SELECT DISTINCT Client_Number FROM table WHERE Client_Number LIKE '" + e2.Text + "%'";
               ((RadComboBox)o2).DataSource = comboDataSource.Select(new DataSourceSelectArguments());
               ((RadComboBox)o2).DataBind();
           }
     
           private void list2_SelectedIndexChanged(object o2, RadComboBoxSelectedIndexChangedEventArgs e2)
           {
               GridFilteringItem filterItem = (GridFilteringItem)((RadComboBox)o2).NamingContainer;
               if ((this.UniqueName == "Client_Number"))
               {
                   //this is filtering for integer column type
                   filterItem.FireCommandEvent("Filter", new Pair("EqualTo", this.UniqueName));
               }
               //filtering for string column type
               filterItem.FireCommandEvent("Filter", new Pair("Contains", this.UniqueName));
           }
     
       }
     
       public partial class TemplateColumn : ITemplate
       {
     
           public void InstantiateIn(Control container)
           {
     
               LiteralControl literalControl = new LiteralControl();
               literalControl.ID = "literalControl";
               literalControl.DataBinding += new EventHandler(literalControl_DataBinding);
     
               LiteralControl literalControl1 = new LiteralControl();
               literalControl1.ID = "literalControl1";
               literalControl1.DataBinding += new EventHandler(literalControl1_DataBinding);
     
               LiteralControl literalControl2 = new LiteralControl();
               literalControl2.ID = "literalControl2";
               literalControl2.DataBinding += new EventHandler(literalControl2_DataBinding);
     
               CheckBox box = new CheckBox();
     
               box.ID = "boxControl";
     
               box.DataBinding += new EventHandler(box_DataBinding);
     
               container.Controls.Add(literalControl);
               container.Controls.Add(literalControl1);
               container.Controls.Add(literalControl2);
               container.Controls.Add(box);
     
           }
     
           void box_DataBinding(object sender, EventArgs e)
           {
               CheckBox box = (CheckBox)sender;
     
               GridDataItem container = box.NamingContainer as GridDataItem;
     
               if (container != null)
                   box.Checked = (bool)((DataRowView)container.DataItem)["Flag"];
               else
               {
                   GridEditFormItem editFormContainer = box.NamingContainer as GridEditFormItem;
                   box.Enabled = (bool)((DataRowView)editFormContainer.DataItem)["Flag"];
     
               }
           }
     
           protected void literalControl_DataBinding(object sender, EventArgs e)
           {
               LiteralControl l = (LiteralControl)sender;
     
               GridDataItem container = l.NamingContainer as GridDataItem;
     
               if (container != null)
               {
                   l.Text = ((DataRowView)container.DataItem)["Client_Name"].ToString();
     
               }
               else
               {
                   GridEditFormItem editFormContainer = l.NamingContainer as GridEditFormItem;
                   l.Text = ((DataRowView)editFormContainer.DataItem)["Client_Name"].ToString();
               }
           }
     
           protected void literalControl1_DataBinding(object sender, EventArgs e)
           {
               LiteralControl lc1 = (LiteralControl)sender;
     
               GridDataItem container1 = lc1.NamingContainer as GridDataItem;
     
               if (container1 != null)
               {
                   lc1.Text = ((DataRowView)container1.DataItem)["Billing_Atty"].ToString();
     
               }
               else
               {
                   GridEditFormItem editFormContainer = lc1.NamingContainer as GridEditFormItem;
                   lc1.Text = ((DataRowView)editFormContainer.DataItem)["Billing_Atty"].ToString();
               }
           }
     
           protected void literalControl2_DataBinding(object sender, EventArgs e)
           {
               LiteralControl lc2 = (LiteralControl)sender;
     
               GridDataItem container2 = lc2.NamingContainer as GridDataItem;
     
               if (container2 != null)
               {
                   lc2.Text = ((DataRowView)container2.DataItem)["Client_Number"].ToString();
     
               }
               else
               {
                   GridEditFormItem editFormContainer = lc2.NamingContainer as GridEditFormItem;
                   lc2.Text = ((DataRowView)editFormContainer.DataItem)["Client_Number"].ToString();
               }
           }


  2. Radoslav
    Admin
    Radoslav avatar
    1564 posts

    Posted 22 Mar 2013 Link to this post

    Hi Alex,

    I reviewed the code snippet which you post and I saw that you add three literals bound to three different fields into each template column:
    public void InstantiateIn(Control container)
           {
      
               LiteralControl literalControl = new LiteralControl();
               literalControl.ID = "literalControl";
               literalControl.DataBinding += new EventHandler(literalControl_DataBinding);
      
               LiteralControl literalControl1 = new LiteralControl();
               literalControl1.ID = "literalControl1";
               literalControl1.DataBinding += new EventHandler(literalControl1_DataBinding);
      
               LiteralControl literalControl2 = new LiteralControl();
               literalControl2.ID = "literalControl2";
               literalControl2.DataBinding += new EventHandler(literalControl2_DataBinding);
      
               CheckBox box = new CheckBox();
      
               box.ID = "boxControl";
      
               box.DataBinding += new EventHandler(box_DataBinding);
      
               container.Controls.Add(literalControl);
               container.Controls.Add(literalControl1);
               container.Controls.Add(literalControl2);
               container.Controls.Add(box);
      
           }
    To achieve the desired functionality you need to add only one literal into your column which is bound to a different field. For example:
    public partial class TemplateColumn : ITemplate
       {
           string fieldName = "";
     
        public TemplateColumn(string fieldName)
        {
            this.fieldName = fieldName;
        }
     
           public void InstantiateIn(Control container)
           {
               LiteralControl literalControl = new LiteralControl();
               if (fieldName == "Client_Name")
               {
                   literalControl.ID = "literalControl";
                   literalControl.DataBinding += new EventHandler(literalControl_DataBinding);
               }
               else if(fieldName == "Billing_Atty")
               {
                    literalControl.ID = "literalControl1";
                    literalControl.DataBinding += new EventHandler(literalControl1_DataBinding);
               }
               else if(fieldName == "Client_Number")
               {
                    literalControl.ID = "literalControl2";
                    literalControl.DataBinding += new EventHandler(literalControl2_DataBinding);
               }
     
               CheckBox box = new CheckBox();
      
               box.ID = "boxControl";
      
               box.DataBinding += new EventHandler(box_DataBinding);
      
               container.Controls.Add(literalControl);
               container.Controls.Add(box);
           }
    Then when you create the TempalteColumn template you can pass the fieldname:
    NewTemplateColumn templateColumn = new NewTemplateColumn();
               templateColumn.ItemTemplate = new TemplateColumn("Client_Name");
               grid.MasterTableView.Columns.Add(templateColumn);
               templateColumn.DataField = "Client_Name";
               templateColumn.HeaderText = "Client Name";
               templateColumn.UniqueName = "Client_Name";
      
               NewTemplateColumn templateColumn1 = new NewTemplateColumn();
               templateColumn1.ItemTemplate = new TemplateColumn("Billing_Atty");
               grid.MasterTableView.Columns.Add(templateColumn1);
               templateColumn1.DataField = "Billing_Atty";
               templateColumn1.HeaderText = "Billing Attorney";
               templateColumn1.UniqueName = "Billing_Atty";
      
               NewTemplateColumn templateColumn2 = new NewTemplateColumn();
               templateColumn2.ItemTemplate = new TemplateColumn("Client_Number");
               grid.MasterTableView.Columns.Add(templateColumn2);
               templateColumn2.DataField = "Client_Number";
               templateColumn2.HeaderText = "Client Number";
               templateColumn2.UniqueName = "Client_Number";

    Please give it try and let me know if it helps you.

    Kind regards,
    Radoslav
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top