C#.NET Tips & Tricks

What / Why?

I've been dragged into .NET programming kicking and screaming. Since you're on this page, I'll assume that you're in the same boat. Welcome to the Good Ship Lollipop.

This is going to be a tips and tricks page. I'd consider making it into a forum, but there are already a million forums for .NET. So, this isn't going to be another one. This is simply going to be My Collection of Bad Ideas that Work.

And Here We Have Content


Dynamic BoundColumn's in Code Behind

Problem:

Let's imagine that you have a large dataset. Let's now imagine that you are not Microsoft's expected user and you have, say, 100 columns in your dataset. Row paging is great, but what are we going to do with all those columns? We could display them all, but that would be ugly.

What we need is something along the lines of "column paging". The way most people know of declaring their columns is to edit the <columns> block in their asp control in the actual .aspx file.

<asp:DataGrid ID="DefaultGrid" Runat="server" AutoGenerateColumns=False> <Columns> <asp:BoundColumn DataField="Index" ReadOnly=True HeaderText="#"> </asp:BoundColumn> <asp:HyperLinkColumn DataNavigateUrlField="Hyperlink" DataTextField="Title" HeaderText="Title"> </asp:HyperLinkColumn> <asp:BoundColumn DataField="Description" ReadOnly=True HeaderText="Description"> </asp:BoundColumn> </Columns> </asp:DataGrid>

This isn't particularly efficient if we want things to be automated though, is it?

Solution:

What we really want to do here is define which columns are bound in our code behind files. Doesn't that sound like a good idea? Yes. It does.

private void InitializeBoundColumns() { DefaultGrid.AutoGenerateColumns = false; BoundColumn indexCol = new BoundColumn(); indexCol.DataField = "Index"; indexCol.HeaderText = "#"; BoundColumn descCol = new BoundColumn(); descCol.DataField = "Description"; descCol.HeaderText = "Description"; HyperLinkColumn urlCol = new HyperLinkColumn(); urlCol.DataTextField = "Title"; urlCol.DataNavigateUrlField = "Hyperlink"; urlCol.HeaderText = "Title"; // Add three columns to collection. DefaultGrid.Columns.Add(indexCol); DefaultGrid.Columns.Add(urlCol); DefaultGrid.Columns.Add(descCol); }

References:

http://www.netomatix.com/DGAutoGen.aspx

Last updated: Sunday, 20-Sep-2009 16:55:45 CDT

Contact me at randall dot will at gmail dot com

Hobbies

Vintage Motorcycles

Networking

Facebook
LinkedIn

Presence

Ubuntu
Discount ASP.NET
CPAN
p2p Wrox
Roundcube
Freshmeat
Blogger
uShip
Psychology Software Tools
Virtual Corvair Club
Chopper Charles
Live Journal

Thoughts

Borrowed Philosophy
Historical Histrionics
Current Histrionics

Old Projects

Motorola V400 / Linux
HP ZE4200 / Linux
VPR Matrix 220a5 / Linux
FreeRadius Accounting / Linux
LDAP / FreeRadius Auth / Linux
Remote MySQL / OSX
Stewie Radio Automation
Wireless Wandering Done Right
DynDNS: TinyDNS / OpenSSH
Xilinx WebPACK 6.3 / Wine
Xilinx WebPACK 7.1 / Debian
Apache 301 Redirection
Using Your Debian Cache
Google AdSense
Gnome AdSense Monitor
Pixlatch Gallery Generator
C#.NET Tips & Tricks
Ubuntu Breezy Clockmod Bug