Next problem :) GridView - formatting text

J

JamesB

OK, so I now have a gridview on my page. Because my data isn't stored in
such a way that I can bind the grid directly to the database, I am creating
a DataTable in code, running some procedures to fill this with data in the
right format from the database, all processed accordingly, then binding the
DataTable to the grid.
This works, but newline characters (i.e. "\n") don't work. I also tried
putting them in as <br> but no joy.

Doing some googling, I have read that I need to use "TemplateFields" on the
grid. If I go to the Column Editor for my grid, I don't have any columns to
edit (they are generated at runtime by the binding to my datatable I guess)
so I can't convert them to template ones.

Any thoughts? I tried adding some columns and setting the
AutoGenerateColumns to false, but then my data doesn't appear so I must be
missing some other step...!
 
P

Phil H

OK, so I now have a gridview on my page. Because my data isn't stored in
such a way that I can bind the grid directly to the database, I am creating
a DataTable in code, running some procedures to fill this with data in the
right format from the database, all processed accordingly, then binding the
DataTable to the grid.
This works, but newline characters (i.e. "\n") don't work. I also tried
putting them in as <br> but no joy.

Doing some googling, I have read that I need to use "TemplateFields" on the
grid. If I go to the Column Editor for my grid, I don't have any columns to
edit (they are generated at runtime by the binding to my datatable I guess)
so I can't convert them to template ones.

Any thoughts? I tried adding some columns and setting the
AutoGenerateColumns to false, but then my data doesn't appear so I must be
missing some other step...!

Hi Jeremy

In the circumstances you describe the best alternative is to use an
ObjectDataSource linked to a class file (placed in the App_Code
folder) containing your data construction code in the form of a public
function that returns the resulting DataTable, and then set the
ObjectDataSource as the DataSourceID property for the GridView
control. The designer wizard for setting up templated databound
controls will not automatically list the column names, but if template
columns are added manually, and the source column names typed in
correctly, it will work Ok at run time. Also, if automatic column
generation is set to true, that should work Ok at run time as well.
There are many other steps to setting this up fully which are too
numerous to describe here, but if you search the help files you should
find what you need.

HTH
Phil
 
J

John Padilla

heremy,

if you know what fields your query will return, you can just use the Column
Editor and add the fields into there manually. This pre-builds the grid
columns for you. You can even convert these fields into template fields. You
will need to read up on <' % Eval("ColumnName") %'> and <'% Bind("ColunName"
%'> so that the grid knows what to bind into each column from your
datasource.

Question for you: why not use stored proceedures and use the DataSet
designer to do all of the heavy lifting for you? you can ass multiple table
adapters for your entire project and in the tableadapters u have built in
support for C.R.U.D. queries. Once u add the table adapter, u can bind to
that via a objectdatasource and volah the columns appear in your grid at
designtime and you also get sorting and paging for free plus Editing,
Deleting and Inserting.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top