2016-09-14 141 views
1

我有一个DataGridView其中我显示一些值,并有一列名称status。此列status具有值1或0,我想将其更改为Active为1时为Inactive时为0.如何更改datagridview的值?

我该怎么做?

尝试。

int i = gridUsuarios.Columns["status"].Index; 
      if (gridUsuarios.Rows[i].Cells[i].Value == "1") { 
       gridUsuarios.Rows[i].Cells[i].Value = "Active"; 
      }else { 
       gridUsuarios.Rows[i].Cells[i].Value = "Inactive"; 
      } 
+0

如何绑定DataGridView? – Prisoner

+0

@Alex是的,它是! – FernandoPaiva

+2

如果DGV是数据绑定的,您可能需要操作数据源。我会在那里创建一个计算列,并显示它而不是数字。这样你不会破坏数字数据。 – TaW

回答

2

这可以使用DataGridCellFormatting事件完成。样品片段是作为follws:

private void gridUsuarios_CellFormatting(object sender,System.Windows.Forms.DataGridViewCellFormattingEventArgs e) 
{ 
    if (this.gridUsuarios.Columns[e.ColumnIndex].Name == "status") 
    { 
     if (e.Value != null) 
     { 
      e.Value.ToString() == "1" ? e.Value = "Active" : e.Value = "Inactive"; 
      e.FormattingApplied = true; 
     } 
    } 
} 

DataGridView.CellFormatting event is here文档。