问题描述
用listview现实数据时,想插入一条数据就显示新插入的数据,也就是想要插入跟现实的那种动态效果,哪位大侠知道应该怎么做啊?数据有两列:记录编号(用subitem.text设置)和记录值,然后试着用sort中降序排列,但是记录编号字段的类型是string,所以刚开始可以很好的降序显示当前新插入的数据,但是插入第11条记录时,会显示在1记录后面,如:987654321211101我想要的效果就是所有的都严格按照降序排列,请大侠们支招!!!
解决方案
解决方案二:
要不你就不按id来排序添加一个字段,默认值是当前时间这样你每加入一条记录会自动为当前时间再取数据的时候按时间降序来排列效果是一样的试试吧
解决方案三:
很显然sort排序是按字符来排序的,1以及1开头的当然排在2的后面,楼主的意思是按数字来排序对吧要改变这种状态,请设置ListViewItemSorter属性
解决方案四:
只是降序?我的方法比较不好,最好的解决方法是设置ListViewItemSorter属性记得msdn上例子,查下巴,下班了..listView1.Sorting=SortOrder.Descending;listView1.Sort();
解决方案五:
引用3楼ericzhangbo1982111的回复:
只是降序?我的方法比较不好,最好的解决方法是设置ListViewItemSorter属性记得msdn上例子,查下巴,下班了..listView1.Sorting=SortOrder.Descending;listView1.Sort();
解决方案六:
usingSystem;usingSystem.Windows.Forms;usingSystem.Drawing;usingSystem.Collections;namespaceListViewSortFormNamespace{publicclassListViewSortForm:Form{privateListViewlistView1;publicListViewSortForm(){//CreateListViewitemstoaddtothecontrol.ListViewItemlistViewItem1=newListViewItem(newstring[]{"Banana","a","b","c"},-1,Color.Empty,Color.Yellow,null);ListViewItemlistViewItem2=newListViewItem(newstring[]{"Cherry","v","g","t"},-1,Color.Empty,Color.Red,newFont("MicrosoftSansSerif",8.25F,FontStyle.Regular,GraphicsUnit.Point,((System.Byte)(0))));ListViewItemlistViewItem3=newListViewItem(newstring[]{"Apple","h","j","n"},-1,Color.Empty,Color.Lime,null);ListViewItemlistViewItem4=newListViewItem(newstring[]{"Pear","y","u","i"},-1,Color.Empty,Color.FromArgb(((System.Byte)(192)),((System.Byte)(128)),((System.Byte)(156))),null);//InitializetheListViewcontrolandaddcolumnstoit.this.listView1=newListView();//SettheinitialsortingtypefortheListView.this.listView1.Sorting=SortOrder.None;//Disableautomaticsortingtoenablemanualsorting.this.listView1.View=View.Details;//Addcolumnsandsettheirtext.this.listView1.Columns.Add(newColumnHeader());this.listView1.Columns[0].Text="Column1";this.listView1.Columns[0].Width=100;listView1.Columns.Add(newColumnHeader());listView1.Columns[1].Text="Column2";listView1.Columns.Add(newColumnHeader());listView1.Columns[2].Text="Column3";listView1.Columns.Add(newColumnHeader());listView1.Columns[3].Text="Column4";//Suspendcontrollogicuntilformisdoneconfiguringform.this.SuspendLayout();//AddItemstotheListViewcontrol.this.listView1.Items.AddRange(newListViewItem[]{listViewItem1,listViewItem2,listViewItem3,listViewItem4});//SetthelocationandsizeoftheListViewcontrol.this.listView1.Location=newPoint(10,10);this.listView1.Name="listView1";this.listView1.Size=newSize(300,100);this.listView1.TabIndex=0;//EnableeditingoftheitemsintheListView.this.listView1.LabelEdit=true;//ConnecttheListView.ColumnClickeventtotheColumnClickeventhandler.this.listView1.ColumnClick+=newColumnClickEventHandler(ColumnClick);//Initializetheform.this.ClientSize=newSize(400,400);this.Controls.AddRange(newControl[]{this.listView1});this.Name="ListViewSortForm";this.Text="SortedListViewControl";//Resumelayoutoftheform.this.ResumeLayout(false);}//ColumnClickeventhandler.privatevoidColumnClick(objecto,ColumnClickEventArgse){//SettheListViewItemSorterpropertytoanewListViewItemComparer//object.Settingthispropertyimmediatelysortsthe//ListViewusingtheListViewItemComparerobject.this.listView1.ListViewItemSorter=newListViewItemComparer(e.Column);}[System.STAThreadAttribute()]publicstaticvoidMain(){Application.Run(newListViewSortForm());}}//Implementsthemanualsortingofitemsbycolumns.classListViewItemComparer:IComparer{privateintcol;publicListViewItemComparer(){col=0;}publicListViewItemComparer(intcolumn){col=column;}publicintCompare(objectx,objecty){returnString.Compare(((ListViewItem)x).SubItems[col].Text,((ListViewItem)y).SubItems[col].Text);}}}