Removing Whitespace from Bound Columns

5 posts, 0 answers
  1. Matthew
    Matthew avatar
    8 posts
    Member since:
    Sep 2017

    Posted 12 Sep 2018 Link to this post

    I have a RadGridView that I populate from a DataTable that holds an SQL result. I am attempting to remove whitespace within a column.

    This is the result of the way the Data is held in the Database. As result group columns divide groups that should be contained.

    So group by "Column A" would result in "TypeA" and "TypeA   ".

    What is the best way to trim this whitespace within the GridView so results would return only "TypeA"?

  2. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3626 posts

    Posted 13 Sep 2018 Link to this post

    Hello, Matthew,           

    In order to achieve your goal, I would recommend you to iterate all rows and for each row iterate all cells and trim the value that is stored in the cells. Here is demonstrated a sample code snippet: 

    public RadForm1()
    {
        InitializeComponent();
     
        DataTable dt = new DataTable();
        dt.Columns.Add("Column A", typeof(string));
        dt.Columns.Add("Column B", typeof(string));
        dt.Rows.Add("TypeA", "TypeB");
        dt.Rows.Add("TypeA ", "TypeB ");
     
        this.radGridView1.DataSource = dt;
        this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
     
        this.radGridView1.BeginUpdate();
        for (int i = 0; i < this.radGridView1.Rows.Count; i++)
        {
            for (int j = 0; j < this.radGridView1.Columns.Count; j++)
            {
                if (this.radGridView1.Columns[j].DataType!= typeof(string))
                {
                    continue;
                }
                this.radGridView1.Rows[i].Cells[j].Value = (this.radGridView1.Rows[i].Cells[j].Value + "").Trim();
            }
        }
        this.radGridView1.EndUpdate();
    }

    Before:



    After: 


    I hope this information helps. If you need any further assistance please don't hesitate to contact me.

    Regards,
    Dess
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  3. Matthew
    Matthew avatar
    8 posts
    Member since:
    Sep 2017

    Posted 13 Sep 2018 in reply to Dess | Tech Support Engineer, Sr. Link to this post

    Perfect Dess, this did exactly what was wanted when specifying a single column.

    Would the same method work to eliminating duplicates? So given the grid,

    1. Column A
      1. A
      2. 2

       

    TypeB

     

  4. Matthew
    Matthew avatar
    8 posts
    Member since:
    Sep 2017

    Posted 13 Sep 2018 in reply to Matthew Link to this post

    Perfect Dess, this did exactly what was wanted when specifying a single column.
    Would the same method work to eliminating duplicates? So given the grid below and wanting to remove duplicate rows on Column B,
    Column A with row cells 1, 2, 3
    Column B with row cells A.com, B.com, B.com

    After removing duplicates on Column B, only Column A with row cells 1 and Column B with row cells "A.com" would remain.

  5. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3626 posts

    Posted 17 Sep 2018 Link to this post

    Hello, Matthew,

    Eliminating the duplicate entries can't be achieved automatically. For this purpose, you need to iterate all records in the original DataTable and fill a new DataTable with the unique records so no duplicates will be available. Then, this DataTable can be set as DataSource.

    Should you have further questions please let me know.

    Regards,
    Dess
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top