您的位置:首页 > 其它

关于listview列排序后,重新再加载数据报错的解决方案

2007-10-11 12:45 260 查看
ListView的ColumnClick事件如下:
private void eqStateList_ColumnClick(object sender, ColumnClickEventArgs e)
{
string Asc = ((char)0x25bc).ToString().PadLeft(4, ' ');
string Des = ((char)0x25b2).ToString().PadLeft(4, ' ');
if (sort == false)
{
sort = true;
string oldStr = this.eqStateList.Columns[e.Column].Text.TrimEnd((char)0x25bc, (char)0x25b2, ' ');
this.eqStateList.Columns[e.Column].Text = oldStr + Des;
}
else if (sort == true)
{
sort = false;
string oldStr = this.eqStateList.Columns[e.Column].Text.TrimEnd((char)0x25bc, (char)0x25b2, ' ');
this.eqStateList.Columns[e.Column].Text = oldStr + Asc;
}
eqStateList.ListViewItemSorter = new ListViewItemComparer(e.Column, sort);
this.eqStateList.Sort();
int rowCount = this.eqStateList.Items.Count;
if (currentCol != -1)
{
for (int i = 0; i < rowCount; i++)
{
this.eqStateList.Items[i].UseItemStyleForSubItems = false;
this.eqStateList.Items[i].SubItems[currentCol].BackColor = Color.White;
if (e.Column != currentCol)
this.eqStateList.Columns[currentCol].Text = this.eqStateList.Columns[currentCol].Text.TrimEnd((char)0x25bc, (char)0x25b2, ' ');
}
}
for (int i = 0; i < rowCount; i++)
{
this.eqStateList.Items[i].UseItemStyleForSubItems = false;
this.eqStateList.Items[i].SubItems[e.Column].BackColor = Color.WhiteSmoke;
currentCol = e.Column;
}
}
排序比较类:
public class ListViewItemComparer : IComparer
{

public bool sort_b;
public SortOrder order = SortOrder.Ascending;

private int col;

public ListViewItemComparer()
{
col = 0;
}

public ListViewItemComparer(int column, bool sort)
{
col = column;
sort_b = sort;
}

public int Compare(object x, object y)
{
int returnVal = 0;
if (sort_b)
{
returnVal = String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
}
else
{
returnVal = String.Compare(((ListViewItem)y).SubItems[col].Text, ((ListViewItem)x).SubItems[col].Text);
}
return returnVal;
}
}

以上在第一次查询数据时可以正常排序,但如果排序后在进行其他条件查询数据时,会报(lnvalidArgument=“2”的值对于“index”无效)这个错误,原因是重新加载的数据表结构与开始排序的表结构不同,[COL]超出了范围,解决办法是在listview排序的时候先添加listView1.ListViewItemSorter = null;,或者是在查询数据时将listView1.ListViewItemSorter = null;。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐