Sortable column on DataGrid -- column header not displaying

T

Teej

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
 
T

Teej

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
 
T

Teej

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
 

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,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top