如何替换Datagridview列中的文本?

时间:2017-06-19 06:59:40

标签: c# winforms c#-4.0 datagridview

我有从<{1}}填充的 DataGridView

DataSource

BindingSource bs = new BindingSource(); bs.DataSource = reader.local(); dataGridView1.DataSource = bs; 返回类Model中的数据:

reader.local();

如何将字段class ClientJson { public int id { get; set; } public string name { get; set; } public string secondname { get; set; } public int sex { get; set; } } 中的值替换为自定义文本(男性,女性)?

4 个答案:

答案 0 :(得分:1)

如果您不想或不能更改模型,可以使用CellFormatting事件替换单元格值:

private void dataGridView1_CellFormatting(object sender, System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
{
    if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("sex"))
    {
        int? sex = e.Value as int?;

        // replace statement checks with your own conditions
        if(sex == 0)
        {
            e.Value  = "Male";
        }
        else if(sex == 1)
        {
            e.Value = "Female";
        }
        else
        {
            e.Value = "Unknown";
        }
    }
}

答案 1 :(得分:0)

您是否考虑过将属性类型更改为&#34; string&#34;?

类似这样的事情

ng-pattern="{{regex}}"

答案 2 :(得分:0)

你可以添加一个只有get的属性并将其添加到你的逻辑中。一个例子:

public string Gender{ get{ return this.sex==1?"Male":"female";}}

并且只绑定新属性Gender。

答案 3 :(得分:0)

你的意思是这样吗?

public string Sex
 {
    get
    {
        return this.sex;
    }
    set
    {
this.sex=value=="1"?"male":"female";
    }
}
相关问题