datagrids..... i just cant get them to do what i want

M

Mike Fellows

Below is my code that is carried out on my dataset, datagrid etc...

Im trying to get column0 "Date & Time" to show date and time, not just date

ive read some stuff posted by Dmitriy Lapshin on this board that goes way
over my
ability (this is my first vb or vb.net project). from what I can tell i
need a GridColumnStyles
collection adding to my datagrid but each and everytime I try to do this my
code crashes.

It seems such a simple problem that is taking way too long to try and sort,
so any help would be greatly appreciated

Regards


Mike Fellows

da.SelectCommand = New OleDbCommand(SQLStr, ocon)
'Fill the DataSet with the Data
da.Fill(ds)

ds.Tables(0).Columns.Item(0).ColumnName = "Date & Time"
ds.Tables(0).Columns.Item(1).ColumnName = "Call Made By"
ds.Tables(0).Columns.Item(2).ColumnName = "Days Since RTB"
ds.Tables(0).Columns.Item(3).ColumnName = "Action"
ds.Tables(0).Columns.Item(4).ColumnName = "Price In"
ds.Tables(0).Columns.Item(5).ColumnName = "Property Valued"
ds.Tables(0).Columns.Item(6).ColumnName = "Notes"

Dim ts As New DataGridTableStyle()
ts.MappingName = "Table"

DataGrid1.TableStyles.Clear()
DataGrid1.TableStyles.Add(ts)
Me.DataGrid1.DataSource = ds.Tables(0)
ts.GridColumnStyles(0).Width = 90
ts.GridColumnStyles(1).Width = 90
ts.GridColumnStyles(2).Width = 100
ts.GridColumnStyles(3).Width = 90
ts.GridColumnStyles(4).Width = 90
ts.GridColumnStyles(5).Width = 90
ts.GridColumnStyles(6).Width = 250
RowCount = ds.Tables(0).Rows.Count
ColumnCount = ds.Tables(0).Columns.Count
da.Dispose()
ds.Dispose()
ocon.Close()
ocon.Dispose()
 
S

scorpion53061

First of all never say your code "crashes". It says nothing about the
problem. Always use a Try Catch block to trap the error and report that.

Dates and times throw many beginners for a loop.

i suspect the problem is with your database setup. See the ADO.NET ng about
that. If you are using an access database the simple Date/Time field will do
what you want. The "General" under format will work nicely.

But lets say you were filling a textbox with a date/time setup and it was
not the way you wanted it:

TextBox7.Text = Format(Date.Now, "MM/dd/yyyy HH:mm:ss")

or

Textbox7.Text = Format(Dsjobitems1.Tables(0).Rows(0).Item("MYDATE"),
"MM/dd/yyyy")

hope this helps.......
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi Mike,

Some observations on your code:

a) Never assign grid's DataSource property directly. Use the
SetDataBinding() method instead.
b) The table style/column style setup should always be done BEFORE the data
source is bound to the grid.
c) Mind this caution from MSDN Library:

CAUTION Always create DataGridColumnStyle objects and add them to the
GridColumnStylesCollection before adding DataGridTableStyle objects to the
GridTableStylesCollection. When you add an empty DataGridTableStyle to the
collection, DataGridColumnStyle objects are automatically generated for you.
Consequently, an exception will be thrown if you try to add new
DataGridColumnStyle objects with duplicate MappingName values to the
GridColumnStylesCollection.

d) Create DataGridColumnStyle instances manually for each of the data
columns. The "DataGridTableStyle Class" topic in the MSDN library gives a
good example on how to do that.
 
M

Mike Fellows

)
NNTP-Posting-Date: Mon, 20 Oct 2003 13:43:00


Organization: ntl Cablemodem News Service

ok ive tried 2 things

Dim mytime As String
mytime = Me.DataGrid1.Item(0, 0)
MessageBox.Show(mytime)

this showed me that the datagrid was storing the time correclty just not
displaying it

then i tried

Me.DataGrid1.Item(0, 0) = Format(Me.DataGrid1.Item(0, 0), "MM/dd/yyyy
HH:mm:ss")
Dim mytime As String
mytime = Me.DataGrid1.Item(0, 0)
MessageBox.Show(mytime)


im not sure i did the above correctly but it didnt make any difference to
the diaply of the datagrid
and the messagebox still showed the time

so im back to square one :-(
 
S

scorpion53061

Put a try catch block around the areas that are bombing and tell what it is
reporting....
 
M

Mike Fellows

sorry you misunderstood

the code that i initially posted has no problems

the problems come when i try to use the gridcolumnstyles which is where i
think
i need to format the data in the datagrid

im sure i need to do somthing like the following:

me.datagrid1.tablestyles(0).gridcolumnstyles("MM/dd/yyyy HH:mm:ss")

but i dont know how to get the datagrid to accept the above (if that makes
sense)
as i recieve the following error "Property access must assign to the
property or use its value."
 
D

Dmitriy Lapshin [C# / .NET MVP]

Mike,
me.datagrid1.tablestyles(0).gridcolumnstyles("MM/dd/yyyy HH:mm:ss")

should be changed to:

me.datagrid1.tablestyles(0).gridcolumnstyles(0).Format = "MM/dd/yyyy
HH:mm:ss"
^^^
Where the second zero (the one in the parenthes following the
"gridcolumnstyles") should be replaced with the index of the column style
corresponding to the datetime column.
 
M

Mike Fellows

that doesnt work either

that just gives me the compile error:
format' is not a member of 'System.Windows.Forms.DataGridColumnStyle'.

cheers

Mike


Dmitriy Lapshin said:
Mike,
me.datagrid1.tablestyles(0).gridcolumnstyles("MM/dd/yyyy HH:mm:ss")

should be changed to:

me.datagrid1.tablestyles(0).gridcolumnstyles(0).Format = "MM/dd/yyyy
HH:mm:ss"
^^^
Where the second zero (the one in the parenthes following the
"gridcolumnstyles") should be replaced with the index of the column style
corresponding to the datetime column.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Mike Fellows said:
sorry you misunderstood

the code that i initially posted has no problems

the problems come when i try to use the gridcolumnstyles which is where i
think
i need to format the data in the datagrid

im sure i need to do somthing like the following:

me.datagrid1.tablestyles(0).gridcolumnstyles("MM/dd/yyyy HH:mm:ss")

but i dont know how to get the datagrid to accept the above (if that makes
sense)
as i recieve the following error "Property access must assign to the
property or use its value."




it
is difference
to not
just can
tell try
and
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top