By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,373 Members | 1,305 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,373 IT Pros & Developers. It's quick & easy.

GridView formatting rows/columns question

P: n/a
Hi

I've been looking into different ways of formatting columns/rows in the
GridView control. I realize now, I think, that once the datasource is bound
to the control the original column type information is lost, i.e. converted
to a string.

So my question is: is it better to format data while it's still in a
DataTable, for example, then bind it to the control, or format the strings
in the BoundField elements, or if more flexibility is needed, in the
RowDataBound event?

If I do the later then I end up doing things like this:

cell = (DataControlFieldCell)(e.Row.Controls[MYDATE_COLUMN])
cell.Text = cell.Text.Substring(0, 16)

Or converting the string to a date and then formatting the date back to a
string.

I'm sure there are better ways so I'm just wondering if anyone has some good
experience with these kinds of issues?

Thanks

Andrew

Mar 23 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
If you want to format the output you could use the DataFormatString property
of the relevent column within the GridViewControl. You could also use the
CAST and CONVERT funtions in your Select Query so that the data is already
formatted the way you require.

"J055" wrote:
Hi

I've been looking into different ways of formatting columns/rows in the
GridView control. I realize now, I think, that once the datasource is bound
to the control the original column type information is lost, i.e. converted
to a string.

So my question is: is it better to format data while it's still in a
DataTable, for example, then bind it to the control, or format the strings
in the BoundField elements, or if more flexibility is needed, in the
RowDataBound event?

If I do the later then I end up doing things like this:

cell = (DataControlFieldCell)(e.Row.Controls[MYDATE_COLUMN])
cell.Text = cell.Text.Substring(0, 16)

Or converting the string to a date and then formatting the date back to a
string.

I'm sure there are better ways so I'm just wondering if anyone has some good
experience with these kinds of issues?

Thanks

Andrew

Mar 23 '06 #2

P: n/a
Hi Andrew,

As for data formatting, I always think it should be done in databound
control(for asp.net application) since such formatting is the work in
presentation layer, while datatable is the component in data accessing
layer. It's better to separate them so as to make the different layer loose
coupled.
Regards,

Steven Cheng
Microsoft Online Community Support
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may

learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Mar 24 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.