views:

60

answers:

4

I've been searching the internet with the string.format for my code and it seems that I can't find the right one that looks like on my code.

DataColumn dtCol;
dtCol = new DataColumn("ImagePath", System.Type.GetType("System.String"));
dtImages.Columns.Add(dtCol);
dtImages.Columns["ImagePath"].Expression = string.Format("<a href=\"'{0}'+ImageFilename\">View Image</a>", ImageDownloadPath);

(the ImageFilename is a column on my database table) the above code always throws an error of "Syntax error: Missing operand before '<' operator"

How do I do this properly?

+1  A: 

Perhaps try the following?

string.Format("<a href=\"{0}\\{1}\">View Image</a>", ImageDownloadPath, ImageFilename);

In your code you are using ''s around the string format identifier, which would have then shown up in your formatted string, and the ImageFilename property was not being used correctly. It would have simply been added as plain text.

The result of your string with the following values would be as such:

ImageFilename = "1.jpg";
ImageDownloadPath = "http://www.downloadme.com/images";

Yours: <a href="'http://www.downloadme.com/images'+ImageFilename"&gt;View Image</a>
Mine: <a href="http://www.downloadme.com/images/1.jpg"&gt;View Image</a>
Kyle Rozendo
A: 

Try this:

dtImages.Columns["ImagePath"].Expression = 
string.Format("&lt;a href=\"'{0}'+ImageFilename\"&gt;View Image&lt;/a&gt;", ImageDownloadPath); 

Im pretty sure the < and > are trying to be interpreted as part of the expression.

boomhauer
+1  A: 

It's not at all clear from the question, but I believe the problem isn't a compile-time one at all... not indeed one with string.Format. It's a problem with DataColumn.Expression. You're giving an expression which includes angle brackets, so it thinks you're trying to perform comparisons.

I can't say I know much about DataColumn.Expression, but you should look into how it quotes strings... and how it quotes quotes within strings! For example, this might work:

dtCol.Expression = string.Format
    ("'<a href=\"{0}'+ImageFilename+'\">View Image</a>'", 
     ImageDownloadPath);

However, I think it's likely to make your life a lot simpler if you didn't try to compute the HTML in the expression to start with. Can you really not apply processing any later?

Jon Skeet
hi Jo,I've tried your code and it's working. However it is displaying the <a href="http://localhost/mhercks/mhercksImageGallery/images/"1.jpg">View Image</a>What i want to happen is make a hyper link "View Image" not the whole <a href=""> </a> tag in my gridview column
nhoyti
@nhoyti: Well that's up to how you *use* your datatable. As I said, I don't think this is the most appropriate way of doing things anyway.
Jon Skeet
A: 

Hi Pros,

Thanks for helping me out. I was able to figure out the way to do it. Thanks for all the posts.

alt text

nhoyti