Much better scrolling with Datagrid
I am not happy with the usual scrolling tip you can find everywhere about Datagrids.
Why ? Because you have an external scrolling, so when you scroll, you lost the headings :-(
As you can see in this example, if I have a long list I lost the header.
Most of the solutions are based on the duplication of the Header to a Table on top of the Datagrid.
It's not really good, because you lost the flexibility of the Datagrid attributes and styles.
After a long search, I finally found an answer.. from MSDN ;-)
I adapt this to the Datagrid, and it's working perfectly as you can see in this screenshot of my CMS !
The things to know is you need an .htc file, so obviously this solution is for IE only.
But if you are building an Intranet or an admin page, this is really neat.
The main table created with Datagrid has a stylesheet embedding with the call to this htc code.
Scroll.htc
<PUBLIC:ATTACH event="ondocumentready" handler="onDocumentReady" /> // Get column widths // Add header row // Add body rows // Set up body container // Change existing table // Set column widths of all but the last column tblHeader.style.fontSize = "100%"; tblBody.style.fontSize = "100%"; this.cellSpacing = 0; |
In the ASP.Net page, where you implement your Datagrid, you need to have the styles below in the <Head> section (eventually you can link to an external stylesheet):
<STYLE>
.tblMain
{
behavior:url(scroll.htc);
background-color: highlight;
border: 1px solid darkblue;
font-family: Verdana;
font-size: .8em;
}
.tblHeader
{
color: highlighttext;
}
.tblBody
{
background-color: #EEEEEE;
color: darkblue;
}
</STYLE>
And finally the Datagrid object (in red and bold the specific parts). This example is the code for the Datagrid shown in the screenshot above :
<asp:datagrid bodyHeight="100" bodyCSS="tblBody" headerCSS="tblHeader"
cssclass="tblMain" id="DGVersions" Runat="server" AutoGenerateColumns="false" Font-Size="8"
HeaderStyle-Font-Bold="True" HeaderStyle-HorizontalAlign="Center" Width="280">
<AlternatingItemStyle CssClass="Alt" />
<Columns>
<asp:BoundColumn DataField="SectionVersion" HeaderText="Version" HeaderStyle-Width=100 ItemStyle-Width="100" ItemStyle-HorizontalAlign=center/>
<asp:BoundColumn DataField="DateCreation" headertext="Created" HeaderStyle-HorizontalAlign=Center HeaderStyle-Width=100 ItemStyle-Width="100" />
<asp:TemplateColumn ItemStyle-Width=50>
<ItemTemplate>
<asp:ImageButton ID="ButDelVersion" Runat="server" ImageUrl="~/images/delete.gif" CommandName="Delete" />
<asp:ImageButton ID="ButEditVersion" Runat="server" imageurl="~/images/edit.gif" CommandName="Edit" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
This is indeed a first version, and I have to fix some stuff like:
- Alternative colors for the datagrid rows
- Writing a Javascript equivalent code to make the scroll working with other browsers.
I hope also that this scroll idea will be part of ASP.Net 2.0 natively ;-)