关于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;。
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;。
相关文章推荐
- 关于listview加载图片时oom的解决方案
- 关于listview加载多种item的解决方案
- 关于ListView滑动加载不流畅
- Android ListView异步加载图片乱序问题,原因分析及解决方案
- 关于jquery+ajax+json+ssh框架整合,数值异步的问题(暂时解决方案--延迟加载(lazy)改为急加载)
- Android ListView异步加载图片错位解决方案一 使用findViewWithTag
- Android ListView异步加载图片乱序问题,原因分析及解决方案
- 主题:关于hibernate延迟加载的错误解决方案
- 【Android开发入门】关于ListView中按钮监听器设置的解决方案
- 关于ListView中包含EditText数据复用引起异常的解决方案
- 关于Vs2012重新生成解决方案时提示exe访问被拒绝的处理方式
- listview、gridview上滑加载更多图片,内存溢出outofmemery的解决方案
- 关于使用百度富文本编辑器UE重新加载数据的问题
- 关于SQL2008 “不允许保存更改。您所做的更改要求删除并重新创建以下表。您对无法重新创建的标进行了更改或者启用了‘阻止保存要求重新创建表的更改’” 解决方案
- Android ListView异步加载图片乱序问题,原因分析及解决方案(二)
- Android ListView异步加载图片乱序问题,原因分析及解决方案
- Android ListView异步加载图片乱序问题,原因分析及解决方案
- 加载ajax内容到页面后的样式重新渲染需$().listview('refresh');
- Android中ListView异步加载图片错位、重复、闪烁问题分析及解决方案
- 关于LayoutInflater动态加载View到AbsListView和ViewGroup之间的区别