简体   繁体   English

radgridview 上的条件格式以删除“-” - C#

[英]Conditional formatting on a radgridview to remove '-' - C#

I have a rad grid view which contains data relating to stock movement.我有一个 rad 网格视图,其中包含与股票走势相关的数据。 The values in a column named will all be pulled through as negative.命名的列中的值都将作为负数通过。 I want to add formatting so that the negative is removed from the number.我想添加格式,以便从数字中删除负数。

I think conditional formatting is how I want to do this, I'm unsure of what I want to do after the condition is met.我认为条件格式是我想要的方式,我不确定在满足条件后我想做什么。

ConditionalFormattingObject obj = new ConditionalFormattingObject("MyCondition", ConditionTypes.StartsWith, "-", "", false);
obj.CellBackColor = Color.SkyBlue;
obj.CellForeColor = Color.Red;
obj.TextAlignment = ContentAlignment.MiddleRight;
this.rgv_orderLines.Columns["Moved so Far"].ConditionalFormattingObjectList.Add(obj);

The above code will turn the cells blue when the condition is met.当条件满足时,上面的代码会将单元格变成蓝色。 I however, don't want this I want the '-' to be removed.但是,我不希望这样,我希望删除'-'

EDIT: The following code gives the desired result, however, without the try catch it crashes on any cells that don't contain a value.编辑:下面的代码给出了所需的结果,但是,如果没有 try catch 它会在任何不包含值的单元格上崩溃。

    private void rgv_orderLines_CellFormatting(object sender, CellFormattingEventArgs e)
    {
        if (e.CellElement.ColumnInfo.Name == "Moved so Far")
        {
            try
            {
                string value = e.CellElement.Value.ToString();
                value = value.Replace("-", "");
                e.CellElement.Value = value;
            }
            catch (Exception ex)
            {

            }
        }
    }

ConditionalFormattingObject is used only to style the row, ie setting fonts and colours. ConditionalFormattingObject仅用于设置行的样式,即设置字体和颜色。 It does not have functionality for formatting data values.它没有格式化数据值的功能。

Ideally, you should store the data as positive numbers in the first place, as that makes sorting, filtering etc. much easier.理想情况下,您应该首先将数据存储为正数,因为这使得排序、过滤等更加容易。 But if you do want to just display the value as positive, you should make sure the column is a GridViewDecimalColumn and set the FormatString of the column to something like但是,如果您只想将该值显示为正数,则应确保该列是GridViewDecimalColumn并将该列的FormatString设置为类似

"#.###;#.###;0"

This means display the value with 3 decimal places, for both positive and negative numbers, and as 0 for 0.这意味着将值显示为 3 位小数,无论是正数还是负数,0 都显示为 0。

If it is a string column, your code wold work, just check if the Value is null before formatting it:如果它是一个字符串列,您的代码可以工作,只需在格式化之前检查 Value 是否为空:

private void rgv_orderLines_CellFormatting(object sender, CellFormattingEventArgs e)
{
    if (e.CellElement.ColumnInfo.Name == "Moved so Far")
    {
        if(e.CellElement.Value != null)
        {
            string value = e.CellElement.Value.ToString();
            value = value.Replace("-", "");
            e.CellElement.Value = value;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM