Sortable column on DataGrid -- column header not displaying

Discussion in 'ASP .Net Datagrid Control' started by Teej, Jul 26, 2006.

  1. Teej

    Teej Guest

    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, Jul 26, 2006
    #1
    1. Advertising

  2. Teej

    Teej Guest

    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

    Teej wrote:
    > 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, Jul 26, 2006
    #2
    1. Advertising

  3. Teej

    Teej Guest

    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

    Teej wrote:
    > 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, Aug 15, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGlmZmlkZW50?=

    Setting CSSClass for Datagrid header's for sortable fields

    =?Utf-8?B?RGlmZmlkZW50?=, Dec 22, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,601
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Dec 23, 2004
  2. Replies:
    0
    Views:
    2,442
  3. =?Utf-8?B?VFBoZWxwcw==?=

    Sortable/pagable datagrid with hyperlink column

    =?Utf-8?B?VFBoZWxwcw==?=, Dec 29, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    4,711
    Steven Cheng[MSFT]
    Jan 3, 2006
  4. tonicvodka
    Replies:
    0
    Views:
    338
    tonicvodka
    Jan 18, 2006
  5. hansiman

    Image in header column (not replacing column header text)

    hansiman, Feb 5, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    3
    Views:
    291
    hansiman
    Feb 7, 2004
Loading...

Share This Page