markzzz
markzzz

Reputation: 47945

Can I trim strings when I manage them to a DropDownList by DataSource?

I have this code :

myObjects ps = new myObjects();

myDD.DataSource = ps;
myDD.DataTextField = "Title";
myDD.DataValueField = "ItemID";
myDD.DataBind();

that add a Text/Value pair values to a DropDownList.

I'd like to add these valus trimming it (so remove empty space first and in the end).

Is it possible on #C/.NET?

Upvotes: 0

Views: 1525

Answers (1)

Bala R
Bala R

Reputation: 108957

myDD.DataSource = ps.Cast<YourItemType>().Select(i => new { 
                                                   Title = i.Title.Trim(),
                                                   ItemID = i.ItemID.Trim()});

myDD.DataTextField = "Title";
myDD.DataValueField = "ItemID";
myDD.DataBind();

if ps is a DataTable, you should be able to do

myDD.DataSource = ps.Cast<DataRow>().Select(i => new { 
                                                       Title = i["Title"].Trim(),
                                                       ItemID = i["ItemID"].Trim()});
myDD.DataTextField = "Title";
myDD.DataValueField = "ItemID";
myDD.DataBind();

Upvotes: 5

Related Questions