Replace/Remove Columns returned from GetDynamicMemberNameshttp://forums.asp.net/t/1767122.aspx/1?Replace+Remove+Columns+returned+from+GetDynamicMemberNamesWed, 08 Feb 2012 06:58:23 -050017671224821731http://forums.asp.net/p/1767122/4821731.aspx/1?Replace+Remove+Columns+returned+from+GetDynamicMemberNamesReplace/Remove Columns returned from GetDynamicMemberNames <p>In a tutorial posted by Mike, Clickable webGrid Row, once a row was clicked, it will be redirected into another page displaying the data related to the clicked row. At the foreach section,&nbsp; column names from the database were dynamically displayed. what if I want to remove some columns that I don't want to be displayed? How about renaming some of the columns? Thank you for responses!</p> <p></p> <pre class="prettyprint">@foreach(var item in data.GetDynamicMemberNames()){ &lt;strong&gt;@item:&lt;/strong&gt; @data[item]&lt;br /&gt; }</pre> 2012-02-08T05:21:18-05:004821846http://forums.asp.net/p/1767122/4821846.aspx/1?Re+Replace+Remove+Columns+returned+from+GetDynamicMemberNamesRe: Replace/Remove Columns returned from GetDynamicMemberNames <p>If you want to do this modification for all the detail pages, the easiest way is changing the SELECT statement.&nbsp;The details.cshtml&nbsp;would become,&nbsp;e.g.:</p> <pre class="prettyprint">@{ Page.Title = &quot;Details&quot;; var db = Database.Open(&quot;Northwind&quot;); // var query = &quot;SELECT * FROM Customers WHERE CustomerID = @0&quot;; var query = &quot;SELECT CustomerID AS Id, CompanyName AS Company, ContactName AS Contact, &quot; &#43; &quot;Address, City, PostalCode AS [Postal Code] FROM Customers WHERE CustomerID = @0&quot;; var data = db.QuerySingle(query, UrlData[0]); } &lt;h1&gt;Details&lt;/h1&gt; @foreach(var item in data.GetDynamicMemberNames()){ &lt;strong&gt;@item:&lt;/strong&gt; @data[item]&lt;br /&gt; }</pre> <p>&nbsp;</p> 2012-02-08T06:34:10-05:004821869http://forums.asp.net/p/1767122/4821869.aspx/1?Re+Replace+Remove+Columns+returned+from+GetDynamicMemberNamesRe: Replace/Remove Columns returned from GetDynamicMemberNames <p>You have (at least) two choices. First, don't use the code above. Create a template:</p> <pre class="prettyprint">&lt;div&gt;&lt;strong&gt;Company Name:&lt;/strong&gt; @data.CompanyName&lt;/div&gt; &lt;div&gt;&lt;strong&gt;Contact Name:&lt;/strong&gt; @data.ContactName&lt;/div&gt; etc</pre> <p></p> <p>Second, use aliases in your SQL:</p> <pre class="prettyprint">@{ var sql = @"SELECT CompanyName AS [Company Name], ContactName AS [Contact Name] FROM Customers WHERE CustomerId = 'ALFKI'"; var data = Database.Open("Northwind").QuerySingle(sql); }</pre> <p>Then you can use the GetDynamicMemberNames method:</p> <pre class="prettyprint">@foreach(var item in data.GetDynamicMemberNames()){ &lt;strong&gt;@item:&lt;/strong&gt; @data[item]&lt;br /&gt; }</pre> <p>Note that if you want to introduce spaces in your aliases, you MUST enclose the alias in [ ] brackets. Also, if you do this, you cannot use dynamic properties to reference the data. You have to use the indexer approach, so</p> <pre class="prettyprint">@data.Company Name</pre> <p>will not work, whereas the following will:</p> <pre class="prettyprint">@data["Company Name"]</pre> <p></p> 2012-02-08T06:44:28-05:004821901http://forums.asp.net/p/1767122/4821901.aspx/1?Re+Replace+Remove+Columns+returned+from+GetDynamicMemberNamesRe: Replace/Remove Columns returned from GetDynamicMemberNames <p>Thanks Mike! works perfectly. btw, I use ' ' insted of [ ] as the database I am using is MySQL.</p> <p></p> 2012-02-08T06:58:23-05:00