Results 1 to 10 of 10

Thread: [RESOLVED] DataRow column count

  1. #1

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Resolved [RESOLVED] DataRow column count

    How can i get the number of columns in A DataRow type?

    Thanks.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: DataRow column count

    vb.net Code:
    1. MyDataRow.ItemArray.Length

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: DataRow column count

    Quote Originally Posted by ForumAccount View Post
    vb.net Code:
    1. MyDataRow.ItemArray.Length
    I'd avoid that because it actually gets the data to determine the number of fields. I'd tend to go with myDataRow.Table.Columns.Count. That said, that will not work on rows that have been created but not added to the table.

  4. #4

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: DataRow column count

    First of all thank you for the answer, but just before you post it I've found another way to achieve this target :
    Code:
    MyDataRow.Table.Columns.Count
    is there any difference?
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  5. #5

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: DataRow column count

    JM I was typing my post while you wrote your's, so the second methods is better?
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  6. #6
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: DataRow column count

    I'm confused, you say avoid it because it gets the data, but the ItemArray is just a property same as Table and Columns? Is it not just getting some private level variable same as .Table and .Columns?

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: DataRow column count

    Quote Originally Posted by motil View Post
    JM I was typing my post while you wrote your's, so the second methods is better?
    All in all it's probably much of a muchness. As I said, FA's approach actually creates an array containing the data to determine how many fields there are, which is not needed unless you're going to use the data. The other approach is more efficient I think you'll find, although the difference will be insignificant. I was also wrong about it not working with Detached rows.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: DataRow column count

    Quote Originally Posted by ForumAccount View Post
    I'm confused, you say avoid it because it gets the data, but the ItemArray is just a property same as Table and Columns? Is it not just getting some private level variable same as .Table and .Columns?
    No, it doesn't. This is courtesy of Reflector:
    vb.net Code:
    1. Public Property ItemArray As Object()
    2.     Get
    3.         Dim defaultRecord As Integer = Me.GetDefaultRecord
    4.         Dim objArray As Object() = New Object(Me._columns.Count  - 1) {}
    5.         Dim i As Integer
    6.         For i = 0 To objArray.Length - 1
    7.             Dim column As DataColumn = Me._columns.Item(i)
    8.             objArray(i) = column.Item(defaultRecord)
    9.         Next i
    10.         Return objArray
    11.     End Get

  9. #9
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: DataRow column count

    Hmm... interesting!

  10. #10

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: DataRow column count

    Ok, Thanks a lot guys
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width