关于LISTBOX排序问题

vagaas 2012-02-27 03:49:19

001 张三
002 李四
003 王五

当我点击李四上升的时候应该显示为
001 李四
002 张三
003 王五

数据都是保存在实体类里,SID,SNAME!
如何控制ID始终是001,002,003 依次显示?
...全文
223 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
youzelin 2012-02-27
  • 打赏
  • 举报
回复
根据你说的东西,如果是 ListBox 的话应该只有一列,那也就是说。。。。。。“001 张三” 是一个item?
这也太别扭了吧,怎么说你也得至少用 ListView 控件吧。否则排序的话,截取字符串会很罗嗦。
阿非 2012-02-27
  • 打赏
  • 举报
回复
序号 显示的顺序就是显示的顺序

不要让它参与排序,序号是不变的

真正变的是姓名列 和 姓名列对应的编号
卡索瓦 2012-02-27
  • 打赏
  • 举报
回复

private void button1_Click_1(object sender, EventArgs e)
{
listBox1.Items.Add("001\t张三");
listBox1.Items.Add("002\t李四");
listBox1.Items.Add("003\t王五");
}
/* 001 张三
* 002 李四
* 003 王五
* 每行间隔是以制表位(Tab)"\t"为分隔符
*/
//向上移动
private void button2_Click(object sender, EventArgs e)
{
int index = listBox1.SelectedIndex;
if (index == -1) return;
if (index == 0) return;
string []strs = listBox1.Items[index].ToString().Split('\t');
string[] strs2 = listBox1.Items[index-1].ToString().Split('\t');
string temp = string.Empty;
temp = strs[1];
strs[1] = strs2[1];
strs2[1] = temp;
listBox1.Items[index] = strs[0] + "\t" + strs[1];
listBox1.Items[index-1] = strs2[0] + "\t" + strs2[1];
listBox1.SelectedIndex = index - 1;
}
//向下移动
private void button3_Click(object sender, EventArgs e)
{
int index = listBox1.SelectedIndex;
if (index == -1) return;
if (index == listBox1.Items.Count-1) return;
string[] strs = listBox1.Items[index].ToString().Split('\t');
string[] strs2 = listBox1.Items[index + 1].ToString().Split('\t');
string temp = string.Empty;
temp = strs[1];
strs[1] = strs2[1];
strs2[1] = temp;
listBox1.Items[index] = strs[0] + "\t" + strs[1];
listBox1.Items[index + 1] = strs2[0] + "\t" + strs2[1];
listBox1.SelectedIndex = index+ 1;
}

在界面调整结束后,可以遍历listBox1更新一下数据库即可。
vagaas 2012-02-27
  • 打赏
  • 举报
回复
GO ON UP
bdmh 2012-02-27
  • 打赏
  • 举报
回复
第一,顺序填充一下
第二,drawitem上画一下
第三,排序时,做处理,不交换第一列的位置

110,538

社区成员

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

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

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