请人帮忙解决一下这个排序的问题,用

renqiang_888 2009-12-17 12:12:14
datagrideview中有一列可能是数字也可能是字符串,我想这样:如果这一列中全是数字的话就按整形的排序,如果有字符串的话就按字符串的顺序排序,请问各位高手有没有实现方法!(我的datagrideview是没有绑定数据源,是一行一行add进去的)















讲故事
...全文
85 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
babyofbaby 2009-12-17
  • 打赏
  • 举报
回复
不用判断,不管有没有字符串,都按字符串排序。就算全是数字,按字符串排序的结果和按整形排的结果是一样的。
Fibona 2009-12-17
  • 打赏
  • 举报
回复
把他的数据放到一个对像中,

如 public class Class1: IComparable<Class1>
{

public string _id = String.Empty;

/// <summary>
///自定义比较器
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public int CompareTo(Class1 other)
{
if (other == null)
return 1;
if (this == null)
return -1;
int _idthis=0;
int _idother=0;
if(int.TryParase(this.Id,out _idthis) && int.TryParse(other.Id,out _idother))
{
return _idthis.CompareTo(_idother);
}
else if(int.TryParase(this.Id,out _idthis))
{
return 1;
)
else if(int.TryParase(other.Id,out _idother))
{
return _1;
)

return this.Id.CompareTo(other.Id);
}

如:List<Class1> list=new List<Class1>();
list.Add(new Class1("111"));
list.Add(new Class1("2222"));
list.Add(new Class1("112D1"));
list.Add(new Class1("222C2"));

list.sort(); //进行排序
sito_hongta 2009-12-17
  • 打赏
  • 举报
回复
不是高手幫頂,
逐行判斷該列是否都為數字或字符!

110,502

社区成员

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

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

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