当前位置:网站首页>DevExpress中针对指定列进行百分比转换
DevExpress中针对指定列进行百分比转换
2022-08-05 05:40:00 【牛奶咖啡13】
一、业务需求
目前使用DevExpress控件开发的项目中,需要对表格(GirdView)的指定列或多列内容进行转为百分比显示(比如:将0.39转为39%以此类推)。
二、需求分析
实现对表格列的内容转为百分比有3种思路:
①直接对指定列的数据进行百分比处理,即(获取到指定列的数据,然后乘以100在添加百分比(%)符号);
②直接使用GridView自带的【DisplayFormat.FormatString】内容进行设置进行百分比显示(P2表示保留小数点后2位的百分比【比如:0.3956转为39.56%】);
③直接给数字后面添加百分比符号(比如:39转为39%)
三、实现方法
这里以使用GridView自带的【DisplayFormat.FormatString】内容进行设置进行百分比显示和直接给数字后面添加百分比符号(比如:39转为39%)进行实现说明:
3.1、必须先将需要转为百分比的列值转为数字格式
注意:转换格式方法必须在dataTable数据附值前
//设置列数据类型为double
dt.Columns[列名称].DataType = typeof(数据类型);
//比如:设置线宽数据类型为double
dt.Columns["TRIAL_LINEWIDTH"].DataType = typeof(double);
3.2、转换指定列或多列为百分比的代码
/// <summary>
/// 设置表格所有单元格都保留2位小数
/// </summary>
/// <param name="gridView">gridView组件</param>
public static void ResreserveTwoBit(GridView gridView)
{
if (gridView != null && gridView.Columns.Count > 0)
{
int count = gridView.Columns.Count;
for (int i = 0; i < count; i++)
{
gridView.Columns[i].DisplayFormat.FormatType = FormatType.Numeric;
gridView.Columns[i].DisplayFormat.FormatString = "F2";
}
}
}
/// <summary>
/// 设置表格指定单元格都保留2位小数
/// </summary>
/// <param name="gridView">gridView组件</param>
/// <param name="startColumnIndex">开始列索引</param>
/// <param name="endColumnIndex">结束列索引</param>
public static void ResreserveTwoBit(GridView gridView, int startColumnIndex, int endColumnIndex)
{
if (gridView != null && gridView.Columns.Count > 0 &&
startColumnIndex > 0 && endColumnIndex > 0 &&
endColumnIndex <= gridView.Columns.Count)
{
//int count = gridView.Columns.Count;
for (int i = startColumnIndex; i <= endColumnIndex; i++)
{
gridView.Columns[i].DisplayFormat.FormatType = FormatType.Numeric;
gridView.Columns[i].DisplayFormat.FormatString = "F2";
}
}
}
/// <summary>
/// 设置表格指定单元格都保留2位小数
/// </summary>
/// <param name="gridView">gridView组件</param>
/// <param name="startColumnIndex">开始列索引</param>
/// <param name="endColumnIndex">结束列索引</param>
public static void ResreserveTwoBit(GridView gridView, string columnName)
{
if (gridView != null && gridView.Columns.Count > 0 && !string.IsNullOrEmpty(columnName))
{
gridView.Columns[columnName].DisplayFormat.FormatType = FormatType.Numeric;
gridView.Columns[columnName].DisplayFormat.FormatString = "F2";
}
}
/// <summary>
/// 设置表格指定单元格都使用百分比
/// </summary>
/// <param name="gridView">gridView组件</param>
/// <param name="startColumnIndex">开始列索引</param>
/// <param name="endColumnIndex">结束列索引</param>
public static void SetPercentage(GridView gridView, int startColumnIndex, int endColumnIndex)
{
if (gridView != null && gridView.Columns.Count > 0 &&
startColumnIndex > 0 && endColumnIndex > 0 &&
endColumnIndex <= gridView.Columns.Count)
{
//int count = gridView.Columns.Count;
for (int i = startColumnIndex; i <= endColumnIndex; i++)
{
gridView.Columns[i].DisplayFormat.FormatType = FormatType.Numeric;
gridView.Columns[i].DisplayFormat.FormatString = "P2";
}
}
}
/// <summary>
/// 设置表格指定单元格都使用百分比
/// </summary>
/// <param name="gridView">gridView组件</param>
/// <param name="columnName">列名称</param>
public static void SetPercentage(GridView gridView, string columnName)
{
if (gridView != null && gridView.Columns.Count > 0 && !string.IsNullOrEmpty(columnName))
{
gridView.Columns[columnName].DisplayFormat.FormatType= FormatType.Numeric;
gridView.Columns[columnName].DisplayFormat.FormatString = "P2";
}
}
/// <summary>
/// 设置表格指定单元格都使用百分比
/// </summary>
/// <param name="gridView">gridView组件</param>
/// <param name="columnName">列名称</param>
/// <param name="keepDecimalPlaces">保留的小数位数(默认保留2位)</param>
public static void SetPercentage(GridView gridView, string columnName,string keepDecimalPlaces="P2")
{
if (gridView != null && gridView.Columns.Count > 0 && !string.IsNullOrEmpty(columnName) && !string.IsNullOrEmpty(keepDecimalPlaces))
{
gridView.Columns[columnName].DisplayFormat.FormatType = FormatType.Numeric;
gridView.Columns[columnName].DisplayFormat.FormatString = keepDecimalPlaces;
}
}
/// <summary>
/// 给表格指定单元格都保留2位小数后添加%号
/// </summary>
/// <param name="gridView"></param>
/// <param name="startColumnIndex"></param>
/// <param name="endColumnIndex"></param>
public static void SetResreserveTwoBitAndPercentageChar(GridView gridView, int startColumnIndex, int endColumnIndex)
{
if (gridView != null && gridView.Columns.Count > 0 &&
startColumnIndex > 0 && endColumnIndex > 0 &&
endColumnIndex <= gridView.Columns.Count)
{
for (int i = startColumnIndex; i <= endColumnIndex; i++)
{
gridView.Columns[i].DisplayFormat.FormatType = FormatType.Numeric;
gridView.Columns[i].DisplayFormat.FormatString = $"{0:N2}'%'";
}
}
}
/// <summary>
/// 给表格指定单元格都保留2位小数后添加%号
/// </summary>
/// <param name="gridView"></param>
/// <param name="columnName">列名称</param>
public static void SetResreserveTwoBitAndPercentageChar(GridView gridView, string columnName)
{
if (gridView != null && gridView.Columns.Count > 0 && !string.IsNullOrEmpty(columnName))
{
gridView.Columns[columnName].DisplayFormat.FormatType = FormatType.Numeric;
gridView.Columns[columnName].DisplayFormat.FormatString = $"{0:N2}'%'";
}
}
/// <summary>
/// 给表格指定单元格都添加%符号,不做运算
/// </summary>
/// <param name="gridView"></param>
/// <param name="columnName"></param>
public static void SetPercentageChar(GridView gridView, string columnName)
{
if (gridView != null && gridView.Columns.Count > 0 && !string.IsNullOrEmpty(columnName))
{
gridView.Columns[columnName].DisplayFormat.FormatType = FormatType.Numeric;
gridView.Columns[columnName].DisplayFormat.FormatString = $"{0}'%'";
}
}
边栏推荐
猜你喜欢
【FAQ】CCAPI Compatible EOS Camera List (Updated in August 2022)
单片机原理与应用复习
人人AI(吴恩达系列)
DevOps-了解学习
DevOps - Understanding Learning
lingo入门——河北省第三届研究生建模竞赛B题
更改小程序原生radio的颜色及大小
The cocos interview answers you are looking for are all here!
Cloud Computing Basics - Study Notes
LeetCode practice and self-comprehension record (1)
随机推荐
小程序input框不允许输入负数
记录vue-页面缓存问题
错误记录集锦(遇到则记下)
UI刘海屏适配方式
After docker is deployed, mysql cannot connect
lingo入门——河北省第三届研究生建模竞赛B题
In-depth analysis if according to data authority @datascope (annotation + AOP + dynamic sql splicing) [step by step, with analysis process]
【内推】新相微电子
【FAQ】什么是 Canon CCAPI
H5开发调试-Fiddler手机抓包
八大排序之堆排序
Error correction notes for the book Image Processing, Analysis and Machine Vision
LaTeX image captioning text column automatic line wrapping
Tips for formatting code indentation
文本样式这一篇文章就够了
DisabledDate date picker datePicker
unity 将Text批量替换为TextMeshProUGUI
盒子模型小练习
深夜小酌,50道经典SQL题,真香~
设置文本向两边居中展示