简体   繁体   中英

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?

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

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

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

For vb code:

Use the code below to change boolean True/False display to Checkbox in Datagrid:

datagrid1.Columns.Add(New DataGridViewCheckBoxColumn)

Use the code below to display column header:

datagrid1.Columns(column number).HeaderText = "User Status"

If you swap the column out for a DataGridViewComboBoxColumn that is bound to a list/datatable having bool and string columns then it will decode the bool to whatever string you want (and the user can edit the values, but cannot put bad values in)

        //your datatable has some boolean column and your DataGridView is bound to it
        DataTable dt = new DataTable();
        dt.Columns.Add("MyBoolColumn", typeof(bool));     //this is your bool column
        dataGridView1.DataSource = dt;


        //make a datatable that has 2 columns, string and bool, and 2 rows (one for true, one for false)
        DataTable dv = new DataTable();
        dv.Columns.Add("Dis");                  //it will be shown in the combo
        dv.Columns.Add("Val", typeof(bool));    //it will be used by the combo to set MyBoolColumn 
        dv.Rows.Add("Yeah baby", true);
        dv.Rows.Add("Nooo way", false);


        //make a combo box column bound to the values table above 
        //and connected to the table you show in the grid

        var dgvcbc = new DataGridViewComboBoxColumn();
        dgvcbc.DataPropertyName = "MyBoolColumn";          //connect to the grid table
        dgvcbc.DisplayMember = "Disp";                     //show this column
        dgvcbc.ValueMember = "Val";                        //use values from this
        dgvcbc.DataSource = dv;
        dataGridView1.Columns.Add(dgvcbc);

This grid will now show a combo where it once showed a checkbox, and editing the combo will change the boolean value

You don't have to bind the combo column to a datatable. Maybe you'd prefer a list of Tuple or ValueTuple as the backing data store for the Yes/No combo:

        var dv2 = new List<Tuple<string, bool>>() {
            new Tuple<string, bool>("Yeah baby", true),
            new Tuple<string, bool>("Noooo way", false)
        };
        var dgvcbc2 = new DataGridViewComboBoxColumn();
        dgvcbc2.DataPropertyName = "MyBoolColumn";
        dgvcbc2.DisplayMember = "Item1";                //the string in the Tuple
        dgvcbc2.ValueMember = "Item2";                  //the bool in the Tuple
        dgvcbc2.DataSource = dv2;

        dataGridView1.Columns.Add(dgvcbc2);

If your datagridview is designed in the forms designer, the easiest thing would probably be to add a DispVal table to a strongly typed dataset, then it becomes available as a "project list instance" in the picker that lets you choose the datasource for the combo column

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM