更改DataGridView的单元格值

时间:2013-07-02 22:28:33

标签: linq-to-sql datagridview

假设我有以下假设表:

enter image description here

如何让DataGridView显示以下内容?

enter image description here

请注意Sick的值已更改。

我试过以下无济于事:

var query = from c in Patients
            select new
            {
                c.Name,
                c.Sick == 1 ? "Yes" : "No"
            };

1 个答案:

答案 0 :(得分:2)

您可以使用DataGridView.CellFormatting事件。

private void dataGridView1_CellFormatting(object sender, System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
{
    if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Sick")
    {
        if (e.Value != null)
        {
            if (e.Value.ToString() == "1"
            {
                e.Value = "Yes";
            }
            else
            {
                e.Value = "No";
            }
            e.FormattingApplied = true;
        }
    }
}