I'm having trouble formatting the datetime field in a gatagrid.

I am able to format the date using:
<asp:BoundColumn DataField="MyDateColumn" DataFormatString="{0:d}" ... />

But Since I'm using two tables, I'm creating the datagrid manually instead of using "GridView" from the Toolbox.


My Code:
================

Dim comm2 As SqlDataAdapter = New SqlDataAdapter("select T.taskID as 'ID', T.taskName as 'Task', T.taskDescription as 'Description', S.staffName as 'AssignedTo', T.assignedOn as 'AssignedOn', T.deadline as 'Deadline', T.priority as 'Priority' from temp_staff S, tasks T where S.staffRecNo = T.assignedTo and T.assignedBy = '" & staffRecNo & "' and T.deadline > '" & finalDate1 & "' and T.deadline < '" & finalDate2 & "' ", conn2)

        Dim ds2 As DataSet = New DataSet
        comm2.Fill(ds2, "test")

        dg1.Visible = True
        dg1.DataSource = ds2.Tables("test")
        dg1.DataBind()

=================

Please help

Hi,

There is no problem using your datasource with two tables, you can still define grid in aspx anyway you can use this code example

Aspx file
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridFormat.aspx.cs" Inherits="GridFormat" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GView" runat="server">
        </asp:GridView>
    
    </div>
    </form>
</body>
</html>



cs file
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;

public partial class GridFormat : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GView.DataSource = DataSource.GetData().Tables[0];
            GView.RowDataBound += new GridViewRowEventHandler(GView_RowDataBound);
            GView.EmptyDataText = "";
            GView.DataBind();
        }
    }

    void GView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DateTimeFormatInfo dtinfo = new CultureInfo("en-US", true).DateTimeFormat;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //e.Row.Cells[3]
            if (!string.IsNullOrEmpty(e.Row.Cells[5].Text.Trim().Replace("&nbsp;","")))
                e.Row.Cells[5].Text = DateTime.Parse(e.Row.Cells[5].Text).ToString("MMM/dd/yyyy");
        }
        //e.Row.DataItemIndex;
        //throw new Exception("The method or operation is not implemented.");
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.