Click here to Skip to main content
Click here to Skip to main content

How to Manually Create a Typed DataTable

By , 28 Oct 2008
 

Introduction

I am writing this article because there is not a lot of information on the web about using the DataTable.GetRowType() method, and the code examples that I found were plain wrong or incomplete. Furthermore, there doesn't appear to be any automated tools for creating just a typed DataTable--instead, there are tools for creating a typed DataSet. In the end, I ended up creating a typed DataSet simply to figure out what I was doing wrong with my manually created typed DataTable. So, this is a beginner article on what I learned, and the purpose is to provide an example and correct information as resource for others. I don't provide a tool for creating a type DataTable, that might be for a future article.

What is a Typed DataTable?

A typed DataTable lets you create a specific DataTable, already initialized with the required columns, constraints, and so forth. A typed DataTable typically also uses a typed DataRow, which lets you access fields through their property names. So, instead of:

DataTable personTable=new DataTable();
personTable.Columns.Add(new DataColumn("LastName"));
personTable.Columns.Add(new DataColumn("FirstName"));

DataRow row=personTable.NewRow();
row["LastName"]="Clifton";
row["FirstName"]="Marc";

using a typed DataTable would look something like this:

PersonTable personTable=new PersonTable();
PersonRow row=personTable.GetNewRow();
row.LastName="Clifton";
row.FirstName="Marc";

The advantage of a typed DataTable is the same as with a typed DataSet: you have a strongly typed DataTable and DataRow, and you are using properties instead of strings to set/get values in a row. Furthermore, by using a typed DataRow, the field value, which in a DataRow is an object, can instead be already cast to the correct type in the property getter. This improves code readability, and eliminates the chances of improper construction and typos in the field names.

Creating the Typed DataTable

To create a typed DataTable, create your own class derived from DataTable. For example:

public class PersonTable : DataTable
{
}

There are two methods that you need to override: GetRowType() and NewRowFromBuilder(). The point of this article is really that it took me about four hours to find out that I needed to override the second method.

protected override Type GetRowType()
{
  return typeof(PersonRow);
}

protected override DataRow NewRowFromBuilder(DataRowBuilder builder)
{
  return new PersonRow(builder);
}

That second method is vital. If you don't provide it, you will get an exception concerning "array type mismatch" when attempting to create a new row. It took me hours to figure that out!

Creating the Typed DataRow

Next, you need a typed DataRow to define the PersonRow type referenced above.

public class PersonRow : DataRow
{
}

Constructor

The constructor parameters, given the NewRowFromBuilder call above, are obvious, but what is less obvious is that the constructor must be marked protected or internal, because the DataRow constructor is marked internal.

public class PersonRow : DataRow
{
  internal PersonRow(DataRowBuilder builder) : base(builder)
  {
  }
}

Filling in the Details

Next, I'll show the basics for both the typed DataTable and DataRow. The purpose of these methods and properties is to utilize the typed DataRow to avoid casting in the code that requires the DataTable.

PersonTable Methods

Constructor

In the constructor, we can add the columns and constraints that define the table.

public class PersonTable : DataTable
{
  public PersonTable()
  {
    Columns.Add(new DataColumn("LastName", typeof(string)));
    Columns.Add(new DataColumn("FirstName", typeof(string)));
  }
}

The above is a trivial example, which doesn't illustrate creating a primary key, setting constraints on the fields, and so forth.

Indexer

You can implement an indexer that returns the typed DataRow:

public PersonRow this[int idx]
{
  get { return (PersonRow)Rows[idx]; }
}

The indexer is implemented on the typed DataTable because we can't override the indexer on the Rows property. Bounds checking can be left to the .NET framework's Rows property. The typical usage for a non-typed DataRow would look like this:

DataRow row=someTable.Rows[n];

whereas the indexer for the type DataRow would look like this:

PersonRow row=personTable[n];

Not ideal, as it looks like I'm indexing an array of tables. An alternative would be to implement a property, perhaps, named PersonRows; however, this would require implementing a PersonRowsCollection and copying the Rows collection to the typed collection, which would most likely be a significant performance hit every time we index the Rows collection. This is even less ideal!

Add

The Add method should accept the typed DataRow. This protects us from adding a row to a different table. If you try to do that with a non-typed DataTable, you get an error at runtime. The advantage of a typed Add method is that you will get a compiler error, rather than a runtime error.

public void Add(PersonRow row)
{
  Rows.Add(row);
}

Remove

A typed Remove method has the same advantages as the typed Add method above:

public void Remove(PersonRow row)
{
  Rows.Remove(row);
}

GetNewRow

Here, we end up with a conflict if we try to use the DataTable.NewRow() method, because the only thing different is the return type, not the method signature (parameters). So, we could write:

public new PersonRow NewRow()
{
  PersonRow row = (PersonRow)NewRow();

  return row;
}

However, I am personally against using the "new" keyword to override the behavior of a base class. So, I prefer a different method name all together:

public PersonRow GetNewRow()
{
  PersonRow row = (PersonRow)NewRow();

  return row;
}

PersonRow Properties

The typed DataRow should include properties for the columns defined in the PersonTable constructor:

public string LastName
{
  get {return (string)base["LastName"];}
  set {base["LastName"]=value;}
}

public string FirstName
{
  get {return (string)base["FirstName"];}
  set {base["FirstName"]=value;}
}

The advantage here is that we have property names (any typo results in a compiler error), we can utilize Intellisense, and we can convert the object type here instead of in the application. Furthermore, we could add validation and property changed events if we wanted to. This might also be a good place to deal with DBNull to/from null conversions, and if we use nullable types, we can add further intelligence to the property getters/setters.

PersonRow Constructor

You may want to initialize the fields in the constructor:

public class PersonRow : DataRow
{
  internal PersonRow(DataRowBuilder builder) : base(builder)
  {
    LastName=String.Empty;
    FirstName=String.Empty;
  }
}

Row Events

If necessary, you may want to implement typed row events. The typical row events are:

  • ColumnChanged
  • ColumnChanging
  • RowChanged
  • RowChanging
  • RowDeleted
  • RowDeleting

We'll look at one of these events, RowChanged, to illustrate a typed event.

Defining the Delegate

First, we need a delegate of the appropriate type:

public delegate void PersonRowChangedDlgt(PersonTable sender, 
                     PersonRowChangedEventArgs args);

Note that this delegate defines typed parameters.

The Event

We can now add the event to the PersonTable class:

public event PersonRowChangedDlgt PersonRowChanged;

Defining the Event Argument Class

We also need a typed event argument class because we want to use our typed PersonRow:

public class PersonRowChangedEventArgs
{
  protected DataRowAction action;
  protected PersonRow row;

  public DataRowAction Action
  {
    get { return action; }
  }

  public PersonRow Row
  {
    get { return row; }
  }

  public PersonRowChangedEventArgs(DataRowAction action, PersonRow row)
  {
    this.action = action;
    this.row = row;
  }
}

Overriding the OnRowChanged Method

Rather than add a RowChanged event handler, we can override the OnRowChanged method and create a similar pattern for the new method OnPersonRowChanged. Note that we still call the base DataTable implementation for RowChanged. These methods are added to the PersonDataTable class.

protected override void OnRowChanged(DataRowChangeEventArgs e)
{
  base.OnRowChanged(e);
  PersonRowChangedEventArgs args = 
    new PersonRowChangedEventArgs(e.Action, (PersonRow)e.Row);
  OnPersonRowChanged(args); 
}

protected virtual void OnPersonRowChanged(PersonRowChangedEventArgs args)
{
  if (PersonRowChanged != null)
  {
    PersonRowChanged(this, args);
  }
}

Note that the above method is virtual, as this is the pattern for how events are raised in the .NET framework, and it's good to be consistent with this pattern.

Now, that's a lot of work to add just one typed event, so you can see that having a code generator would be really helpful.

Using the Event

Here's a silly example to illustrate using the typed DataTable and the event:

class Program
{
  static void Main(string[] args)
  {
    PersonTable table = new PersonTable();
    table.PersonRowChanged += new PersonRowChangedDlgt(OnPersonRowChanged);
    PersonRow row = table.GetNewRow();
    table.Add(row);
  }

  static void OnPersonRowChanged(PersonTable sender, PersonRowChangedEventArgs args)
  {
    // This is silly example only for the purposes of illustrating using typed events.
    // Do not do this in real applications, because you would never use this Changed event
    // to validate row fields!
    if (args.Row.LastName != String.Empty)
    {
      throw new ApplicationException("The row did not initialize " + 
                "to an empty string for the LastName field.");
    }
  }
}

This, however, illustrates the beauty of a typed DataTable and a typed DataRow: readability, and compiler checking of proper usage.

Conclusion

Hopefully, this article clearly illustrates how to create a typed DataTable manually. The "discovery" that I made (that I couldn't find anywhere else on the Internet) is that, when you override GetRowType(), you also need to override NewRowFromBuilder().

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Marc Clifton
United States United States
Member
Marc is the creator of two open source projets, MyXaml, a declarative (XML) instantiation engine and the Advanced Unit Testing framework, and Interacx, a commercial n-tier RAD application suite.  Visit his website, www.marcclifton.com, where you will find many of his articles and his blog.
 
Marc lives in Philmont, NY.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionI'm missing somethingmemberMember 960953821 Jan '13 - 4:22 
I really like the idea of this. Like you I don't like unnecessary code hanging around for a simple datatable so don't want to use a dataset. I also like trying to understand how these things work, rather than just accepting what VS does for me.
 
However I must be missing something. I've downloaded your example and tried to populate the persontable from an SqlDataAdapter the same way I would populate a datatable, then to use that datatable as the source of a datagridview.
 
I'm really a beginner so . I've worked in a non-object orientated programming language for 15 years but only been learning C# for a few months.
 
I have got quite used to using an SQLDataAdapter to populate the DataTable using the Fill method on the adapter, then using a BindingSource and a DataGridView to view the contents of the Datatable. However using the same technique for the PersonTable in your example gives me the correct rows, but no data in each field.
 
If I use a query with just the two columns it shows empty rows. If I use a query with extra columns it creates extra columns in the datatable/gridview and populates them correctly, but the first two columns still show as blank.
 
Can't work out what I am missing.
RosieC

GeneralSuggestion for Primary Key definitionmemberGuttorm Haaversen26 Oct '12 - 13:07 
Inside the PersonTable Constructor
 
DataColumn LastName =
   new DataColumn
      { ColumnName = "LastName"
      , DataType = typeof(string)   // Default string
      , MaxLength = 10                  // Default -1
      , Unique = true                     // Default false
      , AllowDBNull = false            // Default true
      , Caption = "LastName"         // Default is constructor value
      };
 
PrimaryKey = new DataColumn[] {LastName};
GeneralMy vote of 5mvpKanasz Robert26 Sep '12 - 7:39 
Very helpful article.
QuestionInstead I'd rather have the ide do the chore for me, I just drag and drop and make a few edit, much quicker.memberJ Xie10 Oct '11 - 16:28 
as the title explains.
AnswerRe: Instead I'd rather have the ide do the chore for me, I just drag and drop and make a few edit, much quicker.protectorMarc Clifton11 Oct '11 - 1:05 
Hmmm. Can you created a typed DataTable by d&d just the table? Doesn't it require a DataSet?
 
Marc

GeneralMy vote of 5memberKanasz Robert13 Nov '10 - 6:21 
Very good article and very very interesting idea.
GeneralNote about the NewRow() Methodmemberhybridguy6 Oct '09 - 7:34 
If you go the route of using the new keyword to hide the base implementation of NewRow as in...
public new PersonRow NewRow() {
    PersonRow row = (PersonRow)NewRow();
    return row;
}
and you happen to call this method as in...PersonRow pr = personTable.NewRow()
you will recurse into a big 'ol stackoverflow exception..... Frown | :(
You can use the authors preferred way and rename the method to not use the "new" keyword, or explicitly make a call to the base class as in...
public new PersonRow NewRow() {
    PersonRow row = (PersonRow)base.NewRow();
    return row;
}

 
me likes it

GeneralRe: Note about the NewRow() MethodmemberDan Neely1 Dec '09 - 8:46 
I just got found this as well. A bug fix version would be nice...
 
3x12=36
2x12=24
1x12=12
0x12=18

GeneralI'd give you a 6 if I could!membertorial10 Dec '08 - 6:32 
Your article helped me save lots of time!!
GeneralIsNullmemberY_R4 Nov '08 - 21:45 
Hi,
 
Great article, Helped my understand strong typed datasets and helped me with my current project.
 
Question:
When you are returning the fields you are not checking for null.
I have seen that visual studio adds the IsNull() (e.g. IsLastNameNull).
And if the value is null and you access it (row.LastName), it throws an exception.
 
Why is that done? isn't it better to just return null (especially when dealing with strings),
like you r code does? or maybe they do that for some good reason.
 
This behavior has made me a lots of problems (And a lot of extra code to deal with it).
 
Best regards,
Yaniv
 
Sincerely yours
Y.R.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 28 Oct 2008
Article Copyright 2008 by Marc Clifton
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid