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

Removing Whitespace from Bound Columns

4 Answers 499 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Matthew
Top achievements
Rank 1
Matthew asked on 12 Sep 2018, 03:23 PM

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"?

4 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 13 Sep 2018, 06:13 AM
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.
0
Matthew
Top achievements
Rank 1
answered on 13 Sep 2018, 02:03 PM

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

 

0
Matthew
Top achievements
Rank 1
answered on 13 Sep 2018, 02:09 PM

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.

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 17 Sep 2018, 10:00 AM
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.
Tags
GridView
Asked by
Matthew
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Matthew
Top achievements
Rank 1
Share this question
or