How to format unbound gridview column

How do you format an unbound column (datetime field) in a gridview as just the date?
tbeck1983Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anurag ThakurTechnical ManagerCommented:
use the DataFormatString property for the bound column
DataFormatString="{0:dd-MM-yyyy}"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tbeck1983Author Commented:
Is is an unbound column.
0
OMC2000Commented:
Saying "IT is an unbound column", you mean there is no database column associated with that column, but you assign some value to it, don't you?
If solution proposed by regi0017 does not work for you, try to convert Date to the string of desired format using String.Format("{0:dd-MM-yyyy}")
0
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

tbeck1983Author Commented:
I am doing everything by code filling datasets,etc.. When I hit an apply butting in fills the gridview.  I don't see where I can use DataFormatString="{0:dd-MM-yyyy}" since the columns are not bound.  
    Protected Sub btnApplyFilters_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnApply.Click
        Select Case Me.ddlTargetField.Text
            Case "All"
                Me.FillByAll()
                Me.GridView1.DataSource = dsAll.Tables(0)
                Me.GridView1.DataBind()
                SetDataGridSettings()
        End Select
    End Sub
 
Protected Sub FillByAll()
        conTracking.ConnectionString = strConnection
 
        Dim sSelect As String = _
        "SELECT sID, sLogonDate, sLogonTime, sLogonUser, sLogonComputer," & _
        "sLogonType, sLogonIP, sLogonMAC, sLogoffDate, sLogoffTime, sFalsePos " & _
        "FROM tblUserTracking ORDER BY sLogonDate, sLogonTime"
        Dim cmdAll As New OleDbCommand(sSelect, conTracking)
 
        daAll.SelectCommand = cmdAll
        Dim cbAll As New OleDbCommandBuilder(daAll)
 
        Try
            conTracking.Open()
            dsAll.Clear()
            daAll.Fill(dsAll, "tblUserTracking")
        Catch ex As OleDbException
            MsgBox(ex.Message, , "OLEDB Error")
        Finally
            conTracking.Close()
        End Try

Open in new window

0
Anurag ThakurTechnical ManagerCommented:
havent you defined your gridview in the aspx page
if yes then share that piece of code and we will try to help you out
0
tbeck1983Author Commented:

    <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
                        GridLines="None" Height="23px" Width="100%">
                        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                        <RowStyle BackColor="#F7F6F3" Font-Names="Verdana" Font-Size="Smaller" ForeColor="#333333" />
                        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" Font-Names="Verdana" Font-Size="Smaller"
                            ForeColor="White" />
                        <EditRowStyle BackColor="#999999" />
                        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    </asp:GridView>

Open in new window

0
Anurag ThakurTechnical ManagerCommented:
why dont you define some bound columns in your gridview so that you can fix the way you want to show your data and also format in the aspx page using the dataformatstring property
if i see your query you will like to format LogOnDate, LogonTime, Mac, Logoffdate, logofftime and it makes a lot of sense to add bound columns to your grid
0
tbeck1983Author Commented:
I have multiple queries.  One query shows all records one shows current logons.  See Snippet.  How am I going to be able to do these queries if the data is bound?
    'Protected Sub FillByCurrentLogons()
    '    conTracking.ConnectionString = strConnection
 
    '    Dim sSelect As String = _
    '    "SELECT sID, sLogonDate, sLogonTime, sLogonUser, sLogonComputer," & _
    '    "sLogonType, sLogonIP, sLogonMAC, sLogoffDate, sLogoffTime, sFalsePos " & _
    '    "FROM tblUserTracking " & _
    '    "WHERE sLogoffDate IS NULL AND sLogoffTime IS NULL AND sFalsePos = ? " & _
    '    "ORDER BY sLogonDate, sLogonTime"
    '    Dim cmdCurrentLogons As New OleDbCommand(sSelect, conTracking)
 
    '    With cmdCurrentLogons.Parameters
    '        .AddWithValue("sFalsePos", "0")
    '    End With
 
    '    daCurrentLogons.SelectCommand = cmdCurrentLogons
    '    Dim cbCurrentLogons As New OleDbCommandBuilder(daCurrentLogons)
 
    '    Try
    '        conTracking.Open()
    '        dsCurrentLogons.Clear()
    '        daCurrentLogons.Fill(dsCurrentLogons, "tblUserTracking")
    '    Catch ex As OleDbException
    '        MsgBox(ex.Message, "OLEDB Error")
    '    Finally
    '        conTracking.Close()
    '    End Try
 
    'End Sub
    'Protected Sub FillByLogonDate()
    '    conTracking.ConnectionString = strConnection
 
    '    Dim sSelect As String = _
    '    "SELECT sID, sLogonDate, sLogonTime, sLogonUser, sLogonComputer," & _
    '    "sLogonType, sLogonIP, sLogonMAC, sLogoffDate, sLogoffTime, sFalsePos " & _
    '    "FROM tblUserTracking " & _
    '    "WHERE sLogonDate LIKE ? " & _
    '    "ORDER BY sLogonDate, sLogonTime"
    '    Dim cmdLogonDate As New OleDbCommand(sSelect, conTracking)
 
    '    With cmdLogonDate
    '        .Parameters.AddWithValue("sLogonDate", Me.txtCriteria.Text & "%")
    '    End With
 
    '    daLogonDate.SelectCommand = cmdLogonDate
    '    Dim cbLogonDate As New OleDbCommandBuilder(daLogonDate)
 
    '    Try
    '        conTracking.Open()
    '        dsLogonDate.Clear()
    '        daLogonDate.Fill(dsLogonDate, "tblUserTracking")
    '    Catch ex As OleDbException
    '        MsgBox(ex.Message, , "OLEDB Error")
    '    Finally
    '        conTracking.Close()
    '    End Try
 
    'End Sub
    'Protected Sub FillByUser()
    '    conTracking.ConnectionString = strConnection
 
    '    Dim sSelect As String = _
    '    "SELECT sID, sLogonDate, sLogonTime, sLogonUser, sLogonComputer," & _
    '    "sLogonType, sLogonIP, sLogonMAC, sLogoffDate, sLogoffTime, sFalsePos " & _
    '    "FROM tblUserTracking " & _
    '    "WHERE sLogonUser = ? " & _
    '    "ORDER BY sLogonDate, sLogonTime"
    '    Dim cmdUser As New OleDbCommand(sSelect, conTracking)
 
    '    With cmdUser.Parameters
    '        .AddWithValue("sLogonUser", Me.txtCriteria.Text)
    '    End With
 
    '    daUser.SelectCommand = cmdUser
    '    Dim cbUser As New OleDbCommandBuilder(daUser)
 
    '    Try
    '        conTracking.Open()
    '        dsuser.Clear()
    '        daUser.Fill(dsuser, "tblUserTracking")
    '    Catch ex As OleDbException
    '        MsgBox(ex.Message, "OLEDB Error")
    '    Finally
    '        conTracking.Close()
    '    End Try
 
    'End Sub
    'Protected Sub FillByComputer()
    '    conTracking.ConnectionString = strConnection
 
    '    Dim sSelect As String = _
    '    "SELECT sID, sLogonDate, sLogonTime, sLogonUser, sLogonComputer," & _
    '    "sLogonType, sLogonIP, sLogonMAC, sLogoffDate, sLogoffTime, sFalsePos " & _
    '    "FROM tblUserTracking " & _
    '    "WHERE sLogonComputer LIKE ? " & _
    '    "ORDER BY sLogonDate, sLogonTime"
    '    Dim cmdComputer As New OleDbCommand(sSelect, conTracking)
 
    '    With cmdComputer.Parameters
    '        .AddWithValue("sLogonComputer", Me.txtCriteria.Text)
    '    End With
 
    '    daComputer.SelectCommand = cmdComputer
    '    Dim cbComputer As New OleDbCommandBuilder(daComputer)
 
    '    Try
    '        conTracking.Open()
    '        dsComputer.Clear()
    '        daComputer.Fill(dsComputer, "tblUserTracking")
    '    Catch ex As OleDbException
    '        MsgBox(ex.Message, "OLEDB Error")
    '    Finally
    '        conTracking.Close()
    '    End Try
 
    'End Sub

Open in new window

0
prairiedogCommented:
>>>How do you format an unbound column (datetime field) in a gridview as just the date?
Is the GridView working ok without considering the format of the datetime field?
0
tbeck1983Author Commented:
Yes it was.  I figured it out.  Thanks for you help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.