first_page the funky knowledge base
personal notes from way, _way_ back and maybe today

C# Code in Cold Storage: A "Strongly-Typed" DataTable;

/// <summary> /// /// </summary> public class ListControlTable : DataTable,System.Collections.IEnumerable { /// <summary> /// /// </summary> public ListControlTable():base("ListControlTable") { this.InitClass(); }

    /// &lt;summary&gt;
    /// 
    /// &lt;/summary&gt;
    public DataColumn DisplayColumn
    {
        get{ return this.Columns[&quot;displayColumn&quot;]; }
    }

    /// &lt;summary&gt;
    /// 
    /// &lt;/summary&gt;
    public DataColumn ValueColumn
    {
        get{ return this.Columns[&quot;valueColumn&quot;]; }
    }

    /// &lt;summary&gt;
    /// 
    /// &lt;/summary&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    public System.Collections.IEnumerator GetEnumerator() 
    {
        return this.Rows.GetEnumerator();
    }

    /// &lt;summary&gt;
    /// 
    /// &lt;/summary&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    public override DataTable Clone() 
    {
        ListControlTable cln = (ListControlTable)base.Clone();
        return cln;
    }

    /// &lt;summary&gt;
    /// 
    /// &lt;/summary&gt;
    /// &lt;param name=&quot;SourceTable&quot;&gt;&lt;/param&gt;
    /// &lt;param name=&quot;ValueColumn&quot;&gt;&lt;/param&gt;
    /// &lt;param name=&quot;DisplayColumn&quot;&gt;&lt;/param&gt;
    /// &lt;param name=&quot;Refill&quot;&gt;&lt;/param&gt;
    /// &lt;param name=&quot;InsertNull&quot;&gt;&lt;/param&gt;
    /// &lt;param name=&quot;NullText&quot;&gt;&lt;/param&gt;
    public void Fill(DataTable SourceTable,DataColumn ValueColumn,
        DataColumn DisplayColumn,bool Refill,bool InsertNull,string NullText)
    {
        foreach(DataRow row in SourceTable.Rows)
        {
            object[] rowArray = new object[3];
            rowArray[0] = (row.ItemArray[ValueColumn.Ordinal].Equals(DBNull.Value))
                ? 0 : row.ItemArray[ValueColumn.Ordinal];
            rowArray[1] = row.ItemArray[ValueColumn.Ordinal];
            rowArray[2] = row.ItemArray[DisplayColumn.Ordinal];
            if(!Refill) this.Rows.Add(rowArray);
            else
            {
                this.BeginLoadData();
                this.LoadDataRow(rowArray,true);
                this.EndLoadData();
            }
        }

        if(InsertNull)
        {
            DataRow row = this.NewRow();
            this.Rows.Add(new object[] {0,DBNull.Value,NullText});
        }

        this.DefaultView.Sort = &quot;displayColumn&quot;;
    }

    /// &lt;summary&gt;
    /// 
    /// &lt;/summary&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    protected override DataTable CreateInstance() 
    {
        return new ListControlTable();
    }

    private void InitClass()
    {
        this.Columns.Add(new DataColumn(&quot;valueColumnKey&quot;,typeof(int),
            null,System.Data.MappingType.Element));
        this.Columns.Add(new DataColumn(&quot;valueColumn&quot;,typeof(int),
            null,System.Data.MappingType.Element));
        this.Columns.Add(new DataColumn(&quot;displayColumn&quot;,typeof(string),
            null,System.Data.MappingType.Element));

        //This is needed for the DataTable.LoadDataRow() method.
        this.PrimaryKey = new DataColumn[] {this.Columns[&quot;valueColumnKey&quot;]};
    }
}
mod date: 2004-07-10T00:09:34.000Z