TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
lee croydon
NA
48
12k
listView in details view
Sep 13 2014 7:43 AM
Hi i am using a listView to display DVD's the listView is in details view. i have split my dvd into sections i.e Title, Release Date, Main Actor/Actress, Rating and Type. my question is how do i list the dvd in the listView window in there individual columns? at the minute the whole dvd is listed in the 1st column on separate rows. the source code is as follows.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
namespace DVDCollProject
{
public partial class Form1 : Form
{
List<string> list = new List<string>();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)// add dvd
{
bool exists = list.Exists(elements => elements.Contains(titleBox.Text) == true);
if (exists == true)
{
MessageBox.Show("DVD already exists");
}
else
{
MessageBox.Show("DVD added to list");
list.Add(titleBox.Text + ". " + dateBox.Text + ". " + actorBox.Text + ". " + ratingBox.Text + ". " + typeBox.Text);
}
titleBox.Clear();
dateBox.Clear();
actorBox.Clear();
ratingBox.Clear();
typeBox.Clear();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)// exit program
{
Close();
}
private void txToolStripMenuItem_Click(object sender, EventArgs e)//Save list to file
{
StreamWriter dvdColl;
dvdColl = new StreamWriter(@"C:\\dvdList.txt");
foreach (string dvd in list)
{
dvdColl.WriteLine(dvd);
}
dvdColl.Close();
}
private void textFileToolStripMenuItem_Click(object sender, EventArgs e)//Load file to list
{
StreamReader dvdColl;
try
{
dvdColl = new StreamReader(@"C:\\dvdList.txt", true);
}
catch (FileNotFoundException)
{
dvdColl = new StreamReader(@"C:\\dvdList.txt", false);
}
if (true)
{
foreach (string dvd in list)
{
dvdColl.ReadLine();
MessageBox.Show("File added to your DVD list");
}
}
dvdColl.Close();
}
public void SelectDVD()
{
bool exists = list.Exists(elemnets => elemnets.Contains(titleBox.Text) == true);
if (exists == true)
{
foreach (string dvd in list)
{
if (dvd.Contains(titleBox.Text))
{
string[] splitDVD = dvd.Split('.');
listView1.Items.Add(splitDVD[0], 0);
listView1.Items.Add(splitDVD[1], 1);
listView1.Items.Add(splitDVD[2], 2);
listView1.Items.Add(splitDVD[3], 3);
listView1.Items.Add(splitDVD[4], 4);
}
}
}
else
{
MessageBox.Show("DVD does not exist");
}
}
private void button2_Click(object sender, EventArgs e)//select dvd
{
SelectDVD();
titleBox.Clear();
}
private void button4_Click(object sender, EventArgs e)// delete dvd
{
bool exists = list.Exists(elements => elements.Contains(titleBox.Text) == true);
if (exists == true)
{
foreach (string dvd in list)
{
if (dvd.Contains(titleBox.Text))
{
list.Remove(dvd);
MessageBox.Show("DVD removed from list successfully");
break;
}
}
}
else
{
MessageBox.Show("DVD does not exist");
}
titleBox.Clear();
}
private void button3_Click(object sender, EventArgs e)//edit dvd
{
List<string> edit = new List<string>();
bool exists = list.Exists(elements => elements.Contains(titleBox.Text) == true);
if (exists == true)
{
foreach (string dvd in list)
{
if (dvd.Contains(titleBox.Text))
{
listView1.Items.Add(dvd);
DialogResult dialogResult = MessageBox.Show(this, "Is this the DVD you wish to edit", "Yes", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
MessageBox.Show("Please enter the correct DVD details and click the add button");
list.Remove(dvd);
listView1.Items.Clear();
titleBox.Clear();
}
else
{
MessageBox.Show("The DVD you wish to edit does not exist");
listView1.Items.Clear();
titleBox.Clear();
}
}
}
}
else
{
MessageBox.Show("No DVD or incorrect DVD enetered please try again");
}
}
private void button5_Click(object sender, EventArgs e)//List all alphabetically
{
IComparer<string> title = new SortByTitleClass();
list.Sort(title);
foreach (string dvd in list)
{
string[] splitDVD = dvd.Split('.');
listView1.Items.Add(splitDVD[0], 0);
listView1.Items.Add(splitDVD[1], 1);
listView1.Items.Add(splitDVD[2], 2);
listView1.Items.Add(splitDVD[3], 3);
listView1.Items.Add(splitDVD[4], 4);
listView1.Items.Add("\n", 5);
}
}
private void button10_Click(object sender, EventArgs e)//Clear List Box
{
listView1.Items.Clear();
}
private void button7_Click(object sender, EventArgs e)// List all by date oldest first
{
IComparer<string> date = new SortByDateClass();
list.Sort(date);
foreach (string dvd in list)
{
string[] splitDVD = dvd.Split('.');
listView1.Items.Add(splitDVD[0], 0);
listView1.Items.Add(splitDVD[1], 1);
listView1.Items.Add(splitDVD[2], 2);
listView1.Items.Add(splitDVD[3], 3);
listView1.Items.Add(splitDVD[4], 4);
}
}
private void button8_Click(object sender, EventArgs e)//List all by Actor/Actress alphabetically
{
IComparer<string> actor = new SortByActorClass();
list.Sort(actor);
foreach (string dvd in list)
{
string[] splitDVD = dvd.Split('.');
listView1.Items.Add(splitDVD[0], 0);
listView1.Items.Add(splitDVD[1], 1);
listView1.Items.Add(splitDVD[2], 2);
listView1.Items.Add(splitDVD[3], 3);
listView1.Items.Add(splitDVD[4], 4);
}
}
private void button9_Click(object sender, EventArgs e)//List all by age rating
{
IComparer<string> age = new SortByAgeClass();
list.Sort(age);
foreach (string dvd in list)
{
string[] splitDVD = dvd.Split('.');
listView1.Items.Add(splitDVD[0], 0);
listView1.Items.Add(splitDVD[1], 1);
listView1.Items.Add(splitDVD[2], 2);
listView1.Items.Add(splitDVD[3], 3);
listView1.Items.Add(splitDVD[4], 4);
}
}
private void button6_Click(object sender, EventArgs e)//List all by film type
{
IComparer<string> type = new SortByTypeClass();
list.Sort(type);
foreach (string dvd in list)
{
string[] splitDVD = dvd.Split('.');
listView1.Items.Add(splitDVD[0], 0);
listView1.Items.Add(splitDVD[1], 1);
listView1.Items.Add(splitDVD[2], 2);
listView1.Items.Add(splitDVD[3], 3);
listView1.Items.Add(splitDVD[4], 4);
listView1.Items.Add("\n", 5);
}
}
}
public class SortByTitleClass : IComparer<string>
{
public int Compare(string x, string y)
{
string[] arr1 = x.Split(',');
string[] arr2 = y.Split(',');
return arr1[0].CompareTo(arr2[0]);
}
}
public class SortByDateClass : IComparer<string>
{
public int Compare(string x, string y)
{
string[] arr1 = x.Split('.');
string[] arr2 = y.Split('.');
return arr1[1].CompareTo(arr2[1]);
}
}
public class SortByActorClass : IComparer<string>
{
public int Compare(string x, string y)
{
string[] arr1 = x.Split('.');
string[] arr2 = y.Split('.');
return arr1[2].CompareTo(arr2[2]);
}
}
public class SortByAgeClass : IComparer<string>
{
public int Compare(string x, string y)
{
string[] arr1 = x.Split('.');
string[] arr2 = y.Split('.');
return arr1[3].CompareTo(arr2[3]);
}
}
public class SortByTypeClass : IComparer<string>
{
public int Compare(string x, string y)
{
string[] arr1 = x.Split('.');
string[] arr2 = y.Split('.');
return arr1[4].CompareTo(arr2[4]);
}
}
}
Any help you can offer would be greatly appreciated
Quick update i have found the solution the following part of the code
foreach (string dvd in list)
{
string[] splitDVD = dvd.Split('.');
listView1.Items.Add(splitDVD[0], 0);
listView1.Items.Add(splitDVD[1], 1);
listView1.Items.Add(splitDVD[2], 2);
listView1.Items.Add(splitDVD[3], 3);
listView1.Items.Add(splitDVD[4], 4);
}
}
was the culprit and has now changed to:
foreach (string dvd in list)
{
string[] splitDVD = dvd.Split('.');
listViewItem entry = listView1.Items.Add(splitDVD[0]);
entry.SubItems.Add(splitDVD[1]);
entry.SubItems.Add(spiltDVD[2]);
entry.SubItems.Add(spiltDVD[3]);
entry.SubItems.Add(splitDVD[4]);
}
}
Reply
Answers (
0
)
Cluster-Based Routing Protocol for Wireless Sensor Networks
condition for add new row