更改DataGridView的单元格值 [英] Changing Cell Values of DataGridView

查看:220
本文介绍了更改DataGridView的单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下假设表:





如何使 DataGridView 显示以下内容?





请注意, Sick 的值已更改。



我已尝试以下操作无效: / p>

  var query = from c in Patients 
select new
{
c.Name,
c.Sick == 1? 是:否
};


解决方案

您可以使用 DataGridView.CellFormatting 事件。

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


Suppose I have the following hypothetical table:

How can I make a DataGridView display the following?

Notice the values of Sick have changed.

I have tried the following to no avail:

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

解决方案

You can use the DataGridView.CellFormatting event.

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

这篇关于更改DataGridView的单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆