none
Set the Column Attribute of System.Data.DataTable once and use it many times RRS feed

  • Question

  • I have problem here I want to use the DataTable in whose Column Attributtes are set and I don't want to set them again and again..

    private System.Data.DataTable PromoTable = new System.Data.DataTable();

    and to set the attributes for this table I'm calling function like this..

    public bool setPromoTableAttributes()
            {
                string[] DColumns = new string[] { "PoneId", "EclipseId", "Country", "BizGroup", "PromoType", "ProgramType" };
                for (int i = 0; i < DColumns.Length; i++)
                {
                    PromoTable.Columns.Add(DColumns[i],Type.GetType("System.String"));
                }
                DColumns = new string[] { "StartDate", "EndDate" };
                for (int i = 0; i < DColumns.Length; i++)
                {
                    PromoTable.Columns.Add(DColumns[i],Type.GetType("System.DateTime"));
                }
                return true;
            }

    I will be creating PromoTable 100 times and I don't want to set the attributes again and again..

    can any one give me work around for this one ..?



    $Kracker

    Friday, March 16, 2012 5:06 AM

Answers

  • There is no need to set them 100 times. therefore is once decided that what is currently in fact not so well OO.

    A datatable has two important collections.

    DataColumns 
    DataRows

    The Columns describe the items which are used in the rows. Therefore all rows are depending from the collection DataColumns, that is not nice in OO but it is very effective in this case.

    If you use 1000000000000 rows, they all use the column description you have created.

    Be aware it is no spreadsheet, you cannot have less, other or more item types in a datatable in different rows all rows use the same itemtype for a column.


    Success
    Cor

    • Marked as answer by SKracker Friday, March 16, 2012 12:42 PM
    Friday, March 16, 2012 7:29 AM
  • I have problem here I want to use the DataTable in whose Column Attributtes are set and I don't want to set them again and again..

    Hi,

    why not? It needs to be done anyhow; you could create a subclass of type datatable, and run this code in the constructor.

    You'd be on your way to create a self-made typed dataset. 

    So you could also create a typed dataset in a dataset designer, thus creating a similar kind of dataset subclass. 

    In essence they all do the same thing: they start out with a basic datatable (dataset) and start attributing it.


    Regards, Nico

    • Marked as answer by SKracker Friday, March 16, 2012 12:42 PM
    Friday, March 16, 2012 9:33 AM

All replies

  • There is no need to set them 100 times. therefore is once decided that what is currently in fact not so well OO.

    A datatable has two important collections.

    DataColumns 
    DataRows

    The Columns describe the items which are used in the rows. Therefore all rows are depending from the collection DataColumns, that is not nice in OO but it is very effective in this case.

    If you use 1000000000000 rows, they all use the column description you have created.

    Be aware it is no spreadsheet, you cannot have less, other or more item types in a datatable in different rows all rows use the same itemtype for a column.


    Success
    Cor

    • Marked as answer by SKracker Friday, March 16, 2012 12:42 PM
    Friday, March 16, 2012 7:29 AM
  • I have problem here I want to use the DataTable in whose Column Attributtes are set and I don't want to set them again and again..

    Hi,

    why not? It needs to be done anyhow; you could create a subclass of type datatable, and run this code in the constructor.

    You'd be on your way to create a self-made typed dataset. 

    So you could also create a typed dataset in a dataset designer, thus creating a similar kind of dataset subclass. 

    In essence they all do the same thing: they start out with a basic datatable (dataset) and start attributing it.


    Regards, Nico

    • Marked as answer by SKracker Friday, March 16, 2012 12:42 PM
    Friday, March 16, 2012 9:33 AM
  • In essence they all do the same thing: they start out with a basic datatable (dataset) and start attributing it.

    Regards, Nico

    After reading above I felt like "All my effort seems to be in vain" because I created a typed DataTable...

    but when I thought about it even the Typed Data such as List<String>=new List<string>();

    does the same .... create list object and set the string as attribute....

    then I felt "In essence they all do the same thing: they start out with a basic datatable (dataset) and start attributing it." is Truth....

    Anyhow all wanted is something like DataTable(Column,Column,Column,Column)

    thanks Cor Lighthert and NicoBoey for putting some OO sense to me....


    $Kracker

    • Edited by SKracker Saturday, March 17, 2012 4:28 AM
    Friday, March 16, 2012 12:48 PM
  • So did you end up using the DataSet Designer to create an .xsd and a Typed DataSet? You should definitely go with Typed DataSets, IMHO.

    You can even create an .xsd without using the DataSet Designer (an .xsd is, after all, simply XML). I wrote a blog post about creating an .xsd based on a Stored Proc (or simply queries) to a database if you're interested:

    http://geek-goddess-bonnie.blogspot.com/2010/04/create-xsd.html


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    Friday, March 16, 2012 2:54 PM
  • I "created a subclass of type datatable, and ran the code in the constructor."

    $Kracker

    Saturday, March 17, 2012 4:30 AM
  • OK ... that'll work.

    I still think a Typed DataSet would be better. Easier too (at least I think so). Here's what the .xsd file would look like for your particular DataSet:

    <?xml version="1.0" standalone="yes" ?>
    <xs:schema id="MyDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
      <xs:element name="MyDataSet" msdata:IsDataSet="true">
        <xs:complexType>
          <xs:choice maxOccurs="unbounded">
            <xs:element name="MyTable">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="PoneId" type="xs:string" minOccurs="0" />
                  <xs:element name="EclipseId" type="xs:string" minOccurs="0" />
                  <xs:element name="Country" type="xs:string" minOccurs="0" />
                  <xs:element name="BizGroup" type="xs:string" minOccurs="0" />
                  <xs:element name="PromoType" type="xs:string" minOccurs="0" />
                  <xs:element name="ProgramType" type="xs:string" minOccurs="0" />
                  <xs:element name="StartDate" type="xs:dateTime" minOccurs="0" />
                  <xs:element name="EndDate" type="xs:dateTime" minOccurs="0" />
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:choice>
        </xs:complexType>
      </xs:element>
    </xs:schema>

    OK, so maybe it looks like a lot of gibberish, but it's really easier in the long run. Plus when the Typed DataSet gets generated, all your columns are typed. So the syntax for accessing them is this:

    DateTime MyDate = ds.MyTable[0].StartDate;

    ... instead of this:

    DateTime MyDate = (DateTime)ds.Tables["MyTable"].Rows[0]["StartDate"]

    Better, cleaner syntax ... wouldn't you agree?


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    Saturday, March 17, 2012 4:29 PM