110,533
社区成员
发帖
与我相关
我的任务
分享
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 (lVw.Items[i].SubItems[1].Text.CompareTo(Coil) > 0)//判断第二列,如果找到比他大的则插在前面
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();
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();
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");
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");
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 (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;
}
}
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}));
}