locked
datetime format in dgv RRS feed

  • Question

  • heee guys i have a datatable in access with a column " Time "

    and i use it in a datagridview

    how can i format it  to short time in dgv

    Monday, September 6, 2010 11:35 AM

Answers

  • Hi

    I use the following line in my form load event to format a DataGridView column to display Time. The column receives a full DateTime value.

    DGV.Columns("Time").DefaultCellStyle.Format = " HH:mm"


    Regards from Livingston, Scotland (Please ignore any stupid formatting produced by this forum)
    Monday, September 6, 2010 1:37 PM
  • The error message should be something like "Object reference not set to an instance object" and "using new" is just a suggestion. You still to have to analyze and decide what to do. It means that you are trying with an object variable that is nothing. My guess is that you don't have a Column named "Time". Try : If Not DGV.Columns.Contains("Time") Then MessageBox.Show("No Time column.") Either your code is placed before DataBinding or your column is not named Time...
    Monday, September 6, 2010 4:26 PM

All replies

  • Hi

    I use the following line in my form load event to format a DataGridView column to display Time. The column receives a full DateTime value.

    DGV.Columns("Time").DefaultCellStyle.Format = " HH:mm"


    Regards from Livingston, Scotland (Please ignore any stupid formatting produced by this forum)
    Monday, September 6, 2010 1:37 PM
  • well i've tried but an error  says  use new keyword to create an object instance
    Monday, September 6, 2010 2:50 PM
  • Do you have a DataGridView named DGV ? Do you have a column named time ? Also do you had this cdoe after databinding ? Of course you have to adapt for the actual name you are using...

     


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    Monday, September 6, 2010 2:52 PM
  • Yes well  of course i've adjust to my own code  

    but i have to use the keyword New to create an object instance

    Monday, September 6, 2010 3:28 PM
  • Hi

    What object are you using that needs the 'New' declaration?

    What is the Name of your DGV, and what is the column name of your Time column?

     


    Regards from Livingston, Scotland (Please ignore any stupid formatting produced by this forum)
    Monday, September 6, 2010 3:34 PM
  • The error message should be something like "Object reference not set to an instance object" and "using new" is just a suggestion. You still to have to analyze and decide what to do. It means that you are trying with an object variable that is nothing. My guess is that you don't have a Column named "Time". Try : If Not DGV.Columns.Contains("Time") Then MessageBox.Show("No Time column.") Either your code is placed before DataBinding or your column is not named Time...
    Monday, September 6, 2010 4:26 PM
  • i still have the same problem

    this is my code

     

    Private stroledb As String = "SELECT* FROM Bookingen"

     

    Private strconnection As String = "provider= Microsoft.ACE.OLEDB.12.0; Data Source=D:\tam project21.accdb; persist security Info=false;"

     

    Private objConnection As New OleDbConnection(strconnection)

     

    Private objDataAdapter As OleDbDataAdapter = New OleDbDataAdapter(stroledb, objConnection)

     

     

    Private objCommandbuilder As OleDbCommandBuilder = New OleDbCommandBuilder(objDataAdapter)

     

    Private objbindingsource As New BindingSource

     

    Private mydataset As DataSet = New tam_project21DataSet1

     

                                    as you can see my dgv is called D1  and mydatasource has a column "tijd"

    what am i doing wrong?

     

    Private Sub Booking_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    D1.Columns(

    "tijd").DefaultCellStyle.Format = "HH:mm"

    Monday, September 6, 2010 10:18 PM
  • Hi

    Have you actually bound the datasource to the DGV before you are coding for the columns?


    Regards from Livingston, Scotland (Please ignore any stupid formatting produced by this forum)
    Tuesday, September 7, 2010 11:55 AM