数据排序的问题

net0003 2008-06-17 10:25:34
向listview中插入数据,根据第二列的数据大小升序插入到相应的位置,

private void Add(string St,string PlanCoil,string Coil,string Dt,string Row,string Out,string Level)//插入数据
{
for(int i=0;i<=this.lVw .Items .Count -2;i++)
{
if(Coil.ToString ()!=this.lVw .Items [i].SubItems [1].Text )
{
if(this.lVw .Items [i].SubItems [0].Text .CompareTo (this.lVw .Items [i+1].SubItems [0].Text )<0)
{
this.lVw .Items .Insert (i+1,new ListViewItem(new string[]{St.ToString (),PlanCoil.ToString (),Coil.ToString (),Dt.ToString (),Row.ToString (),Out.ToString (),Level.ToString ()}));
//break;
}
else
{
this.lVw .Items .Insert (i,new ListViewItem(new string[]{St.ToString (),PlanCoil.ToString (),Coil.ToString (),Dt.ToString (),Row.ToString (),Out.ToString (),Level.ToString ()}));
//break;
}
}
else
{
MessageBox.Show (" ","");
}
}


这样写,是个死循环,而且
if(this.lVw .Items [i].SubItems [0].Text .CompareTo (this.lVw .Items [i+1].SubItems [0].Text )<0
这个也不对,因为这样只要<0就插入数据了,况且 ,写在for里面,虚幻插入了,大家给我个思路就行
...全文
148 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
ericzhangbo1982111 2008-06-17
  • 打赏
  • 举报
回复
回复我居然看不到.
net0003 2008-06-17
  • 打赏
  • 举报
回复
哈哈 ,sorry
lovefootball 2008-06-17
  • 打赏
  • 举报
回复
你看看你的Coil到底是第几列~~~~~~
net0003 2008-06-17
  • 打赏
  • 举报
回复
另外根据第二列 应该是
 
if (lVw.Items[i].SubItems[1].Text.CompareTo(Coil) > 0)//判断第二列,如果找到比他大的则插在前面
net0003 2008-06-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 lovefootball 的回复:]
上面Add错了
this.lVw.Items.Add(new ListViewItem(new string[]{St,PlanCoil,Coil,Dt,Row,Out,Level}));
[/Quote]



你给的差不多了,现在测试没什么毛病,等我在看看
net0003 2008-06-17
  • 打赏
  • 举报
回复
我知道有排序功能 ,我是要进行双重排序,也就是我用了listview进行排序后还要进行次排序
ericzhangbo1982111 2008-06-17
  • 打赏
  • 举报
回复
......
listview有排序功能。
为什么要自己算

 public class ListViewColumnSorter : IComparer
{

int index;
/// </summary>
public ListViewColumnSorter(int x)
{
index = x;
}

/// <summary>
/// This method is inherited from the IComparer interface. It compares the two objects passed using a case insensitive comparison.
/// </summary>
/// <param name="x">First object to be compared</param>
/// <param name="y">Second object to be compared</param>
/// <returns>The result of the comparison. "0" if equal, negative if 'x' is less than 'y' and positive if 'x' is greater than 'y'</returns>
public int Compare(object x, object y)
{
int compareResult;
ListViewItem listviewX, listviewY;

// Cast the objects to be compared to ListViewItem objects
listviewX = (ListViewItem)x;
listviewY = (ListViewItem)y;

// Compare the two items
return compareResult = -string.Compare(listviewX.SubItems[index].Text, listviewY.SubItems[index].Text);


}




 ListViewColumnSorter sor = new ListViewColumnSorter(e.Column);
listView1.ListViewItemSorter = sor;
listView1.Sorting = listView1.Sorting == SortOrder.Descending ? SortOrder.Ascending : SortOrder.Descending;
listView1.Sort();
ericzhangbo1982111 2008-06-17
  • 打赏
  • 举报
回复
......
listview有排序功能。
为什么要自己算

 public class ListViewColumnSorter : IComparer
{

int index;
/// </summary>
public ListViewColumnSorter(int x)
{
index = x;
}

/// <summary>
/// This method is inherited from the IComparer interface. It compares the two objects passed using a case insensitive comparison.
/// </summary>
/// <param name="x">First object to be compared</param>
/// <param name="y">Second object to be compared</param>
/// <returns>The result of the comparison. "0" if equal, negative if 'x' is less than 'y' and positive if 'x' is greater than 'y'</returns>
public int Compare(object x, object y)
{
int compareResult;
ListViewItem listviewX, listviewY;

// Cast the objects to be compared to ListViewItem objects
listviewX = (ListViewItem)x;
listviewY = (ListViewItem)y;

// Compare the two items
return compareResult = -string.Compare(listviewX.SubItems[index].Text, listviewY.SubItems[index].Text);


}




 ListViewColumnSorter sor = new ListViewColumnSorter(e.Column);
listView1.ListViewItemSorter = sor;
listView1.Sorting = listView1.Sorting == SortOrder.Descending ? SortOrder.Ascending : SortOrder.Descending;
listView1.Sort();
lovefootball 2008-06-17
  • 打赏
  • 举报
回复


你看看你的代码跟我给你的一样么???
我的for里面有else???
你就直接把我的代码拷贝就可以了
为什么要改我的代码??
net0003 2008-06-17
  • 打赏
  • 举报
回复

private void Form1_Load(object sender, System.EventArgs e)
{
//测试用数据

this.lVw .Items .Add (new ListViewItem(new string[] {"0","AH0000T008", "00100T", "01","1" ,"2" ,"2"}));
this.lVw .Items .Add (new ListViewItem(new string[] {"2","AH0000T004", "00200T", "02","2" ,"2" ,"2"}));
this.lVw .Items .Add (new ListViewItem(new string[] {"2","CH0000T005", "00300T", "02","2" ,"2" ,"2"}));
// this.lVw .Items .Add (new ListViewItem(new string[] {"1","DH0000T007", "00400T", "02","1" ,"2" ,"2"}));
// this.lVw .Items .Add (new ListViewItem(new string[] {"0","AH0000T000", "00500T", "01","2" ,"2" ,"2"}));
// this.lVw .Items .Add (new ListViewItem(new string[] {"2","FH0000T001", "00600T", "01","1" ,"2" ,"2"}));
// this.lVw .Items .Add (new ListViewItem(new string[] {"1","GH0000T002", "00700T", "02","1" ,"2" ,"2"}));


Add("0","AH0000T001", "00500T", "01","1" ,"2" ,"2");
Add("2","AH0000T005", "00300T", "02","2" ,"2" ,"2");
Add("1","GH0000T002", "00700T", "02","1" ,"2" ,"2");
Add("1","DH0000T007", "00400T", "02","1" ,"2" ,"2");



测试下看看
net0003 2008-06-17
  • 打赏
  • 举报
回复
========
net0003 2008-06-17
  • 打赏
  • 举报
回复

private void Add(string St,string PlanCoil,string Coil,string Dt,string Row,string Out,string Level)//插入数据
{
for (int i = 0; i < this.lVw.Items.Count - 1; i++)
{
if (lVw.Items[i].SubItems[1].Text.CompareTo(Coil) > 0)//判断第二列,如果找到比他大的则插在前面
{
this.lVw.Items.Insert(i,new ListViewItem(new string[]{St,PlanCoil,Coil,Dt,Row,Out,Level}));
break;
}
else
{
this.lVw.Items.Add(new ListViewItem(new string[]{St,PlanCoil,Coil,Dt,Row,Out,Level}));
break;
}
}
}
private void Form1_Load(object sender, System.EventArgs e)
{
//测试用数据

this.lVw .Items .Add (new ListViewItem(new string[] {"0","AH0000T008", "00100T", "01","1" ,"2" ,"2"}));
this.lVw .Items .Add (new ListViewItem(new string[] {"2","AH0000T004", "00200T", "02","2" ,"2" ,"2"}));


Add("0","AH0000T001", "00500T", "01","1" ,"2" ,"2");
Add("2","AH0000T005", "00300T", "02","2" ,"2" ,"2");




结果不对
lovefootball 2008-06-17
  • 打赏
  • 举报
回复
不对


bool isInsert = false;
for (int i = 0; i < lVw.Items.Count - 1; i++)
{
if (lVw.Items[i].SubItems[2].Text.CompareTo(Coil) > 0)//判断第二列,如果找到比他大的则插在前面
{
this.lVw.Items.Insert(i,new ListViewItem(new string[]{St,PlanCoil,Coil,Dt,Row,Out,Level}));
isInsert = true;
break;
}
}
if (!isInsert)
{
//如果没找到则添加到最后
this.lVw.Items.Add(new ListViewItem(new string[]{St,PlanCoil,Coil,Dt,Row,Out,Level}));
}



在for里面为什么要else??
net0003 2008-06-17
  • 打赏
  • 举报
回复

for (int i = 0; i < this.lVw.Items.Count - 1; i++)
{
if (lVw.Items[i].SubItems[2].Text.CompareTo(Coil) > 0)//判断第二列,如果找到比他大的则插在前面
{
this.lVw.Items.Insert(i,new ListViewItem(new string[]{St,PlanCoil,Coil,Dt,Row,Out,Level}));
isInsert = true;
break;
}
else
{
this.lVw.Items.Insert(i,new ListViewItem(new string[]{St,PlanCoil,Coil,Dt,Row,Out,Level}));
break;
}
}



这样对不
sxmonsy 2008-06-17
  • 打赏
  • 举报
回复
数据源按第二列数据排序.排序完后直接绑定到ListView就行了吧?
lovefootball 2008-06-17
  • 打赏
  • 举报
回复
上面Add错了
this.lVw.Items.Add(new ListViewItem(new string[]{St,PlanCoil,Coil,Dt,Row,Out,Level}));
lovefootball 2008-06-17
  • 打赏
  • 举报
回复
你在插入数据之前数据源应该已经是排序好的了吧??
如果是的话参考如下代码


bool isInsert = false;
for (int i = 0; i < lVw.Items.Count - 1; i++)
{
if (lVw.Items[i].SubItems[2].Text.CompareTo(Coil) > 0)//判断第二列,如果找到比他大的则插在前面
{
this.lVw.Items.Insert(i,new ListViewItem(new string[]{St,PlanCoil,Coil,Dt,Row,Out,Level}));
isInsert = true;
break;
}
}
if (!isInsert)
{
//如果没找到则添加到最后
this.lVw.Items.Add(i,new ListViewItem(new string[]{St,PlanCoil,Coil,Dt,Row,Out,Level}));
}

xinheixia 2008-06-17
  • 打赏
  • 举报
回复
顶一个再看
代码阿
ruoyanzhang 2008-06-17
  • 打赏
  • 举报
回复
up

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧