在Listview中对列进行排序 [英] Sort columns in Listview

查看:114
本文介绍了在Listview中对列进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚接触c#

我已经制定了一个客户经理计划.我有两种形式,第一种是我的列表视图,另一种是您在其中键入客户信息(姓名,国家/地区等)的地方
我想在不同的列中排序

看这里,您会了解
http://imageshack.us/f/717/skiten1.png/ [

Im pretty new to c#

I have made a customer manager program. I have two forms, the first is my listview and the other is where you type in the information of the customer(name, country, etc)
I want to sort in the diffrent columns

Look here and you will understand
http://imageshack.us/f/717/skiten1.png/[^]


Here is some code, maybe it''s not enough i dont know.

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



Form1



Form1

private void MainForm_Load(object sender, EventArgs e)
{
    if (customerframe.ShowDialog() == DialogResult.OK) //if button OK is clicked then value will be inserted
    {
        // var item = string.Format("[{0}]",contact.ToString());

        listView1.Items.Add(string.Format("[{0}]", customerframe.ToString()));

    }
}



Form2



Form2

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

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

           contact.PhoneData = new CustomerFiles.Phone(tbCellPhone.Text);
           phone = contact.PhoneData;

           contact.PhoneData = new CustomerFiles.Phone(tbHomePhone.Text);
           email = contact.EmailData;

           //inside address class
           address.City = tbCity.Text;
           city = address.City;

           address.country = cbCountry.Text;
           //address.Country = new CustomerFiles.Countries(cbCountry.Text);
          // countryinfo = address.Country;


           address.Street = tbStreet.Text;
           street = address.Street;

           address.ZipCode = tbZipCode.Text;
           zipcode = address.ZipCode;




       }

       private void btnCancel_Click(object sender, EventArgs e)
       {
           this.Close();
       }

       private void tbFirstName_TextChanged(object sender, EventArgs e)
       {


           if (tbFirstName.Text != string.Empty)
           {
               contact.FirstName = tbFirstName.Text;
           }
           else
               MessageBox.Show("You must fill in your first name");
           tbFirstName.Focus();
       }



Form2



Form2

public override string ToString()
        {
            return string.Format("[{0}, {1}, {2}]", contact.ToString(), address.ToString(), country.Name);

        }

推荐答案

作为第一种选择,我建议使用DataGridView代替ListView.对于DataGridView,按列排序是此控件的组成部分.

使用ListView,将需要对数据本身进行排序,这是不好的.但是,如果使用VirtualMode,则可以避免出现这种情况,请参阅
http ://msdn.microsoft.com/zh-CN/library/system.windows.forms.listview.virtualmode.aspx [
For a first alternative, I would advise to use DataGridView instead of ListView. With the DataGridView, sorting by columns is the integral part of this control.

With ListView, it would require sorting of data itself, which is not good. However, you could avoid it if you use VirtualMode, please see http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.virtualmode.aspx[^]. Actually, using the virtual mode is good for both controls. This would make another alternative.

—SA


这篇关于在Listview中对列进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆