listview列排序 [英] listview column sort

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

问题描述





我有一些代码,允许用户对列表视图中的列进行排序。我在Windows项目周围以多种形式使用它。它在所有页面上运行良好,并且通过listview搜索很好地工作,除了一个页面,当
启用列排序但搜索工作正常时,搜索速度会大大减慢。下面是该页面的代码。



我允许用户在搜索框中搜索(不显示,但它是用户可以键入的文本框,它在列表视图中搜索)然后根据搜索条件重新加载底部的列表视图。


此外,用户可以单击列表视图中的列标题,它将对列进行排序... 。


SEARCH



  
private
void ProductSearch_TextChanged( object 发件人,
EventArgs e)



         {



            ProductsSearch(btnAdvancedSearch.Text);


        }



   
private
void ProductsSearch( string filterOption)



        {



           
StringBuilder sb = new
StringBuilder ();



            ;
string ID = txtItemID.Text.EscapeLikeValue();



            
string Name1 = txtItemName1.Text.EscapeLikeValue();



            
string Name2 = txtItemName2.Text.EscapeLikeValue();



            
string Name3 = txtItemName3.Text.EscapeLikeValue();



            
if (ID.Length> 0)



             {



                sb.Append(的" ITEMSTRING像 '" + ID +
"%' " );



            }



           
if (Name1.Length> 0)



             {



               
if (sb.Length> 0)



                 {



                     sb.Append("和");



    ;             }



               
开关(filterOption)



      ;           {



                    
case "基本搜索"



                    &NBSP ;   sb.Append("''+ Description
like'%"
+ Name1 + " %'" );



            ;            
break ;



       ;             
case "高级搜索"



                    &NBSP ;   sb.Append("描述如
'"
+ Name1 + "%'" );



'跨度风格= "字体大小:10.0pt;字体家庭: '信使新'">            &NBSP ;          
break ;



       ;          }



            }



           
if (Name2.Length> 0)



             {



               
if (sb.Length> 0)



                 {



                     sb.Append("和");



    ;             }



               
开关(filterOption)



      ;           {



                    
case "基本搜索"



                    &NBSP ;   sb.Append("''+ Description
like'%"
+ Name2 + " %'" );



            ;              break ;



      ;              
case "高级搜索"



                    &NBSP ;   sb.Append("描述如
'%"
+ Name2 + "%'" ; );



                        
break ;



       ;          }



            }



           
if (Name3.Length> 0)



             {



               
if (sb.Length> 0)



                 {



                     sb.Append("和");



    ;             }



               
开关(filterOption)



      ;           {



                    
case "基本搜索"



                    &NBSP ;   sb.Append("''+ Description
like'%"
+ Name3 + " %'" );



            ;            
break ;



       ;             
case "高级搜索"



                    &NBSP ;   sb.Append("描述如
'%"
+ Name3 + "%'" ; );



                        
break ;



       ;          }



            }



            lsvItems.Items.Clear();



            dtProducts.DefaultView.RowFilter = sb.ToString();



           ; 
foreach DataRow
in dtProducts.Select(sb.ToString()))



             {



               
ListViewItem lvi = new
ListViewItem (行[" Item" ]。ToString());



                lvi.SubItems.Add(row [" Description" ]。ToString());



                lvi.SubItems.Add(row [" Cost" ]。ToString());



                lvi.SubItems.Add(row [" Price" ]。ToString());



                lsvItems.Items.Add(lvi);



            }



           
if (dtProducts.DefaultView.Count< = 0)



  ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {



               
验证 .ShowMessage( Main .lblStatus,
"没有与您的搜索匹配的项目。");



     &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }


    ;&NBSP;&NBSP;&NBSP;&NBSP; }



SORTING



  
public
Form1()



      ;&NBSP;&NBSP; {



            InitializeComponent();



            lsvColumnSorter =
new ListViewColumnSorter (0);



           
.lsvItems.ListViewItemSorter = lsvColumnSorter;



    ;&NBSP;&NBSP;&NBSP;&NBSP; }





       
私有 ListViewColumnSorter lsvColumnSorter;


< p style ="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none">





   
private
void lsvItems_ColumnClick( object 发件人,
ColumnClickEventArgs e)



    &NBSP;&NBSP;&NBSP;&NBSP; {



           ((的 ListView的)发送方).ColumnSort(E,lsvColumnSorter);



  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }





     
public static
void
ColumnSort( this
ListView
lsv, ColumnClickEventArgs e,
ListViewColumnSorter
lsvColumnSorter)



&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {



           
//确定点击的列是否已经是正在排序的列。



           
if (e.Column == lsvColumnSorter.SortColumn)



            {



               
// Reverse the current sort direction for this column.



               
if (lsvColumnSorter.Order == System.Windows.Forms.SortOrder.Ascending)



                {



                    lsvColumnSorter.Order = System.Windows.Forms.SortOrder.Descending;



                }



               
else



                {



                    lsvColumnSorter.Order = System.Windows.Forms.SortOrder.Ascending;



                }



            }



           
else



            {



               
// Set the column number that is to be sorted; default to ascending.



                lsvColumnSorter.SortColumn = e.Column;



                lsvColumnSorter.Order = System.Windows.Forms.SortOrder.Ascending;



            }





           
// Perform the sort with these new sort options.



            lsv.Sort();


        }


In this specific page the column sorting code is effecting the searching code.


What could the problem be?





Debra has a question

解决方案

Hi Debra,


I suggest you could use the listview to search.


you could refer to this post:
http://stackoverflow.com/questions/16549823/filtering-items-in-a-listview


Use the listView item, not to reload the listViewitem.


And then i see you using the ListViewColumnSorter class, what is this class? Is it customized?


If you could share the more details, I could help you better.


Best regards,

Youjun Tang



I have code that allows a user to sort columns in a listview. I use it in many forms around my windows project. It works nicely on all pages and searching through the listview works nicely except for one page the searching is slowed down enormously when the column sorting is enabled but sorting works fine. Below is the code for that page.

I allow the users to search in the search box on top (not showing but it was a textbox the user can type in and it searches in the listview) and then it reloads the listview on bottom based on the search criteria.

Also the user can click on the column header in the listview and it will sort the columns….

SEARCH

   private void ProductSearch_TextChanged(object sender, EventArgs e)

        {

            ProductsSearch(btnAdvancedSearch.Text);

        }

    private void ProductsSearch(string filterOption)

        {

            StringBuilder sb = new StringBuilder();

            string ID = txtItemID.Text.EscapeLikeValue();

            string Name1 = txtItemName1.Text.EscapeLikeValue();

            string Name2 = txtItemName2.Text.EscapeLikeValue();

            string Name3 = txtItemName3.Text.EscapeLikeValue();

            if (ID.Length > 0)

            {

                sb.Append("ITEMSTRING like '" + ID + "%'");

            }

            if (Name1.Length > 0)

            {

                if (sb.Length > 0)

                {

                    sb.Append(" and ");

                }

                switch (filterOption)

                {

                    case "Basic Search":

                        sb.Append("' ' + Description like '%" + Name1 + "%'");

                        break;

                    case "Advanced Search":

                        sb.Append("Description like '" + Name1 + "%'");

                        break;

                }

            }

            if (Name2.Length > 0)

            {

                if (sb.Length > 0)

                {

                    sb.Append(" and ");

                }

                switch (filterOption)

                {

                    case "Basic Search":

                        sb.Append("' ' + Description like '% " + Name2 + "%'");

                        break;

                    case "Advanced Search":

                        sb.Append("Description like '% " + Name2 + "%'");

                        break;

                }

            }

            if (Name3.Length > 0)

            {

                if (sb.Length > 0)

                {

                    sb.Append(" and ");

                }

                switch (filterOption)

                {

                    case "Basic Search":

                        sb.Append("' ' + Description like '% " + Name3 + "%'");

                        break;

                    case "Advanced Search":

                        sb.Append("Description like '% " + Name3 + "%'");

                        break;

                }

            }

            lsvItems.Items.Clear();

            dtProducts.DefaultView.RowFilter = sb.ToString();

            foreach (DataRow row in dtProducts.Select(sb.ToString()))

            {

                ListViewItem lvi = new ListViewItem(row["Item"].ToString());

                lvi.SubItems.Add(row["Description"].ToString());

                lvi.SubItems.Add(row["Cost"].ToString());

                lvi.SubItems.Add(row["Price"].ToString());

                lsvItems.Items.Add(lvi);

            }

            if (dtProducts.DefaultView.Count <= 0)

            {

                Validation.ShowMessage(Main.lblStatus, "No items match your search.");

            }

        }

SORTING

   public Form1()

        {

            InitializeComponent();

            lsvColumnSorter = new ListViewColumnSorter(0);

            this.lsvItems.ListViewItemSorter = lsvColumnSorter;

        }

        private ListViewColumnSorter lsvColumnSorter;

    private void lsvItems_ColumnClick(object sender, ColumnClickEventArgs e)

        {

           ((ListView)sender).ColumnSort(e, lsvColumnSorter);

        }

      public static void ColumnSort(this ListView lsv, ColumnClickEventArgs e, ListViewColumnSorter lsvColumnSorter)

        {

            // Determine if clicked column is already the column that is being sorted.

            if (e.Column == lsvColumnSorter.SortColumn)

            {

                // Reverse the current sort direction for this column.

                if (lsvColumnSorter.Order == System.Windows.Forms.SortOrder.Ascending)

                {

                    lsvColumnSorter.Order = System.Windows.Forms.SortOrder.Descending;

                }

                else

                {

                    lsvColumnSorter.Order = System.Windows.Forms.SortOrder.Ascending;

                }

            }

            else

            {

                // Set the column number that is to be sorted; default to ascending.

                lsvColumnSorter.SortColumn = e.Column;

                lsvColumnSorter.Order = System.Windows.Forms.SortOrder.Ascending;

            }

            // Perform the sort with these new sort options.

            lsv.Sort();

        }

In this specific page the column sorting code is effecting the searching code.

What could the problem be?


Debra has a question

解决方案

Hi Debra,

I suggest you could use the listview to search.

you could refer to this post: http://stackoverflow.com/questions/16549823/filtering-items-in-a-listview

Use the listView item, not to reload the listViewitem.

And then i see you using the ListViewColumnSorter class, what is this class? Is it customized?

If you could share the more details, I could help you better.

Best regards,
Youjun Tang


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

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