Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / DataGrid / August 2006

Tip: Looking for answers? Try searching our database.

Sortable column on DataGrid -- column header not displaying

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Teej - 26 Jul 2006 20:18 GMT
Good afternoon,

General goal:
I am working on a Web application (C#) in which the first screen is
simple enough: it displays a DataGrid based on data from a SQL Server
database.  One of the columns in the db is a DateTime column, which of
course has the date and time displayed in each record.  In the
DataGrid, I only want the date, and not the time, displayed for each
record, which I am successfully able to do by parsing out the date
portion of the record in the ItemDataBound event handler.  This is also
a sortable column, but I haven't yet implemented any sort code in the
code-behind; all I did was put an empty sort method in.

Problem(s):
As mentioned, I can get the date parsed out correctly, but at the same
time, the column header for this column is blank.  If I clicked on any
of the other sortable column headers, the column header in question
appears after the postback.  Here is a little bit of code from the
ItemDataBound event handler and some things I noticed through
trialing-and-erroring (with "Created" being the column header text):

    if (e.Item.Cells[1].Text != "Created")
    {
        string createdDate = e.Item.Cells[1].Text.ToString();
        e.Item.Cells[1].Text = createdDate.Substring(0, createdDate.IndexOf("
") + 1);
    }

1.  If I comment out the above code, the date is obviously displayed in
full form, and the column header is displayed, BUT not sortable.
2.  If I remove the SortExpression attribute from the HTML portion, the
column header is displayed and the date is truncated correctly, but
obviously not sortable.

As I'm typing this, I am realizing there appears to be some kind of
connection between the header not being displayed and the
SortExpression attribute existing.  I will keep researching, but does
anyone have any more insight on this, or might someone be able to
suggest any more steps I could take toward solving this?  If so, I
would greatly appreciate it.  Thanks for your consideration!

Sincerely,
Tim
Teej - 26 Jul 2006 20:23 GMT
My apologies...the way the post came out separated a single line of
code into two, and it may be hard to determine what I'm searching for
in the IndexOf() method, but it's a single space (" ").  Sorry!

Tim

> Good afternoon,
>
[quoted text clipped - 39 lines]
> Sincerely,
> Tim
Teej - 15 Aug 2006 19:23 GMT
Update - I resolved this.  The first row for that cell actually
contained an empty string, so I included that in my conditional
statement.  So now it looks like this:

        if (e.Item.Cells[1].Text != "Created" && e.Item.Cells[1].Text != "")
        {
            string receivedDate = e.Item.Cells[1].Text.ToString();
            string receivedDateMod = receivedDate.Substring(0,
receivedDate.IndexOf(" ") + 1);
            e.Item.Cells[1].Text = receivedDateMod;
        }

Now, as was desired, the "Created" column header displays with only the
date (not the time) displayed in each row.

Thanks,
Tim

> Good afternoon,
>
[quoted text clipped - 39 lines]
> Sincerely,
> Tim

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.