By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,221 Members | 1,663 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,221 IT Pros & Developers. It's quick & easy.

Formatting date in listview

P: n/a
Hi all.

I have a problem. I get all my data from a dataset (not mssql) and in that
dataset i have a table called "Activity". In this table i have a few date
fields. When i display the dataset "Activity" in a listview the date fields
is displayed like : dd.mm.yy 00:00:00. How can i format this date. I do not
want the time. Just the date.

In my dataset "activity" all date fields are formatted as: dd.mm.yyyy. Why
do i get the 00:00:00 ?? and how can i avoid it?

some code i use:
private void LoadActivityList(DataTable dtActivities)
{
//Sort the datatable
dtActivities.DefaultView.Sort = "a-stringactno";

//display datatable in listview
//Set the List to Detail View
lvActivities.View = View.Details;
lvActivities.FullRowSelect = true;
lvActivities.HideSelection = false;
lvActivities.MultiSelect = false;

//create columns to display
lvActivities.Columns.Add("Activity no.", 80,
HorizontalAlignment.Left);
lvActivities.Columns.Add("Description", 200,
HorizontalAlignment.Left);
lvActivities.Columns.Add("Status", 50, HorizontalAlignment.Left);
// BUG: this date i want to display as dd.mm.yyyy
lvActivities.Columns.Add("Created", 80, HorizontalAlignment.Left);
lvActivities.Columns.Add("Last event", 100,
HorizontalAlignment.Left);
lvActivities.Columns.Add("Contract", 0, HorizontalAlignment.Left);
lvActivities.Columns.Add("Product", 0, HorizontalAlignment.Left);

// Clear the ListView control
lvActivities.Items.Clear();

// Display items in the ListView control
for (int i = 0; i < dtActivities.Rows.Count; i++)
{
DataRow drow = dtActivities.Rows[i];

// Define the list items and get data from datatable
ListViewItem lvi = new
ListViewItem(drow["a-stringactno"].ToString());
lvi.SubItems.Add(drow["a-problem"].ToString());
lvi.SubItems.Add(drow["a-status"].ToString());
lvi.SubItems.Add(drow["a-regdate"].ToString());
lvi.SubItems.Add(drow["a-lastacteventdate"].ToString());
lvi.SubItems.Add(drow["a-contr1"].ToString());
lvi.SubItems.Add(drow["a-prodid"].ToString());

// Add the list items to the ListView
lvActivities.Items.Add(lvi);
}
this.lvActivities.Sorting = SortOrder.Ascending;
}

Jun 6 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi,

Use :

lvi.SubItems.Add(
((DateTime)drow["a-lastacteventdate"]).ToShortDateString());
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Lars Erik Nes" <La*********@discussions.microsoft.com> wrote in message
news:A2**********************************@microsof t.com...
Hi all.

I have a problem. I get all my data from a dataset (not mssql) and in that
dataset i have a table called "Activity". In this table i have a few date
fields. When i display the dataset "Activity" in a listview the date
fields
is displayed like : dd.mm.yy 00:00:00. How can i format this date. I do
not
want the time. Just the date.

In my dataset "activity" all date fields are formatted as: dd.mm.yyyy. Why
do i get the 00:00:00 ?? and how can i avoid it?

some code i use:
private void LoadActivityList(DataTable dtActivities)
{
//Sort the datatable
dtActivities.DefaultView.Sort = "a-stringactno";

//display datatable in listview
//Set the List to Detail View
lvActivities.View = View.Details;
lvActivities.FullRowSelect = true;
lvActivities.HideSelection = false;
lvActivities.MultiSelect = false;

//create columns to display
lvActivities.Columns.Add("Activity no.", 80,
HorizontalAlignment.Left);
lvActivities.Columns.Add("Description", 200,
HorizontalAlignment.Left);
lvActivities.Columns.Add("Status", 50,
HorizontalAlignment.Left);
// BUG: this date i want to display as dd.mm.yyyy
lvActivities.Columns.Add("Created", 80,
HorizontalAlignment.Left);
lvActivities.Columns.Add("Last event", 100,
HorizontalAlignment.Left);
lvActivities.Columns.Add("Contract", 0,
HorizontalAlignment.Left);
lvActivities.Columns.Add("Product", 0,
HorizontalAlignment.Left);

// Clear the ListView control
lvActivities.Items.Clear();

// Display items in the ListView control
for (int i = 0; i < dtActivities.Rows.Count; i++)
{
DataRow drow = dtActivities.Rows[i];

// Define the list items and get data from datatable
ListViewItem lvi = new
ListViewItem(drow["a-stringactno"].ToString());
lvi.SubItems.Add(drow["a-problem"].ToString());
lvi.SubItems.Add(drow["a-status"].ToString());
lvi.SubItems.Add(drow["a-regdate"].ToString());
lvi.SubItems.Add(drow["a-lastacteventdate"].ToString());
lvi.SubItems.Add(drow["a-contr1"].ToString());
lvi.SubItems.Add(drow["a-prodid"].ToString());

// Add the list items to the ListView
lvActivities.Items.Add(lvi);
}
this.lvActivities.Sorting = SortOrder.Ascending;
}

Jun 6 '06 #2

P: n/a
Hi.

Thank you for your reply. I get a run-time error.

Message "the specified cast is not valid". What can be wrong. I can see that
my datatabel "dtActivities" contains a date in the field "a-lastactevetndate"
(18.06.2006 00:00:00)

Any more tips`?

Thanks.

Lars E.

"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,

Use :

lvi.SubItems.Add(
((DateTime)drow["a-lastacteventdate"]).ToShortDateString());
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Lars Erik Nes" <La*********@discussions.microsoft.com> wrote in message
news:A2**********************************@microsof t.com...
Hi all.

I have a problem. I get all my data from a dataset (not mssql) and in that
dataset i have a table called "Activity". In this table i have a few date
fields. When i display the dataset "Activity" in a listview the date
fields
is displayed like : dd.mm.yy 00:00:00. How can i format this date. I do
not
want the time. Just the date.

In my dataset "activity" all date fields are formatted as: dd.mm.yyyy. Why
do i get the 00:00:00 ?? and how can i avoid it?

some code i use:
private void LoadActivityList(DataTable dtActivities)
{
//Sort the datatable
dtActivities.DefaultView.Sort = "a-stringactno";

//display datatable in listview
//Set the List to Detail View
lvActivities.View = View.Details;
lvActivities.FullRowSelect = true;
lvActivities.HideSelection = false;
lvActivities.MultiSelect = false;

//create columns to display
lvActivities.Columns.Add("Activity no.", 80,
HorizontalAlignment.Left);
lvActivities.Columns.Add("Description", 200,
HorizontalAlignment.Left);
lvActivities.Columns.Add("Status", 50,
HorizontalAlignment.Left);
// BUG: this date i want to display as dd.mm.yyyy
lvActivities.Columns.Add("Created", 80,
HorizontalAlignment.Left);
lvActivities.Columns.Add("Last event", 100,
HorizontalAlignment.Left);
lvActivities.Columns.Add("Contract", 0,
HorizontalAlignment.Left);
lvActivities.Columns.Add("Product", 0,
HorizontalAlignment.Left);

// Clear the ListView control
lvActivities.Items.Clear();

// Display items in the ListView control
for (int i = 0; i < dtActivities.Rows.Count; i++)
{
DataRow drow = dtActivities.Rows[i];

// Define the list items and get data from datatable
ListViewItem lvi = new
ListViewItem(drow["a-stringactno"].ToString());
lvi.SubItems.Add(drow["a-problem"].ToString());
lvi.SubItems.Add(drow["a-status"].ToString());
lvi.SubItems.Add(drow["a-regdate"].ToString());
lvi.SubItems.Add(drow["a-lastacteventdate"].ToString());
lvi.SubItems.Add(drow["a-contr1"].ToString());
lvi.SubItems.Add(drow["a-prodid"].ToString());

// Add the list items to the ListView
lvActivities.Items.Add(lvi);
}
this.lvActivities.Sorting = SortOrder.Ascending;
}


Jun 7 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.