Set the controls to lightly toasted muffins... RSS 2.0
 Thursday, June 01, 2006

Yesterday I was working on a product list using the Gridview component and needed to render the list of products ordered by category. The list also had to have a subheading for the category of the product e.g.

It took a while to figure out but this article by Tim Heuer (there's a show/hide code at the bottom of the page) helped solve the problem. With a minor modification I got what I wanted.

I didn't require the sorting mechanism so I assigned the DataKeyNames property of the Gridview the names of the fields used to order the list before binding to the datasource so that I could pick out the category ID (line 71) in the controls overridden Render method:

    1         protected override void  Render(HtmlTextWriter writer)

    2         {

    3             Table table = (Table)this.productsGrid.Controls[0];

    4 

    5             int lastCategory = -1;

    6 

    7             foreach(GridViewRow row in productsGrid.Rows)

    8             {

    9                 int realIndex = table.Rows.GetRowIndex(row);

   10                 int currentCategory = Convert.ToInt32(this.productsGrid.DataKeys[row.RowIndex].Values[1]);

   11                 if(currentCategory != lastCategory)

   12                 {

   13                     GridViewRow groupHeaderRow =

   14                         new GridViewRow(realIndex, realIndex, DataControlRowType.Separator, DataControlRowState.Normal);

   15                     TableCell newCell = new TableCell();

   16                     newCell.ColumnSpan = this.productsGrid.Columns.Count;

   17                     newCell.BackColor = System.Drawing.Color.FromArgb(233, 229, 229);

   18                     newCell.ForeColor = System.Drawing.Color.DarkGray;

   19                     newCell.Font.Bold = true;

   20 

   21                     switch(currentCategory)

   22                     {

   23                         case 515:

   24                         case 517:

   25                             newCell.Text = "Home Products";

   26                             break;

   27 

   28                         default:

   29                             newCell.Text = "Business Products";

   30                             break;

   31                     }

   32 

   33                     groupHeaderRow.Cells.Add(newCell);

   34 

   35                     table.Controls.AddAt(realIndex, groupHeaderRow);

   36                     lastCategory = currentCategory;

   37                 }

   38             }

   39 

   40             base.Render(writer);

   41         }

 

Thursday, June 01, 2006 5:23:29 PM UTC  #    Comments [5] -
.NET
Thursday, June 01, 2006 8:17:58 PM UTC
Tut, Tut. Magic numbers in yer code.
Friday, June 02, 2006 12:08:34 AM UTC
I know..I know....but it was a prototype and I left them in for code clarity/readability because I thought it easier to catch on to the concept than not :)
Friday, February 09, 2007 7:21:20 PM UTC
Comment on last comment. To work with master pages, extract the code from the OVERRIDDEN RENDER and insert in to the GRIDVIEWS PRE-RENDER
Worked flawlessly!!!

Thanks.
MkTapps
Sunday, February 11, 2007 3:40:13 PM UTC
Thanks for the last comment by MkTapps for the info on getting this working with Master Pages.

I didn't actually encounter this as a problem because I only had <form runat="server"> tag on the page and that resided in the Master Page anyway.

Thanks for reading.
Kev
Wednesday, January 23, 2008 4:30:24 PM UTC
Please nevermind the last question - I just realized my own stupidity. Got it now! Thanks for the info!
Mark
All comments require the approval of the site owner before being displayed.
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Live Comment Preview
Now Playing
Top Artists This Week
Fluff

Powered by FeedBurner
Categories
Archive
<June 2006>
SunMonTueWedThuFriSat
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008
Kevin Kenny
Sign In
Statistics
Total Posts: 194
This Year: 41
This Month: 0
This Week: 0
Comments: 101
All Content © 2008, Kevin Kenny
DasBlog theme 'Business' created by Christoph De Baene (delarou)