7

表格中有datagridview显示数据库表的内容,表类型的一列是布尔值,所以在datagridview中显示真/假,但我想自定义它以显示是/否。你建议哪种方式?

4

5 回答 5

19

当谈到自定义格式时,我想到了两种可能的解决方案。

1.处理CellFormatting事件并自行格式化。

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.使用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
                }
        }
    }
 }

然后像这样使用它,并处理CellFormatting事件以使其工作

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

编辑 您可以订阅这样的CellFormatting事件

dataGridView1.CellFormatting += dataGridView1_CellFormatting;

希望这可以帮助

于 2013-07-31T21:49:00.767 回答
4
    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;
    }
}
于 2016-08-23T08:12:23.663 回答
1

如果你只想展示这个怎么样。这很容易思考。

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;
    }
}
于 2016-02-10T11:11:21.067 回答
0

对于VB代码:

使用下面的代码将布尔 True/False 显示更改为 Datagrid 中的复选框:

datagrid1.Columns.Add(New DataGridViewCheckBoxColumn)

使用下面的代码显示列标题:

datagrid1.Columns(column number).HeaderText = "User Status"
于 2020-01-15T13:07:12.470 回答
0

如果您将列换成绑定到具有布尔和字符串列的列表/数据表的 DataGridViewComboBoxColumn 列,那么它将将该布尔解码为您想要的任何字符串(并且用户可以编辑值,但不能放入错误的值)

        //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);

这个网格现在将显示一个组合,它曾经显示一个复选框,编辑组合将更改布尔值

您不必将组合列绑定到数据表。也许您更喜欢 Tuple 或 ValueTuple 列表作为 Yes/No 组合的后备数据存储:

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

如果您的 datagridview 是在表单设计器中设计的,最简单的方法可能是将 DispVal 表添加到强类型数据集,然后它会在选择器中作为“项目列表实例”使用,让您选择组合的数据源柱子

在此处输入图像描述

于 2020-08-19T06:32:42.410 回答