2013-07-31 11 views
5

C'è una vista datagrid in un modulo che mostra il contenuto della tabella del database, una colonna di tipo di tabella è booleana, quindi in datagridview mostra true/false, ma voglio personalizzarlo in mostra Sì/No. quale strada suggerisci?mostra Sì/NO invece True/False in datagridview

+2

vedere http://stackoverflow.com/questions/9914411/replace-true-false-in-datagridview-columns?rq=1 – Shoe

risposta

14

Quando si tratta di formattazione personalizzata, mi vengono in mente due possibili soluzioni.

1.Handle CellFormatting evento e formattare il proprio.

void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
{ 
    if (e.ColumnIndex == yourcolumnIndex) 
    { 
     if (e.Value is bool) 
     { 
      bool value = (bool)e.Value; 
      e.Value = (value) ? "Yes" : "No"; 
      e.FormattingApplied = true; 
     } 
    } 
} 

2.Utilizzare Custom Formatter

public class BoolFormatter : ICustomFormatter, IFormatProvider 
{ 
    public object GetFormat(Type formatType) 
    { 
     if (formatType == typeof(ICustomFormatter)) 
     { 
      return this; 
     } 
     return null; 
    } 

    public string Format(string format, object arg, IFormatProvider formatProvider) 
    { 
     if (arg == null) 
     { 
      return string.Empty; 
     } 

     bool value = (bool)arg; 
     switch (format ?? string.Empty) 
     { 
      case "YesNo": 
       { 
        return (value) ? "Yes" : "No"; 
       } 
      case "OnOff": 
       { 
        return (value) ? "On" : "Off"; 
       } 
      default: 
       { 
        return value.ToString();//true/false 
       } 
     } 
    } 
} 

Quindi utilizzare in questo modo, e gestire CellFormatting evento per farlo funzionare

dataGridView1.Columns[1].DefaultCellStyle.FormatProvider = new BoolFormatter(); 
dataGridView1.Columns[1].DefaultCellStyle.Format = "YesNo"; 

void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
{ 
    if (e.CellStyle.FormatProvider is ICustomFormatter) 
    { 
     e.Value = (e.CellStyle.FormatProvider.GetFormat(typeof(ICustomFormatter)) as ICustomFormatter).Format(e.CellStyle.Format, e.Value, e.CellStyle.FormatProvider); 
     e.FormattingApplied = true; 
    } 
} 

Modifica È possibile iscriversi a CellFormatting evento come questo

dataGridView1.CellFormatting += dataGridView1_CellFormatting; 

Spero che questo aiuti

+0

Questo evento 'CellFormatting' quando occures? Voglio mostrare Sì/No sempre! –

+1

@MiladSobhkhiz non è sicuro di cosa intendi, devi sottoscrivere l'evento 'CellFormatting' che si verificherà ogni volta prima di mostrare il valore sulla griglia. controlla la mia modifica. –

1

ne dite di questo, se si vogliono solo mostrare. È facile da pensare.

private void Form1_Load(object sender, EventArgs e) 
{ 
    List<Person> list = new List<Person>(); 
    list.Add(new Person(20, true)); 
    list.Add(new Person(25, false)); 
    list.Add(new Person(30, true)); 

    dgv.DataSource = list; 

    //Hide checkbox column 
    dgv.Columns["IsProgrammer"].Visible = false; 

    //Add represent text column 
    DataGridViewTextBoxColumn textColumn = new DataGridViewTextBoxColumn(); 
    textColumn.Name = "Yes/No"; 
    dgv.Columns.Add(textColumn); 

    //true/false -> yes/no 
    foreach (var row in dgv.Rows.Cast<DataGridViewRow>()) 
     row.Cells["Yes/No"].Value = (bool)row.Cells["IsProgrammer"].Value ? "Yes" : "No"; 
} 
private class Person 
{ 
    public int Age { get; set; } 
    public bool IsProgrammer { get; set; } 

    public Person(int i, bool b) 
    { 
     Age = i; 
     IsProgrammer = b; 
    } 
} 
2
void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
{ 
    var grid = (DataGridView)sender; 
    if (grid.Columns[e.ColumnIndex].Name == "IsActive") 
    { 
     e.Value = (bool)e.Value ? "True_Text_Replace" : "False_Text_Replace"; 
     e.FormattingApplied = true; 
    } 
}