[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1124
  • Last Modified:

How to format unbound gridview column

How do you format an unbound column (datetime field) in a gridview as just the date?
0
tbeck1983
Asked:
tbeck1983
2 Solutions
 
Anurag ThakurCommented:
use the DataFormatString property for the bound column
DataFormatString="{0:dd-MM-yyyy}"
0
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now