Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用N/a替换C#winforms datagridview的负值_C#_Datagridview - Fatal编程技术网

如何用N/a替换C#winforms datagridview的负值

如何用N/a替换C#winforms datagridview的负值,c#,datagridview,C#,Datagridview,我有一个c#winforms datagridview,它充满了从数据库中获取的数据。现在,我希望网格中所有列的负值都替换为N/A。订阅CellFormatting事件。在事件处理程序中,检查该值是否为负值,然后将该值设置为N/A。在示例列中,我已对第一列执行了此操作。这里的一个问题是,新值应该是由cellFormattedValueType属性指定的类型 示例代码: void dataGridView1_CellFormatting(object sender, DataGridViewCel

我有一个c#winforms datagridview,它充满了从数据库中获取的数据。现在,我希望网格中所有列的负值都替换为N/A。

订阅
CellFormatting
事件。在事件处理程序中,检查该值是否为负值,然后将该值设置为N/A。在示例列中,我已对第一列执行了此操作。这里的一个问题是,新值应该是由cell
FormattedValueType
属性指定的类型

示例代码:

void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (e.ColumnIndex == 0 && e.Value != null)
        {
            int val;
            if (int.TryParse(e.Value.ToString(), out val) && val < 0)
            {
                e.Value = "N/A";
                e.FormattingApplied = true;
            }
        }
    }
void dataGridView1\u单元格格式(对象发送方,DataGridViewCellFormattingEventArgs e)
{
如果(e.ColumnIndex==0&&e.Value!=null)
{
int-val;
if(int.TryParse(e.Value.ToString(),out val)&&val<0)
{
e、 Value=“不适用”;
e、 FormattingApplied=true;
}
}
}

发布您编写的代码为什么不遍历所有单元格并将其值与0进行比较?如果它小于0,您只需将其更改为“N/A”。我不能这样做,因为它将在网格中有10列和500行以上。