简体   繁体   中英

Sort item in listview columns

Hi i have a listview with diffrent columns ID, Name, Zipcode, City, Country, Phone, Email And i have two Forms, Form1 and Form2

In the First form i have the listview with the columns On the second Form i have a Custom manager with diffrent Listboxes where you can type in the customer information (ex Name, Zipcode, City, etc)

I want the information in Form2 to be sorted after the columns in Form1 . ex the "name" Texbox in Form2 should be inserted in Form1 listview under the column Name, and The textbox Zipcode text should be inserted under the Zipcode column.

picture http://img717.imageshack.us/img717/3486/skiten1.png

It's hard because im little bit of a beginner and to follow diffrent sites didn't help me.

Please what can i do.

Form1

InitializeComponent();


listView1.View = View.Details;
listView1.LabelEdit = true;
listView1.AllowColumnReorder = true;
listView1.FullRowSelect = true;
listView1.GridLines = true;
listView1.Sorting = SortOrder.Ascending;

listView1.FullRowSelect = true;
listView1.Columns.Add("ID", 300, HorizontalAlignment.Left);
listView1.Columns.Add("Name", 70, HorizontalAlignment.Left);
listView1.Columns.Add("Zipcode", 70, HorizontalAlignment.Left);
listView1.Columns.Add("City", 100, HorizontalAlignment.Left);
listView1.Columns.Add("Country", 100, HorizontalAlignment.Left);
listView1.Columns.Add("Phone", 100, HorizontalAlignment.Left);
listView1.Columns.Add("Email", 100, HorizontalAlignment.Left);

Form2

private void btnOk_Click(object sender, EventArgs e)
       {
           contact.FirstName = tbFirstName.Text;
           firstName = contact.FirstName;

           contact.LastName = tbLastName.Text;
           lastName = contact.LastName;

Form2

private void btnOk_Click(object sender, EventArgs e)
        {
            MainForm main = new MainForm();

            contact.FirstName = tbFirstName.Text;
            firstName = contact.FirstName;

            contact.LastName = tbLastName.Text;
            lastName = contact.LastName;

still Form2

 public override string ToString()
    {
        return string.Format("[{0}]", contact.ToString());

    }

//Here comes the Contact class

class Contact
    {
    private string firstName;
    private string lastName

In the Contact class there is also properties of the above variables and then a ToString like this

public override string ToString()
        {
            return string.Format("[{0}, {1}, {2}, {3}]", firstName, lastName);


        }

Form1

private void MainForm_Load(object sender, EventArgs e)
        {
            ColumnHeader columnheader;
            ListViewItem listviewitem;

            // Ensure that the view is set to show details.
            listView1.View = View.Details;
if (customerframe.ShowDialog() == DialogResult.OK) //if button OK is clicked then value will be inserted
            {
                listviewitem = new ListViewItem(contact.FirstName);
                listviewitem.SubItems.Add(contact.LastName);
this.listView1.Items.Add(listviewitem);

I dont really know know how much code you need for helping me, moreover, my code is very messy

That's not exactly "sorting" in the traditional sense. You are just trying to add a ListViewItem into the ListView control and then add more information into that ListViewItem's SubItems.

An example would look something like this:

ListViewItem lvi = new ListViewItem(yourID);
lvi.SubItems.Add(contact.Fullname);
lvi.SubItems.Add(zipcode);
lvi.SubItems.Add(city);
lvi.SubItems.Add(address.country);
lvi.SubItems.Add(phone);
lvi.SubItems.Add(email);

listView1.Items.Add(lvi);

Update:

Your customerFrame class (Form2) doesn't look like it has the reference to your Contact reference.

Your customerFrame return a Contact object with the data filled out. Something like this:

Public Contact GetContact() {
  Contact contact = new Contact();
  contact.FirstName = tbFirstName.Text;
  // etc.
  return contact;
}

And then your Form1 call should look something like this:

using (var customerFrame = new CustomerFrame()) {
  if (customerFrame.ShowDialog() == DialogResult.OK) {
    Contact contact = customerFrame.GetContact();

    listviewitem = new ListViewItem(contact.FirstName);
    listviewitem.SubItems.Add(contact.LastName);
    // etc.
    this.listView1.Items.Add(listviewitem);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM