Link to home
Start Free TrialLog in
Avatar of tbeck1983
tbeck1983Flag for United States of America

asked on

How to format unbound gridview column

How do you format an unbound column (datetime field) in a gridview as just the date?
ASKER CERTIFIED SOLUTION
Avatar of Anurag Thakur
Anurag Thakur
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tbeck1983

ASKER

Is is an unbound column.
SOLUTION
Avatar of OMC2000
OMC2000
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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

    <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

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
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

>>>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?
Yes it was.  I figured it out.  Thanks for you help.