Reputation: 1159
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?
Upvotes: 9
Views: 22729
Reputation: 15782
The answer where we handle CellFormatting
event didn't work for me, though it seemed so simple and promising. Perhaps because I have bound a List as the DataSource to my DataGridView. I was getting the exception
value of the cell has a wrong type
I solved it in the DataSource List's class, so instead of having a bool property, I hid it and put the logic inside a string property, like this
Old
public bool PropertyName
{
get => someBusinessLogic();
}
New
[Browsable(false)] // will not be displayed in the DataGridView
public bool PropertyNameBool
{
get => someBusinessLogic();
}
public string PropertyName
{
get => this.PropertyNameBool ? "True" : "False";
}
This worked for me as this class is only used as a DataSource for my grid. But changing around the class may not work for everybody.
Upvotes: 0
Reputation: 74730
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
Upvotes: 1
Reputation: 1
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"
Upvotes: 0
Reputation: 910
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;
}
}
Upvotes: 4
Reputation: 11
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;
}
}
Upvotes: 1
Reputation: 73502
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
Upvotes: 20