?
Solved

Pull column names from a DataView or DataRowCollection?

Posted on 2005-03-14
4
Medium Priority
?
1,081 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
  • 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

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
Suggested Courses
Course of the Month4 days, 23 hours left to enroll

601 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