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