Posts in category .net - page 25

Extending GridView to access generated columns

ASP.NET’s GridView is a useful control and one of it’s best features is it’s ability to generate the columns automatically from the data source given.

The problem however is that these generated columns are not exposed as part of the Columns collection or indeed available at all so you can’t hide or manipulate the selected columns.

One simple scenario might be that you want the first column to be a “View” link to drill down into the row displayed. Whilst you can add the column to the GridView before data binding you can’t actually pull out the information needed from another columns to construct the URL.

By sub-classing GridView you can obtain this functionality with some caveats.

Version 1: Auto generated columns added to the Columns collection… with caveats.

using System;
using System.Data;
using System.Collections;
using System.Web.UI.WebControls;

public class GridViewEx1 : GridView
{
    private DataControlFieldCollection originalColumns;

    public GridViewEx1() : base() {
    }

    public void RecordColumns() {
        originalColumns = new DataControlFieldCollection();
        foreach(DataControlField column in Columns)
            originalColumns.Add(column as DataControlField);
    }

    public void ResetColumns() {
        if (originalColumns == null)
            RecordColumns();
        else {
            Columns.Clear();
            foreach(DataControlField column in originalColumns)
                Columns.Add(column as DataControlField);
        }
    }

    protected override ICollection CreateColumns(PagedDataSource dataSource, bool useDataSource) {
        ResetColumns();
        ICollection generatedColumns = base.CreateColumns(dataSource, useDataSource);
        foreach(DataControlField column in generatedColumns)
            if (!originalColumns.Contains(column))
                Columns.Add(column as DataControlField);
        return Columns;
    }
}

This version provides some compatibility with existing code/expectations in that the auto-generated columns are part of the Columns collection after the DataBind.

Should you call DataBind again however as well as wiping out the changes to the generated columns (they are, after all re-generated) any additional columns added to the collection after the first DataBind will also be lost as it does not track which are added by the programmer and which automatically.

Version 2: All bound columns exposed as BoundColumns, user ones as Columns.

using System;
using System.Data;
using System.Collections;
using System.Web.UI.WebControls;

public class GridViewEx2 : GridView
{
    private DataControlFieldCollection boundColumns = new DataControlFieldCollection();

    public GridViewEx2() : base() {
    }

    public DataControlFieldCollection BoundColumns {
        get { return boundColumns; }
    }

    protected override ICollection CreateColumns(PagedDataSource dataSource, bool useDataSource) {
        ICollection generatedColumns = base.CreateColumns(dataSource, useDataSource);
        BoundColumns.Clear();
        foreach (DataControlField column in generatedColumns)
            BoundColumns.Add(column as DataControlField);
        return BoundColumns;
    }
}

After the DataBind you will have full access to the generated columns as part of the BoundColumns collection.

[)amien

Subtext .NET blogging system 1.9 released

My favorite .NET blogging system, Subtext, is celebrating it’s first .NET 2.0 compatible release known as Subtext 1.9.

Haack and the team have worked hard on getting it out so here’s a thanks to them.

I’ve deployed it here with only a couple of minor issues which are most likely due to me previously running a self-modified version off the Subversion trunk. As a result it didn’t quite detect that it needed to upgrade the database nor that my skin was missing and it should use something else for now.

If anyone else has the same problem simply remove the 1.9.0 record from your subversion_version table and it should upgrade the tables and stored procedures appropriately.

Now Subtext is a Web Application Project you can add a deployment project so deploying to live is only two clicks away (you’ll need to install Web Application Project and Web Deployment Project upgrades to VS 2005 first until SP1 gets here)

  1. Right-click on Subtext.Web and choose Add Web Deployment Project
  2. Accept the defaults by clicking OK
  3. Right mouse button on your new Subtext.Web.csproj_deploy deployment project and choose Property Pages
  4. Set the output folder to the path to your blog’s home on your live server and hit OK

So now we have almost what we need but we don’t want the live server being deployed to every time we build so:

  1. Right-click on the SubtextSolution and choose Configuration Manager
  2. Un-check the build for Subtext.Web.csproj_deploy and press Close

Now you can modify, run, debug and test safely on your dev machine until you are happy it’s ready for live.

To deploy to live either build in Release mode or right-click on Subtext.Web.csproj_deploy and choose build.

You can also have the deployment project replace sections of the web.config on the target machine – useful when your live server connects to a different database for example.

[)amien

XNA Game Studio Express out today

Beta 1 of Microsoft’s free XNA Game Studio Express product should be available for download later today.

If you haven’t already got started you’ll need to download the following bits first:

Then sign up for the beta and come hang out in the IRC channel #xna on irc.quakenet.org.

Update

XNA Game Studio Express is now available for download!

[)amien

Choose your ORM: Runtime, code generation or build provider?

Selecting the right object-relational mapper is a tricky decision with many factors to weigh up.

One of the basic decisions is whether to go with a dynamic run-time (“black-box”) or a code generator.

I’m not a fan of the run-time approach – the discovery at run-time negatively impacts performance as it often uses reflection (or failing that post-compilation byte code modification) whilst robbing you of compile-time checking, IntelliSense support against your database objects, deployment and potentially licensing issues. In effect, it’s not that much better than a typed dataset.

Code generation provides for a much finer granularity letting you tweak the templates for the performance and features you need whilst also providing full compile-time checking and IntelliSense support.

Tools such as CodeSmith (my personal favorite), MyGeneration (free) do a good job of letting you write these templates and create the necessary ORM code but require being re-run every time you change the schema. During the starting phases of a project this could be quite often and goes against the whole concept of RAD.

So step in SubSonic and it’s build provider approach.

The idea here is that you modify your .config file to include the SubSonic build provider and it’s connection string, drop a simple text file in that lists which tables to work with and you’re done.

SubSonic now goes off to your database via the connection and generates all the code for tables you need and it’s magically there to be used like any other classes. Check out the demo to see just how easy it is.

SubSonic supports a large number of databases, has support for Enterprise Library, is open source and also provides simple “scaffold” pages that let you throw a basic web add/edit/update/delete table maintenance page by just throwing a table name attribute onto an empty page’s form element.

The only downside at this point is that it uses the ActiveRecord pattern for the ORM. If I manage to get some time to spend with it and can knock up a Domain Object + Data Mapper version I’ll let you know.

[)amien