2017-06-19 14 views
0

私はDataSourceから充填されるDataGridViewのありますDatagridview列のテキストを置き換える方法は?

BindingSource bs = new BindingSource(); 

bs.DataSource = reader.local(); 

dataGridView1.DataSource = bs; 

クラスモデルからreader.local();データを返します:

class ClientJson 
    { 

     public int id { get; set; } 
     public string name { get; set; } 
     public string secondname { get; set; } 
     public int sex { get; set; } 
} 

(男性、女性としてのカスタムテキストにフィールドsexの値を置換する方法を)?

答えて

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

あなたはプロパティのタイプを「文字列」に変更することを検討しましたか?あなただけ取得し、thistあなたを置くロジックにしてプロパティを追加することができ、この

public string Sex 
{ 
    get 
    { 
     return this.sex; 
    } 
    set 
    { 
     if(value == "1") 
      this.sex = "male"; 
     else 
      this.sex = "female"; 
    } 
} 
0

よう

何か。例:

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

あなたは新しいプロパティGenderのみをバインドします。

0

このような意味ですか?

public string Sex 
{ 
    get 
    { 
     return this.sex; 
    } 
    set 
    { 
this.sex=value=="1"?"male":"female"; 
    } 
} 
+0

はい、私は必要があるとそれを思え – user3573738

関連する問題