简体   繁体   English

在列表视图列中对项目进行排序

[英]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 嗨,我有一个具有不同列ID,名称,邮政编码,城市,国家/地区,电话,电子邮件的列表视图,并且我有两个表单,即Form1和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 . 我希望Form2中的信息在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. 例如,应将Form2中的“名称” Texbox插入到“名称”列下的Form1列表视图中,并将文本框的Zipcode文本插入到Zipcode列下。

picture http://img717.imageshack.us/img717/3486/skiten1.png 图片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 表格1

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 表格2

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

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

Form2 表格2

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 仍然是Form2

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

    }

//Here comes the Contact class //这里是Contact类

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 在Contact类中,还有上述变量的属性,然后是一个像这样的ToString

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


        }

Form1 表格1

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. 您只是试图将ListViewItem添加到ListView控件中,然后将更多信息添加到该ListViewItem的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. 您的customerFrame类(Form2)看起来好像没有对Contact参考的引用。

Your customerFrame return a Contact object with the data filled out. 您的customerFrame返回一个Contact对象,其中已填写数据。 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: 然后您的Form1调用应如下所示:

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);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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