Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading DataReader when field names are unknown.

Status
Not open for further replies.

qwert231

Programmer
Sep 4, 2001
756
0
0
US
I want to make a generic tool that formats a query into a table. (This may be something I can do with a DataGrid, but I can't see how.)

I am going to build this control that takes 2 things, the query/Sql command, and the number of columns.

Let's say I have a query that returns 3 fields, and I want that table to go into 2 columns, like this:
Field1 Field2 Field3 Field1 Field2 Field3

Or, 1 field name and 3 colums:
FieldX FieldX FieldX

Basically, I want to do this regardless of how many fields or what their names are.
In ASP classic, I could do something like this:
Code:
For each fldF in recSet.Fields
 Response.Write &quot;<td>&quot; & fldF.Value & &quot;</td>&quot;
Next

I got most of the principles set, but I want to know how I could do something like that For Each statement.

If you can show me how to do that in DataGrids that would be even better. (If it is real simple, then forgive me, I am a newbie to .Net.)
 
Oh my, yes. This is where the datagrid shines.

I'll assume you know how to get the datareader, and will omit that logic, but once you have it, simply drag a datagrid onto your page from the toolbox, and then in the codebehind:

~~~~~
myDataGrid.DataSource = myDataReader
myDataGrid.DataBind()
~~~~~

That's it. **wipes hands**

ALL HAIL ASP.NET!!!

;-)
paul

ps. obviously, you can get alot more complicated than that, but that code right there will take your existing column names, plug those into a header row, and then iterate over your datareader spitting out table rows as it goes. has a good tutorial on the datagrid. I think you'd find it very helpful.


it's 4 parts, I think.
penny1.gif
penny1.gif
 
Erm...before anything else I would check what type of functionality you wish to present with the grid, if its simply to display data then first make sure the data being returned to the reader is legible, i.e. no ID type fields, so use a 'view' of your data and your 'sorted'!

However if you need to edit the data and if you want to use custom item templates but have no previous knowledge of the data schema then all of a sudden you have a load more work to do - you will need to custom build the grid using dynamic item templates to correctly render each field/ datagrid template column (investigate the LoadTemplate method for further ideas here)

This will mean walking through the fields of the data returned, a cool way to do this before requesting the data is to use the GetSchema method. Once you have the grid defined as per your schema then it IS as easy as passing the data to the grid (setting the datasource property) and then binding :)

Recommended reading/research - check out articles on aspalliance, wintellect (especially Dino Esposito) and of course DotNetJunkies as well as p2p.wrox.com

Rgds.,

Ed
 
Hmmm... I'll look into this, of course, it looks like I will still get something like this:
Field1 Field2 Field3
Record1
Record2
Record3
Record4
Record5
Record6
Record7
Record8

When what I want to do is this:
Field1 Field2 Field3 Field1 Field2 Field3
Record1 Record5
Record2 Record6
Record3 Record7
Record4 Record8

Instead of the table running down one column, I have two columns for the same table (even tho both columns list 3 fields).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top