WebGrid Column Formathttp://forums.asp.net/t/1793909.aspx/1?WebGrid+Column+FormatWed, 25 Apr 2012 07:55:57 -040017939094937243http://forums.asp.net/p/1793909/4937243.aspx/1?WebGrid+Column+FormatWebGrid Column Format <p>Can anyone get the format method on the WebGrid columns to work. Here's my attempt.</p> <p>And can anyone get the Code insert function here fixed. This is the second time it ate my post when I tried to add code. I guess Microsoft built this forum, too.</p> <pre class="prettyprint">private delegate Object ColumnFormatDelegate(Object item); private WebGridColumn[] GetGridColumns() { WebGridColumn[] oWebGridColumns = new WebGridColumn[3]; WebGridColumn oWebGridColumn1 = new WebGridColumn(); WebGridColumn oWebGridColumn2 = new WebGridColumn(); WebGridColumn oWebGridColumn3 = new WebGridColumn(); oWebGridColumn1.ColumnName = &quot;Store&quot;; oWebGridColumn2.ColumnName = &quot;Value&quot;; oWebGridColumn3.ColumnName = &quot;Rank&quot;; ColumnFormatDelegate oColumnFormatDelegate = new ColumnFormatDelegate(ColumnFormat); oWebGridColumn1.Format(oColumnFormatDelegate); oWebGridColumn2.Format(oColumnFormatDelegate); oWebGridColumn3.Format(oColumnFormatDelegate); oWebGridColumns[0] = oWebGridColumn1; oWebGridColumns[1] = oWebGridColumn2; oWebGridColumns[2] = oWebGridColumn3; return oWebGridColumns; }</pre> <p><span color="#0000ff" size="2" face="Consolas" style="color:#0000ff; font-family:Consolas; font-size:small"><span color="#0000ff" size="2" face="Consolas" style="color:#0000ff; font-family:Consolas; font-size:small"><span color="#0000ff" size="2" face="Consolas" style="color:#0000ff; font-family:Consolas; font-size:small">private</span></span></span><span size="2" face="Consolas" style="font-family:Consolas; font-size:small"><span size="2" face="Consolas" style="font-family:Consolas; font-size:small"> </span></span><span color="#2b91af" size="2" face="Consolas" style="color:#2b91af; font-family:Consolas; font-size:small"><span color="#2b91af" size="2" face="Consolas" style="color:#2b91af; font-family:Consolas; font-size:small"><span color="#2b91af" size="2" face="Consolas" style="color:#2b91af; font-family:Consolas; font-size:small">Object</span></span></span><span size="2" face="Consolas" style="font-family:Consolas; font-size:small"><span size="2" face="Consolas" style="font-family:Consolas; font-size:small"> ColumnFormat(</span></span><span color="#2b91af" size="2" face="Consolas" style="color:#2b91af; font-family:Consolas; font-size:small"><span color="#2b91af" size="2" face="Consolas" style="color:#2b91af; font-family:Consolas; font-size:small"><span color="#2b91af" size="2" face="Consolas" style="color:#2b91af; font-family:Consolas; font-size:small">Object</span></span></span><span size="2" face="Consolas" style="font-family:Consolas; font-size:small"><span size="2" face="Consolas" style="font-family:Consolas; font-size:small"> item)</span></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p> <p>{</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span color="#2b91af" size="2" face="Consolas" style="color:#2b91af; font-family:Consolas; font-size:small"><span color="#2b91af" size="2" face="Consolas" style="color:#2b91af; font-family:Consolas; font-size:small"><span color="#2b91af" size="2" face="Consolas" style="color:#2b91af; font-family:Consolas; font-size:small">Object</span></span></span><span size="2" face="Consolas" style="font-family:Consolas; font-size:small"><span size="2" face="Consolas" style="font-family:Consolas; font-size:small"> sColumnFormat = </span></span><span color="#a31515" size="2" face="Consolas" style="color:#a31515; font-family:Consolas; font-size:small"><span color="#a31515" size="2" face="Consolas" style="color:#a31515; font-family:Consolas; font-size:small"><span color="#a31515" size="2" face="Consolas" style="color:#a31515; font-family:Consolas; font-size:small">&quot;#,##0.00&quot;</span></span></span><span size="2" face="Consolas" style="font-family:Consolas; font-size:small"><span size="2" face="Consolas" style="font-family:Consolas; font-size:small">;</span></span></p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span color="#0000ff" size="2" face="Consolas" style="color:#0000ff; font-family:Consolas; font-size:small"><span color="#0000ff" size="2" face="Consolas" style="color:#0000ff; font-family:Consolas; font-size:small"><span color="#0000ff" size="2" face="Consolas" style="color:#0000ff; font-family:Consolas; font-size:small">return</span></span></span><span size="2" face="Consolas" style="font-family:Consolas; font-size:small"><span size="2" face="Consolas" style="font-family:Consolas; font-size:small"> sColumnFormat;</span></span></p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p> <p></p> 2012-04-17T18:07:37-04:004940163http://forums.asp.net/p/1793909/4940163.aspx/1?Re+WebGrid+Column+FormatRe: WebGrid Column Format <p>Hi, you can format WebGrid columns with the following method:</p> <pre class="prettyprint">@grid.GetHtml( columns: grid.Columns( grid.Column(&quot;DataBaseColumnName&quot;, &quot;Store&quot;, format: @&lt;i&gt;@item.DataBaseColumnName&lt;/i&gt;), grid.Column(&quot;DataBaseColumnName&quot;, &quot;Value&quot;, format: @&lt;i&gt;@item.DataBaseColumnName&lt;/i&gt;), grid.Column(&quot;DataBaseColumnName&quot;, &quot;Rank&quot;, format: @&lt;i&gt;@item.DataBaseColumnName&lt;/i&gt;) ) )</pre> <p>For more information, please refer to the example on the following link:</p> <p><a href="http://www.asp.net/web-pages/tutorials/data/6-displaying-data-in-a-grid">http://www.asp.net/web-pages/tutorials/data/6-displaying-data-in-a-grid</a></p> 2012-04-19T06:07:15-04:004941677http://forums.asp.net/p/1793909/4941677.aspx/1?Re+WebGrid+Column+FormatRe: WebGrid Column Format <p>Allen</p> <p>Thanks for trying to help.&nbsp; I can't get this to compile because I'm not using Razor.&nbsp; Also I'm in the View and I don't know how to reference the item object.&nbsp; Any ideas?</p> <p>Steve</p> <p>&nbsp;</p> 2012-04-19T19:04:15-04:004946124http://forums.asp.net/p/1793909/4946124.aspx/1?Re+WebGrid+Column+FormatRe: WebGrid Column Format <p>Hi, in order to narrow down the cause of the issue, please paste your markup and behind codes here for analysis.</p> 2012-04-23T10:45:32-04:004946615http://forums.asp.net/p/1793909/4946615.aspx/1?Re+WebGrid+Column+FormatRe: WebGrid Column Format <p>Allen</p> <p>Thanks for replying.&nbsp; I got this to work using the Lambda function shown below, but I don't like this solution.&nbsp; I want to call a stand-alone function that can dynamically format different columns.&nbsp; Can you suggest another way to do this?&nbsp; The problem I was having calling a stand-alone function is that the item property of the data doesn't seem to have any way to identify which column is being formatted within the function.&nbsp; I need that information in order to select the correct format.</p> <pre class="prettyprint">WebGridColumn oWebGridColumn1 = oGrid.Column(columnName: &quot;Store&quot;, header: &quot;Store&quot;, style: &quot;ArialRight2&quot;, format: (item) =&gt; new HtmlString(Convert.ToInt32(item[&quot;Store&quot;]).ToString(&quot;#,##0&quot;))); WebGridColumn oWebGridColumn2 = oGrid.Column(columnName: &quot;Value&quot;, header: &quot;Value&quot;, style: &quot;ArialRight2&quot;, format: (item) =&gt; new HtmlString(Convert.ToDouble(item[&quot;Value&quot;]).ToString(&quot;#0.0%&quot;))); WebGridColumn oWebGridColumn3 = oGrid.Column(columnName: &quot;Rank&quot;, header: &quot;Rank&quot;, style: &quot;ArialRight2&quot;, format: (item) =&gt; new HtmlString(Convert.ToInt32(item[&quot;Rank&quot;]).ToString(&quot;#,##0&quot;))); String sStandingsGrid = Convert.ToString(oGrid.GetHtml(columns: oGrid.Columns(oWebGridColumn1, oWebGridColumn2, oWebGridColumn3), headerStyle: &quot;HeaderUnderline&quot;, alternatingRowStyle: &quot;ArialRight2&quot;, rowStyle: &quot;ArialRight2&quot;));</pre> <p></p> 2012-04-23T14:13:59-04:004950156http://forums.asp.net/p/1793909/4950156.aspx/1?Re+WebGrid+Column+FormatRe: WebGrid Column Format <p>Hi, thanks for sharing. Your post will help others encountered the same issue. As far as I know, it is not possible to dynamically format different columns with a function. If I found one, I will let you know, thanks for sharing again.</p> 2012-04-25T07:55:57-04:00