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
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
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,
- Column A
- A
- 2
TypeB
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.
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