在一个窗体中有一个datagridview,它显示数据库表的内容,其中一个列是布尔类型,所以在datagridview中显示为true/false,但我想自定义它显示为Yes/No。你有什么建议吗?
在一个窗体中有一个datagridview,它显示数据库表的内容,其中一个列是布尔类型,所以在datagridview中显示为true/false,但我想自定义它显示为Yes/No。你有什么建议吗?
说到自定义格式化,我脑海中出现了两个解决方案。
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. 使用 自定义格式化程序
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;
希望这能有所帮助
CellFormatting
事件是什么时候发生的?我想始终显示是/否! - Milad SobhkhizCellFormatting
事件,它会在每次将值显示到网格之前发生。请查看我的编辑。 - Sriram Sakthivel 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;
}
}
如果您只想显示,那么这样怎样。
很容易思考。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;
}
}
对我来说,处理CellFormatting
事件的答案并没有起作用,尽管它看起来非常简单和有前途。也许是因为我将List绑定为DataGridView的DataSource。我一直在收到异常:
单元格的值具有错误类型
我在DataSource List的类中解决了这个问题,所以我隐藏了一个bool属性,并将逻辑放在一个字符串属性中,像这样:
旧的:
public bool PropertyName
{
get => someBusinessLogic();
}
新的
[Browsable(false)] // will not be displayed in the DataGridView
public bool PropertyNameBool
{
get => someBusinessLogic();
}
public string PropertyName
{
get => this.PropertyNameBool ? "True" : "False";
}
这对我有用,因为这个类只用作我的网格的数据源。但是改变类可能不适用于每个人。
//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);
此网格现在将显示一个组合框,而不是以前的复选框,编辑组合框将更改布尔值
您无需将组合框列绑定到数据表。也许您更喜欢使用元组列表或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);
针对VB代码:
使用下面的代码将布尔值True/False显示更改为Datagrid中的复选框:
datagrid1.Columns.Add(New DataGridViewCheckBoxColumn)
datagrid1.Columns(column number).HeaderText = "User Status"