473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

GridView formatting rows/columns question

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
2 2681
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: | last post by:
Hello, Does anyone have an idea on how I can filter the data in the gridview control that was returned by an sql query? I have a gridview that works fine when I populate it with data. Now I...
1
by: Miguel Dias Moura | last post by:
Hello, I have a GridView in my page which is created in runtime. It works fine. My page has 2 Asp Buttons: - The HIDE button makes GridView.Visible = False; - The SHOW button makes...
1
by: Giovanni | last post by:
Dear Friends/Gurus, I have exhausted myself and have yet no solution to the following: I have an ASP.NET 2.0 Survey type application. On a page, I have placed a GridView which is bound to an...
0
by: Innova | last post by:
Hi, We are working on a gridview inside the gridview (parent-child) scenario. The data of child grid will depend on the data of parent. Objectives: 1.Add new row in parent grid after each row...
4
by: Ken Wigle | last post by:
All, I would be very grateful for any help on this question. I have an application in asp.net 2.0 where I dynamically create a datatable and then bind that to a gridview. Unfortunately, the...
2
by: Jacksm | last post by:
How can I align an asp:table columns with gridview columns (the widths)? I have tried setting table.column(0).width = gridview.column(0).width at page_load but it doesn't work. Thanks in advance
5
by: NKaufman | last post by:
<asp:GridView ID="Basic" runat="server" DataKeyNames="QuestionID,isHeading" AutoGenerateColumns="false" AllowPaging="true" PageSize="100"> <Columns> <asp:BoundField Visible="false"...
1
by: Brian | last post by:
I currently have a GridView that displays information like: , Name, Address, City, State, Notes. Right now this information is displayed in columns but I would like it if each row in the table...
3
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I have a gridview with several template columns and a few command button columns, column 1 and 2 are dropdown boxes, column 3 is a checkbox and column 4 is a text box, followed by command button...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.