FernandoPaiva
FernandoPaiva

Reputation: 4462

How to change value of datagridview?

I have a DataGridView where I show some values and has a column with name status. This column status has values 1 or 0 and I want to change this to Active when is 1 and Inactive when is 0.

How could I do this ?

Trying.

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";
            }

Upvotes: 1

Views: 186

Answers (1)

Parthipan Natkunam
Parthipan Natkunam

Reputation: 784

This can be done using DataGridCellFormatting event. A sample snippet is as 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;
        }
    }
}

Documentation on DataGridView.CellFormatting event is here.

Upvotes: 3

Related Questions