Tuesday, July 3, 2007

How cool was that!

On your O's update for July 3rd:

O's beat the White Sox in the top of the 9th inning! That was AWESOME. First time this year the heart break was on the other side (I don't like Mothers Day anymore). It was a complete team win. Patterson (who taught him to hit all-of-a-sudden?), Roberts, Gomez, Honeybear, Millar... they all had a game changing hit in the last 2 innings. I had given them up for dead and was on my way to the TV when Fields dropped the Millars soft grounder in the 8th. Then I thought, "What the hell, its 5 more outs." So glad I did.

O!, by the way, Erik Bedard - don't sweat it. Even god had to rest on the seventh day and you earned an off day, what with not giving up more than 4 runs a game since opening day (19 starts).

Monday, July 2, 2007

Developer Guide: ASP.NET Grid View Part 2

Common Properties


The GridView control offers a lot of your standard .NET properties, CssClass, Height, Width etc. But some of them are specific to the GridView. These include:


  • RowStyle
  • AlternatingRowStyle
  • HeaderStyle
  • PagerStyle
  • EmptyDataTemplate

RowStyle and AlternatingRowStyle serve a similar function. RowStyle is the CssClass applied to ever ODD row in the GridView (1, 3, 5 etc.) while AlternatingRowStyle is for the EVEN rows. This lets you create a checkbook pattern to your grid views making it easier to read large amounts of data.

PageStyle and HeaderStyle are for the same thing as RowStyle but it styles the Header Row (duh) and the PagerRow. The PagerRow is the final row of the table following the FooterRow that is reserved for Paging Links. More these later.

The EmptyDataTemplate should also always be filled in. This is a set of HTML markup that is used in the case where the data set the GridView is bound to is empty. Under ordinary cirumstances this results in white space on the page. No header, no footer no nothin.

Always set these properties.

For the lazy among us (and yes I include myself) just cut and paste the following:

<AlternatingRowStyle CssClass="EvenRow" />
<RowStyle CssClass="OddRow" />
<HeaderStyle CssClass="HeaderRow" />
<PagerStyle CssClass="PageLinks" />
<EmptyDataTemplate>
<br /><b><span style="color: Red;">No Results</span></b><p />
</EmptyDataTemplate>

That closes the book on your style properties but there are a few more properties that you should set before close the tag:
  • AllowPaging & PageSize
  • AutoGenerateColumns
  • DataKeyNames
  • GridLines


AllowPaging turns the automatic paging of the grid on and off (true or false). If you turn this option on (true) you must also define a pair of functions to handle the OnPageIndexChanged and OnPageIndexChanging events. More on these later when I discuss paging. PageSize dictates the number of rows to be displayed before moving onto the next page of resutls.

AutoGenerateColumns turns on or off the automatic generation of column values and headers from your input data set. If this option is turned on (not reccomended) the GridView will examine your dataset and DataBind and generate one column per column in the dataset with a header named after the SQL column name. Why don't I reccomend this? First, its lazy. Second, you wont get to know your data. Third, SQL column names are rarely useful for your typical user. Fourth, trying to shoe-horn in better column names into your SQL select statement makes that statement a nightmare. And, ofcourse, .NET turns this on by default. Punks.

DataKeyNames is used to identify the primary key in the input data set. This only needs to be set if you're going to turn on sorting inside the GridView (another article). But it is good practice to always identify a PK column.

GridLines turns the automatic generation of table grid lines on or off. You can also specify Horizontal or Vertical lines only.

Too put it all together we get something like this:

<asp:GridView ID="JobSummaryGrid" runat="server" AllowPaging="True" AutoGenerateColumns="False" OnDataBound="JobSummaryGrid_OnDataBound" OnRowCommand="JobSummary_ItemCommand" Width="884px" OnRowDataBound="JobSummary_RowDataBound" DataKeyNames="Id" GridLines="None" CssClass="ListView_Table" OnPageIndexChanged="JobSummary_OnPageIndexChanged" OnPageIndexChanging="JobSummary_OnPageIndexChanging" PageSize="20" CellPadding="0">

<AlternatingRowStyle CssClass="EvenRow" />
<RowStyle CssClass="OddRow" />
<HeaderStyle CssClass="HeaderRow" />
<PagerStyle CssClass="PageLinks" />
<EmptyDataTemplate>
<br /><b><span style="color: Red;">No Results</span></b><p />
</EmptyDataTemplate>
</asp:GridView>