0

I have a weird problem on my hand. I am trying to read an oracle database and using some queries to get info back. I have a datagridview with column names already set at the beginning of the program. Here is an example setup of my datagridview columns

Number   Word   Sentence   Paragraph

Now i am reading a database and selecting its columns named N S P representing Number Sentence and Paragraph respectively. How can i load the results of the query into a datatable and display the contents under my datagridview so the N contents in the database are displayed under the Number column and nothing under word column etc. I can always query by selecting N, S and P one at a time, but i want to load all the data at once.

THanks

David Hall
  • 32,624
  • 10
  • 90
  • 127
hWorld
  • 185
  • 2
  • 3
  • 9
  • I cant exactly post my code here. But its just a test i am running that reads a database using oledb and uses an sql query that looks like: command = new OleDbCommand("SELECT N, S, P FROM dFile WHERE N = 1"). THe only way i know how to store the result is using dt.Load(command.ExecuteReader()); but that loads the datatable with the column names and the set order of column like N then S then P – hWorld Jun 16 '11 at 14:50

1 Answers1

0

You can change the column names in the SQL by writing

SELECT N AS Number, S AS Sentence, ...
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Its telling me an error that select statement includes a reserved word. How do you fix this? – hWorld Jun 16 '11 at 15:13
  • Have a look at this online reserved word checker http://www.petefreitag.com/tools/sql_reserved_words_checker/ – markpsmith Jun 16 '11 at 15:17
  • number is reserved... how do i fix the query? – hWorld Jun 16 '11 at 15:22
  • awesome, now can you selectively fill a datatable instead of just dumping all the result from the query into it? – hWorld Jun 16 '11 at 15:29
  • Do you mean you only want data from certain columns? – markpsmith Jun 16 '11 at 15:33
  • The datatable only holds info for N S and P but not for W, In the datagridview under form, the order of columns should be N, W, S, P but if i just load all the info in the datatable to the gridview, the W column between N and S will be missing – hWorld Jun 16 '11 at 15:35
  • Your query doesn't have W in the select, is that the problem? – markpsmith Jun 16 '11 at 15:41
  • w can come from any other source such as an arraylist holding the hard coded values during the program execution – hWorld Jun 16 '11 at 15:41
  • You can call `table.Columns.Add`, then loop through the rows and set your new column. – SLaks Jun 16 '11 at 15:43
  • instructions:http://stackoverflow.com/questions/351557/how-does-one-insert-a-column-into-a-dataset-between-two-existing-columns – markpsmith Jun 16 '11 at 15:53
  • nice! one kinda related question. How can you resize a column header to fit the header name? – hWorld Jun 16 '11 at 15:58