?
Solved

Pull column names from a DataView or DataRowCollection?

Posted on 2005-03-14
4
Medium Priority
?
1,073 Views
Last Modified: 2012-08-14
If I create a DataView or DataRowCollection from a SQL Query, is there a way to determine the column names that are contained therein?  Example:

    DataSet ds = new DataSet();
    OdbcDataAdapter da = new OdbcDataAdapter("SELECT * FROM myTable, conn);
    da.Fill(ds);
    DataRowCollection drc = ds.Tables[0].Rows;

Is there a way to determine what column names were returned from the SQL Select statement?
0
Comment
Question by:hendridm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 6

Expert Comment

by:Fahdmurtaza
ID: 13538870
Hi hendridm,

Well you shouldcopy them to an array of strings and thats all.

Regards,
Fahd Murtaza
0
 
LVL 8

Author Comment

by:hendridm
ID: 13538928
Uhm, I'm not sure I understand.  Copy what to an array of strings?  Where do I get the column names from to begin with?

I know I can use a SQL Statement like so to retrieve the column names:
SELECT name FROM syscolumns WHERE id = (SELECT id FROM sysobjects WHERE name='myTableName')

However, I was hoping there was a way to pull them from the DataRowCollection, DataView, or DataSet, similar to how the DataGrid control does with 'AutoGenerateColumns'.
0
 
LVL 8

Author Comment

by:hendridm
ID: 13539100
I figured it out for a DataView:

    DataSet ds = new DataSet();
    OdbcDataAdapter da = new OdbcDataAdapter("SELECT * FROM myTable", conn);
    da.Fill(ds);
    DataView dv = ds.Tables[0].DefaultView;

    foreach( DataColumn c in dv.Table.Columns)
    {
        Response.Write(c.ToString()+"<br>");
    }
0
 
LVL 4

Accepted Solution

by:
jaynus earned 1300 total points
ID: 13539359
Its the same for DataView, DataGrid, and DataSet.

DataRow's do not contain column definitions, only an ItemArray and internal specifications to the column formats, Columns must be pulled from a view, dataset, columncollection, or grid.

0

Featured Post

Introducing Priority Question

Increase expert visibility of your issues by participating in Priority Question, our latest feature for Premium and Team Account holders. Adjust the priority of your question to get emergent issues in front of subject-matter experts for help when you need it most.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question