Can anyone get the format method on the WebGrid columns to work. Here's my attempt.
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.
Thanks for trying to help. I can't get this to compile because I'm not using Razor. Also I'm in the View and I don't know how to reference the item object. Any ideas?
Thanks for replying. I got this to work using the Lambda function shown below, but I don't like this solution. I want to call a stand-alone function that can dynamically format different columns. Can you suggest another way to do this? 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. I need that information in order to select the correct format.
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.
DallasSteve
Member
221 Points
331 Posts
WebGrid Column Format
Apr 17, 2012 06:07 PM|LINK
Can anyone get the format method on the WebGrid columns to work. Here's my attempt.
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.
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 = "Store"; oWebGridColumn2.ColumnName = "Value"; oWebGridColumn3.ColumnName = "Rank"; 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; }private Object ColumnFormat(Object item)
{
Object sColumnFormat = "#,##0.00";
return sColumnFormat;
}
www.SteveGaines.us
Allen Li - M...
Star
10411 Points
1196 Posts
Re: WebGrid Column Format
Apr 19, 2012 06:07 AM|LINK
Hi, you can format WebGrid columns with the following method:
@grid.GetHtml( columns: grid.Columns( grid.Column("DataBaseColumnName", "Store", format: @<i>@item.DataBaseColumnName</i>), grid.Column("DataBaseColumnName", "Value", format: @<i>@item.DataBaseColumnName</i>), grid.Column("DataBaseColumnName", "Rank", format: @<i>@item.DataBaseColumnName</i>) ) )For more information, please refer to the example on the following link:
http://www.asp.net/web-pages/tutorials/data/6-displaying-data-in-a-grid
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework
DallasSteve
Member
221 Points
331 Posts
Re: WebGrid Column Format
Apr 19, 2012 07:04 PM|LINK
Allen
Thanks for trying to help. I can't get this to compile because I'm not using Razor. Also I'm in the View and I don't know how to reference the item object. Any ideas?
Steve
www.SteveGaines.us
Allen Li - M...
Star
10411 Points
1196 Posts
Re: WebGrid Column Format
Apr 23, 2012 10:45 AM|LINK
Hi, in order to narrow down the cause of the issue, please paste your markup and behind codes here for analysis.
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework
DallasSteve
Member
221 Points
331 Posts
Re: WebGrid Column Format
Apr 23, 2012 02:13 PM|LINK
Allen
Thanks for replying. I got this to work using the Lambda function shown below, but I don't like this solution. I want to call a stand-alone function that can dynamically format different columns. Can you suggest another way to do this? 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. I need that information in order to select the correct format.
WebGridColumn oWebGridColumn1 = oGrid.Column(columnName: "Store", header: "Store", style: "ArialRight2", format: (item) => new HtmlString(Convert.ToInt32(item["Store"]).ToString("#,##0"))); WebGridColumn oWebGridColumn2 = oGrid.Column(columnName: "Value", header: "Value", style: "ArialRight2", format: (item) => new HtmlString(Convert.ToDouble(item["Value"]).ToString("#0.0%"))); WebGridColumn oWebGridColumn3 = oGrid.Column(columnName: "Rank", header: "Rank", style: "ArialRight2", format: (item) => new HtmlString(Convert.ToInt32(item["Rank"]).ToString("#,##0"))); String sStandingsGrid = Convert.ToString(oGrid.GetHtml(columns: oGrid.Columns(oWebGridColumn1, oWebGridColumn2, oWebGridColumn3), headerStyle: "HeaderUnderline", alternatingRowStyle: "ArialRight2", rowStyle: "ArialRight2"));www.SteveGaines.us
Allen Li - M...
Star
10411 Points
1196 Posts
Re: WebGrid Column Format
Apr 25, 2012 07:55 AM|LINK
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.
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework