RWF

Is it possible How do i create my own class which will be inheriting all the features of DataRow so I can expand it to have a Tag property

Re: .NET Framework Data Access and Storage Inheriting from DataRow

Paul Louth

public class MyDataRow : DataRow
{
    public MyDataRow(DataRowBuilder builder)
        : base(builder)
    {
    }

    // your code here

}





Re: .NET Framework Data Access and Storage Inheriting from DataRow

RWF

DataRowBuilder cannot be instantiated from code



Re: .NET Framework Data Access and Storage Inheriting from DataRow

Paul Louth

Yep, check my example code below.  You must create a DataTable companion with an overloaded NewRowFromBuilder method.  As the DataRow must be created via NewRow:

 /// <summary>
 /// Pop3 message header
 /// </summary>
 public class Pop3MessageHeader : DataRow
 {
  Pop3MessageHeaderTable parent;

  /// <summary>
  /// CTor
  /// </summary>
  /// <param name="rowBuilder">Row builder object to use when building this row</param>
  internal Pop3MessageHeader(DataRowBuilder rowBuilder) : base(rowBuilder)
  {
   parent = (Pop3MessageHeaderTable)this.Table;
  }

  public long Number
  {
   get
   {
    return (long)this[parent.NumberColumn];
   }
   set
   {
    this[parent.NumberColumn] = value;
   }
  }

  public long Bytes
  {
   get
   {
    return (long)this[parent.BytesColumn];
   }
   set
   {
    this[parent.BytesColumn] = value;
   }
  }

  public string Subject
  {
   get
   {
    return (string)this[parent.SubjectColumn];
   }
   set
   {
    this[parent.SubjectColumn] = value;
   }
  }

  public string To
  {
   get
   {
    return (string)this[parent.ToColumn];
   }
   set
   {
    this[parent.ToColumn] = value;
   }
  }

  public string ID
  {
   get
   {
    return (string)this[parent.IDColumn];
   }
   set
   {
    this[parent.IDColumn] = value;
   }
  }

  public DateTime Date
  {
   get
   {
    return (DateTime)this[parent.DateColumn];
   }
   set
   {
    this[parent.DateColumn] = value;
   }
  }

  public string From
  {
   get
   {
    return (string)this[parent.FromColumn];
   }
   set
   {
    this[parent.FromColumn] = value;
   }
  }

  public bool Attachments
  {
   get
   {
    return (bool)this[parent.AttachmentsColumn];
   }
   set
   {
    this[parent.AttachmentsColumn] = value;
   }
  } 
 }

 /// <summary>
 /// Pop 3 message table
 /// </summary>
 public class Pop3MessageHeaderTable : DataTable
 {
  DataColumn numberColumn;
  DataColumn bytesColumn;
  DataColumn subjectColumn;
  DataColumn toColumn;
  DataColumn idColumn;
  DataColumn dateColumn;
  DataColumn fromColumn;
  DataColumn attachmentsColumn;

  public Pop3MessageHeaderTable()
  {
   subjectColumn = new DataColumn("Subject",typeof(System.String), null, MappingType.Element);
   Columns.Add(subjectColumn);

   toColumn = new DataColumn("To",typeof(System.String), null, MappingType.Element);
   Columns.Add(toColumn);

   idColumn = new DataColumn("ID",typeof(System.String), null, MappingType.Element);
   Columns.Add(idColumn);

   dateColumn = new DataColumn("Date",typeof(DateTime), null, MappingType.Element);
   Columns.Add(dateColumn);

   fromColumn = new DataColumn("From",typeof(System.String), null, MappingType.Element);
   Columns.Add(fromColumn);

   numberColumn = new DataColumn("Number",typeof(long), null, MappingType.Element);
   Columns.Add(numberColumn);

   bytesColumn = new DataColumn("Bytes",typeof(long), null, MappingType.Element);
   Columns.Add(bytesColumn);

   attachmentsColumn = new DataColumn("Attachments",typeof(bool), null, MappingType.Element);
   Columns.Add(attachmentsColumn);
  }

  public DataColumn AttachmentsColumn
  {
   get
   {
    return attachmentsColumn;
   }
  }

  public DataColumn NumberColumn
  {
   get
   {
    return numberColumn;
   }
  }

  public DataColumn BytesColumn
  {
   get
   {
    return bytesColumn;
   }
  }

  public DataColumn SubjectColumn
  {
   get
   {
    return subjectColumn;
   }
  }

  public DataColumn ToColumn
  {
   get
   {
    return toColumn;
   }
  }

  public DataColumn IDColumn
  {
   get
   {
    return idColumn;
   }
  }

  public DataColumn DateColumn
  {
   get
   {
    return dateColumn;
   }
  }

  public DataColumn FromColumn
  {
   get
   {
    return fromColumn;
   }
  }

  /// <summary>
  /// Indexer of this strong typed list
  /// </summary>
  public Pop3MessageHeader this[int index]
  {
   get
   {
    return ((Pop3MessageHeader)(this.Rows[index]));
   }
  }

  /// <summary>
  /// Creates a new typed row
  /// </summary>
  /// <param name="rowBuilder">supplied row builder to pass to the typed row</param>
  /// <returns>the new typed datarow</returns>
  protected override DataRow NewRowFromBuilder(DataRowBuilder rowBuilder)
  {
   return new Pop3MessageHeader(rowBuilder);
  }

  /// <summary>
  /// Return the type of the typed datarow
  /// </summary>
  /// <returns>returns the requested type</returns>
  protected override Type GetRowType()
  {
   return typeof(Pop3MessageHeader);
  } 
 }
}





Re: .NET Framework Data Access and Storage Inheriting from DataRow

RWF

Thank you!



Re: .NET Framework Data Access and Storage Inheriting from DataRow

Paul Louth

No problem, glad to help! 



Re: .NET Framework Data Access and Storage Inheriting from DataRow

Mikey Likes It

I'm missing something. What is the purpose of a RowBuilder - to initialize the row's item values to some defaults, or to provide the typed and appropriately named accessors that go from name-to-ordinal of the item[]