Convert string to HorizontalAlign value

J

Jeff User

Hi all

vs2003, .NET1.1

I would like to store alignment values in SQL Server db and then use
them in C# to set values of cells in tables. Problem is that a string
variable will not work. Is there a way to set Alignment values from a
string variable?

string sValue = "Center"; //Comes from database in real application

//Find a particular cell in a table
System.Web.UI.WebControls.TableCell c =
(TableCell)Page.FindControl(sHeadCell);

//This line wont compile
cl.HorizontalAlign = sValue;

//This is what it is looking for:
cl.HorizontalAlign =HorizontalAlign.Center

//This has run time error that it cant convert:
cl.HorizontalAlign = (HorizontalAlign)sValue;

Any idea how to get :
"cl.HorizontalAlign = sValue;"
to work?

Thanks
Jeff
 
M

Marc Gravell

How about:

cl.HorizonatalAlign = Enum.Parse(typeof(HorizontalAlign), sValue,
true);

Mrac
 
J

Jeff User

I had to add :
(HorizontalAlign)
to avoid another cannot implicitly convert to type HorizontalAlign
error

cl.HorizonatalAlign =
(HorizontalAlign)Enum.Parse(typeof(HorizontalAlign), sValue,
true);

Works very well
Thanks

jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top