2016-09-14 211 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文檔。