DataGridView单元格文本和单元格值

Cươ*_*gTV 4 c# text datagridview cell

我有一个只读C#Winform DataGridView的问题.

我有DataSource一个DataTable和它分配给DataGridView1.DataSource.我想按单元格值显示单元格文本而不更改DataSource.

例如:

cell value=1 => cell display text="one", 
cell value=2 => cell display text="two"
Run Code Online (Sandbox Code Playgroud)

我想要,如果我得到:

DataGridView1.Rows[rowIndex].Cells[columnIndex].Value
Run Code Online (Sandbox Code Playgroud)

那么它必须是1(或2,或3)不是"一"(或"两个"或"三个").

hor*_*ity 7

您可以使用CellFormatting事件处理程序.

private void DataGridView1_CellFormatting(object sender,
    DataGridViewCellFormattingEventArgs e)
{
    DataGridView dgv = (DataGridView)sender;
    if (dgv.Columns[e.ColumnIndex].Name == "TargetColumnName" &&
        e.RowIndex >= 0 &&
        dgv["TargetColumnName", e.RowIndex].Value is int)
    {
        switch ((int)dgv["TargetColumnName", e.RowIndex].Value)
        {
            case 1:
                e.Value = "one";
                e.FormattingApplied = true;
                break;
            case 2:
                e.Value = "two";
                e.FormattingApplied = true;
                break;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)