我们经常遇到这样的问题,字符串尾部带数字,如何正确排序;

首先设置GridView ,Columns 的相关列,设置属性中,SortMode为Custom

解决思路,把字符串尾缀数字,分离出来。先比较去除尾部数字的字符串,再比较尾部数字位。

gridView.CustomColumnSort += GridView_CustomColumnSort;

private void GridView_CustomColumnSort(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnSortEventArgs e)
{
if (e.Column == gridColumn)
{
string value1 = e.Value1.ToString();
string value2 = e.Value2.ToString();
var cc1 = GetNumberIndex(value1);
var cc2 = GetNumberIndex(value2);
if (cc1 > -1 && cc2 > -1)
{
string zm1 = value1.Substring(0, cc1);
string zm2 = value2.Substring(0, cc2);
if (zm1 == zm2)
{
string nb1 = value1.Substring(cc1);
string nb2 = value2.Substring(cc2);
decimal.TryParse(nb1, out decimal dec1);
decimal.TryParse(nb2, out decimal dec2);
int result = Comparer<decimal>.Default.Compare
(dec1, dec2);
e.Result = result;
e.Handled = true;
}
}
}
} private int GetNumberIndex(string value)
{
int v = -1;
if (value != null && value.Length > 1)
{
int i = 0;
for (; i < value.Length; i++)
{
if (char.IsNumber(value[i]))
{
if (i != 0)
{
v = i;
}
break;
}
}
}
return v;
}

C# DevExpress gridview 字符串尾部带数字如何排序的更多相关文章

  1. mysql—从字符串中提取数字(类型1)

    select reason,CHAR_LENGTH(reason),mid(reason,5,CHAR_LENGTH(reason)-5)+0 from `table` 解释: CHAR_LENGTH ...

  2. delphi 简单的删除字符串尾部数字的代码

    delphi  简单的删除字符串尾部数字的代码 方式一: function FilterShowName(const sName: String): String; var I: Integer; b ...

  3. 写出将字符串中的数字转换为整型的方法,如:“as31d2v”->312,并写出相应的单元测试,正则去掉非数值、小数点及正负号外的字符串

    写出将字符串中的数字转换为整型的方法,如:"as31d2v"->312,并写出相应的单元测试,输入超过int范围时提示不合法输入. public struct Convert ...

  4. 字符串--java中判断字符串是否为数字的方法的几种方法?

    ava中判断字符串是否为数字的方法: 1.用JAVA自带的函数 public static boolean isNumeric(String str){ for (int i = 0; i < ...

  5. C#验证字符串是否是数字,是否包括中文,是否是邮箱格式,是否是电话格式

    using System;     using System.Web;     using System.Text;     using System.Web.UI.WebControls;     ...

  6. java中判断字符串是否为数字的方法的几种方法

    1.用JAVA自带的函数 public static boolean isNumeric(String str){ for (int i = 0; i < str.length(); i++){ ...

  7. 关于怎样获取DevExpress GridView过滤后或排序后的数据集问题(转)

    GridView用自带的过滤功能过滤数据后,想要获取过滤后的数据集,有两种方式: 一.笨办法就是循环遍历GridView,根据gridView.GetRow()或者gridView.GetDataRo ...

  8. DevExpress GridView 整理(转)

    DevExpress GridView 那些事儿 1:去除 GridView 头上的 "Drag a column header here to group by that column&q ...

  9. DevExpress GridView 那些事儿

    1:去除 GridView 头上的 "Drag a column header here to group by that column" -->  点击 Run Desig ...

  10. DevExpress GridView 整理

    1:去除 GridView 头上的 "Drag a column header here to group by that column" -->  点击 Run Desig ...

随机推荐

  1. IIS添加MIME类型实现未知文件下载

    application/octet-stream 无需重启

  2. p标签设置行数,超出部分用省略号隐藏

    p { overflow: hidden; text-overflow: ellipsis; -webkit-line-clamp: 2; word-wrap: break-word; display ...

  3. gin web 2

    routers/router.go package routers import ( "github.com/gin-gonic/gin" "gin-blog/pkg/s ...

  4. django项目中使用swagger来实现接口文档自动生成

    一.Swagger 一般我们在对接前后端的时候,都需要提供相应的接口文档.对于后端来说,编写接口文档即费时费力,还会经常因为没有及时更新,导致前端对接时出现实际接口与文档不一致.而且手写接口文档还容易 ...

  5. C++实现链式表示多项式加法运算

    #include<iostream>#include<cstdlib>using namespace std;#define MAXSIZE 100#define OK 1#d ...

  6. 面试视频知识点整理1-7(http协议)

    http协议类 1)http协议的主要特点             简单快速   统一资源符 灵活          通过http协议,可以修改http头,完成不同数据类型的传输 无连接        ...

  7. windows环境go安装和goland激活

    1. go环境安装 1.1. go安装包下载 https://golang.google.cn/dl/ https://studygolang.com/dl 1.2. 安装go环境 1.2.1. 点击 ...

  8. ThinkPHP3.2设置异常页面404跳转

    在ThinkPHP3.2版本中当我们访问不存在的页面时会出现非常不友好错误提示页面. 解决办法: 1.在ThinkPHP3.2详细的介绍了该框架下的ThinkPHP惯例配置文件convention.p ...

  9. Java向MySQL写入中文乱码问题解决

    Java向MySQL写入中文乱码问题解决 以下仅为本人工作.学习过程中所接触到的内容,不足之处欢迎指出. 问题现象: 使用Java代码向MySQL数据库写入数据,中文字符出现乱码. 解决步骤: 1.查 ...

  10. 服务器新建分支,vscode检测不到

    执行    git remote update origin  命令,刷新远程分支