show Yes/NO instead True/False in datagridview

There is datagridview in a form that shows content of table of database, one column of table type is boolean, so in datagridview shows true/false, but i want to customize it to show Yes/No. which way you suggest?


Solution 1:

When it comes to custom formatting, two possible solutions comes in my mind.

1.Handle CellFormatting event and format your own.

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.Use 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
                }
        }
    }
 }

Then use it like this, and handle CellFormatting event to make it work

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

Edit You can subscribe to CellFormatting event like this

dataGridView1.CellFormatting += dataGridView1_CellFormatting;

Hope this helps

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

Solution 3:

How about this if you only want to show. It's easy to think.

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