int FillList(out string strError) { strError = ""; this.listView_columns.Items.Clear(); if (String.IsNullOrEmpty(this.DefString) == true) { return(0); } this.columns = new SortColumnCollection(); this.columns.Build(this.DefString); for (int i = 0; i < this.columns.Count; i++) { SortColumn column = this.columns[i]; string strColumnName = GetColumnName(column.nColumnNumber); ListViewItem item = new ListViewItem((i + 1).ToString(), 0); item.SubItems.Add(strColumnName); item.SubItems.Add(column.bAsc == true ? "升" : "降"); item.SubItems.Add(column.dataType.ToString()); item.SubItems.Add(column.bIgnorCase == true ? "是" : "否"); this.listView_columns.Items.Add(item); } return(0); }
// Calls CaseInsensitiveComparer.Compare with the parameters reversed. int IComparer <Line> .Compare(Line line1, Line line2) { /* * if (!(x is Line)) * throw new ArgumentException("object x is not a Line"); * if (!(y is Line)) * throw new ArgumentException("object y is not a Line"); * * * Line line1 = (Line)x; * Line line2 = (Line)y; */ if (sortstyle == null) { throw (new Exception("sortstyle尚未创建")); } for (int i = 0; i < this.sortstyle.Count; i++) { SortColumn column = (SortColumn)this.sortstyle[i]; int nRet = 0; // 取行标题进行比较 if (column.nColumnNumber == -1) { nRet = column.CompareString(line1.strKey, line2.strKey); if (nRet != 0) { return(nRet); } #if NO if (column.dataType == DataType.Auto || column.dataType == DataType.Number) { if (line1.strKey.Length == line2.strKey.Length) { nRet = String.Compare(line1.strKey, line2.strKey, column.bIgnorCase); } else { // 右对齐? string s1 = line1.strKey; string s2 = line2.strKey; if (s1.Length < s2.Length) { s1 = s1.PadLeft(s2.Length, ' '); } else if (s1.Length > s2.Length) { s2 = s2.PadLeft(s1.Length, ' '); } nRet = String.Compare(s1, s2, column.bIgnorCase); } if (column.bAsc == false) { nRet = nRet * (-1); } if (nRet != 0) { return(nRet); } } else { nRet = String.Compare(line1.strKey, line2.strKey, column.bIgnorCase); if (column.bAsc == false) { nRet = nRet * (-1); } if (nRet != 0) { return(nRet); } } #endif } else { object o1 = null; if (column.nColumnNumber < line1.cells.Length) { o1 = line1.cells[column.nColumnNumber]; } object o2 = null; if (column.nColumnNumber < line2.cells.Length) { o2 = line2.cells[column.nColumnNumber]; } nRet = column.CompareObject(o1, o2); if (nRet != 0) { return(nRet); } #if NO if (column.dataType == DataType.Auto || column.dataType == DataType.Number) { Int64 n1 = 0; Int64 n2 = 0; string s1 = null; string s2 = null; bool bException = false; if ((o1 is Int32) || (o1 is Int64)) { n1 = (Int64)o1; } else if (o1 is string) { try { n1 = Convert.ToInt64((string)o1); // 可能抛出异常 } catch { s1 = (string)o1; bException = true; } } if ((o2 is Int32) || (o2 is Int64)) { n2 = (Int64)o2; if (bException == true) { s2 = Convert.ToString(n2); } } else if (o2 is string) { if (bException == true) { s2 = (string)o2; } else { try { n2 = Convert.ToInt64((string)o2); } catch { s2 = (string)o2; bException = true; s1 = Convert.ToString(n1); } } } if (bException == true) { // 对齐 int nMaxLength = Math.Max(s1.Length, s2.Length); s2 = s2.PadLeft(nMaxLength, '0'); s1 = s1.PadLeft(nMaxLength, '0'); nRet = String.Compare(s1, s2, column.bIgnorCase); if (column.bAsc == false) { nRet = nRet * (-1); } if (nRet != 0) { return(nRet); } } else { Int64 n64Ret = n1 - n2; if (column.bAsc == false) { n64Ret = n64Ret * (-1); } if (n64Ret != 0) { return((int)n64Ret); } } } else if (column.dataType == DataType.String) { string s1 = ""; string s2 = ""; if ((o1 is Int32) || (o1 is Int64)) { s1 = Convert.ToString((Int64)o1); } else if (o1 is string) { s1 = (string)o1; } if ((o2 is Int32) || (o2 is Int64)) { s2 = Convert.ToString((Int64)o2); } else if (o2 is string) { s2 = (string)o2; } nRet = String.Compare(s1, s2, column.bIgnorCase); if (column.bAsc == false) { nRet = nRet * (-1); } if (nRet != 0) { return(nRet); } } #endif } // end of else } // end of loop return(0); }