3

I just read: http://www.mikesdotnetting.com/Article/154/Looking-At-The-WebMatrix-WebGrid and see a reference to [Optional, Default Value(null)] string header

Header text if you don't want database field names

But I'm not sure how to format cell values. For example, if I have a WebGrid that looks like this:

Column Name          Column Name          Column Name          Column Name          
Cell value           Cell value           Cell value           Cell value           
Cell value           Cell value           Cell value           Cell value           
Cell value           Cell value           Cell value           Cell value           
Cell value           Cell value           Cell value           Cell value           
Cell value           Cell value           Cell value           Cell value           

I would like to make each Cell clickable, and depending on the column it is in, I would like its corresponding hyperlink to be different from another cell's hyperlink.

Can this be done using WebGrid? I've done this in PHP, but have no idea where to look, or how to do it with WebGrid.

Upon searching Google, Bing and Yahoo!(?), I only see results for those premium WebGrid components, not a single result for the real WebGrid, nor any results that would be of any help.

  • 1
    Have you looked at this? http://weblogs.asp.net/andrebaltieri/archive/2010/11/02/asp-net-mvc-3-working-with-webgrid-part-2.aspx – Derek Beattie Mar 24 '11 at 00:49
  • Thanks for the link, I'm sure it'll come in handy soon, but when I tried using the code there, I kept getting overloading errors. –  Mar 24 '11 at 01:07

1 Answers1

2

In Mike's DotNetting article that you reference, he shows how to display the shortdate in the following line of code:

format: @<text>@item.DatePublished.ToShortDateString()</text>

Since the format replaces the entire cell, you just need to put the code that produces the HTML you want, including hyperlinks. Since to produce anything complicated might make that line of code too painful to read, it might be best to write your own class/function that produces the code you want. I have a situation like that and my format line looks like:

format : @<text>@Html.Raw(NDisplay.displayComment( username, item.AssignedTo, item.NALComment, item.refID, item.Process))</text>,

And then in that function:

public static string displayComment( string username, string AssignedTo, string NALComment, int refID, string Process) 
{
    // various junk code removed, testing user and rights
    // here we know we have the right user, he or she needs the edit URL
    // two parameters are passed, first the refID, second the Process (or document)
    string e = "<a href =\"../Process/" + refID.ToString() + "/" + Process +"/\">Edit</a> " + NALComment;

    return e;
}

In each cell, there's an edit hyperlink, followed by a text comment.

Knox
  • 2,909
  • 11
  • 37
  • 65
  • 2
    If you are writing your own custom function which is intended to generate HTML, you should use a Helper: http://www.mikesdotnetting.com/Article/173/The-Difference-Between-@Helpers-and-@Functions-In-WebMatrix – Mike Brind Mar 24 '11 at 19:06
  • 1
    @Mike Brind, very interesting. I didn't consider that since the function is inside a column definition. – Knox Mar 24 '11 at 20:27
  • thanks, but that still doesn't work. All of these format things always show the same error. –  Mar 24 '11 at 23:01
  • Sorry, it works perfectly. I just forgot to refresh the page. (having a really blonde day). The following works good: *format: @Jason Rules* –  Mar 24 '11 at 23:07