Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Articles
Search: 

Bonnie Berent's Tips
Bonnie DeWitt, December 1, 2006
Great tips for .NET developers
Summary
Great tips for .NET developers
Description

You'd think that writing a Tips column would be easy ... especially when all the Tips I use come from questions that have actually been posted and answered on the Universal Thread.

But here's the hard part ... trying to decide which questions should be used here. You see, some questions are so basic to long-time .NET developers, that it's easy to forget that not everyone already knows the answer.

So, this month's column will be dedicated to those basic kinds of questions. Enjoy.

How To Loop Through Enums
Here's an easy way to iterate through an Enumerator:
public class GetNamesTest {
    enum Colors { Red, Green, Blue, Yellow };
    enum Styles { Plaid, Striped, Tartan, Corduroy };

    public static void Main() {

        Console.WriteLine("The values of the Colors Enum are:");
        foreach(string s in Enum.GetNames(typeof(Colors)))
            Console.WriteLine(s);

        Console.WriteLine();

        Console.WriteLine("The values of the Styles Enum are:");
        foreach(string s in Enum.GetNames(typeof(Styles)))
            Console.WriteLine(s);
    }
}

from a solution provided by Neil Tonkin in Message #1089743

DataSets and NULL values
One question I run into a lot is how to check for a NULL value in a column of a DataTable. Here's how with a regular DataSet:
if (MyDataSet.Tables["MyTable"].Rows[0]["MyColumn"] == DBNull.Value)
	MessageBox.Show("Column is NULL");
Now, you could also do the same thing with a Typed DataSet:
if (MyDataSet.MyTable[0]["MyColumn"] == DBNull.Value)
	MessageBox.Show("Column is NULL");
But notice how this sort of defeats part of the niceties of using Typed DataSets, since you have to know the ColumnName you're checking instead of using the Typed DataSet's Intellisense.

Well, that's because what you should have used is the IsNull capabilities of the Typed DataSet's generated code. Here's the simpler version:

if (MyDataSet.MyTable[0].IsMyColumnNull())
	MessageBox.Show("Column is NULL");

from a solution provided by Einar Kvandahl in Message #1102122

Web Pages and a Default "Enter" button
How do you design a Web page that doesn't have the default behavior for the Enter key? In other words, when the user hits the Enter key, you either don't want a default button or you want to trap the keypress.

Add the following javascript to the page:

function fnTrapKD(btn) {
    if (document.all) {
        if (event.keyCode == 13) {
                event.returnValue=false;
                event.cancel = true;
                if (btn != null) btn.click();
        }
    }
}
And add the following attribute to the FORM tag:
<FORM ...  onkeydown="fnTrapKD()"  />

from a solution provided by Keith Payne in Message #1161903

Multiple Grids with Multiple Related Tables
Say you have three or four related tables, and you want to display each table in a separate Windows DataGrid and of course, have the relationships be respected in each Grid. Here's how to do it:

First, assume the following DataSet and relationships:

dataset: myDS
relation 1: relationTable1_Table2
relation 2: relationTable2_Table3
relation 3: relationTable3_Table4

Then, your DataGrid bindings would be as follows:

dataGrid1.SetDataBinding(myDS,"Table1");
dataGrid2.SetDataBinding(myDS,"Table1.relationTable1_Table2");
dataGrid3.SetDataBinding(myDS,"Table1.relationTable1_Table2.relationTable2_Table3");
dataGrid4.SetDataBinding(myDS,"Table1.relationTable1_Table2.relationTable2_Table3.relationTable3_Table4");

from a solution provided by Çetin Basöz in Message #1069287

Bonnie DeWitt, Geneva Systems Group
Bonnie is currently one of the principals of Geneva Systems Group. Call her the Senior Software Engineer, or even call her the VP of Engineering. She has no official title at the moment. Bonnie has been writing software in various languages for about 30 years. Bonnie's current focus on C# .NET applications began in early 2002. She has been a Microsoft C# MVP since Oct 2003 and an active member of the .NET online community.
More articles from this author
Bonnie DeWitt, September 1, 2005
Great tips for .NET developers
Bonnie DeWitt, October 1, 2005
Great tips for .NET developers
Bonnie DeWitt, November 1, 2005
Great tips for .NET developers
Bonnie DeWitt, December 1, 2005
Great tips for .NET developers
Bonnie DeWitt, April 1, 2009
Great tips from the .NET developer community compiled by Bonnie Berent.
Bonnie DeWitt, February 1, 2006
Great tips for .NET developers
Bonnie DeWitt, March 1, 2006
Great tips for .NET developers
Bonnie DeWitt, April 1, 2006
Great tips for .NET developers
Bonnie DeWitt, May 1, 2006
Great tips for .NET developers
Bonnie DeWitt, June 1, 2006
Great tips for .NET developers
Bonnie DeWitt, July 1, 2006
Great tips for .NET developers
Bonnie DeWitt, August 1, 2006
Great tips for .NET developers
Bonnie DeWitt, September 1, 2006
Great tips for .NET developers
Bonnie DeWitt, October 1, 2006
Great tips for .NET developers
Bonnie DeWitt, November 1, 2006
Great tips for .NET developers
Bonnie DeWitt, January 1, 2007
Great tips for .NET developers
Bonnie DeWitt, February 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, March 1, 2006
Great tips for .NET developers.
Bonnie DeWitt, April 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, March 1, 2007
Good tips for .NET developers.
Bonnie DeWitt, May 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, June 1, 2007
Great tips and tricks for .NET developers.
Bonnie DeWitt, July 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, August 1, 2007
Great tips for .NET developers
Bonnie DeWitt, September 1, 2007
Great tips for .NET developers.
Bonnie DeWitt, February 1, 2008
Great tips from the Universal Thread .NET community.
Bonnie DeWitt, March 1, 2008
Great tips for .NET developers selected from the community by Bonnie Berent.
Bonnie DeWitt, April 1, 2008
Great tips from the .NET developer community compiled by Bonnie Berent.
Bonnie DeWitt, January 1, 2006
Great tips for .NET developers