Babak Fakhriloo
Babak Fakhriloo

Reputation: 2126

replace true/false in datagridview columns

I have a datagridview which I fill it as below :

var q= repository.GetStudents();//

dataGridView1.DataSource = null;
dataGridView1.Columns.Clear();

dataGridView1.DataSource = q;

dataGridView1.Columns.RemoveAt(1);
//Remove IsActive 
//Cause I want to have my own implementation 

dataGridView1.Columns[0].DataPropertyName = "StudentID";
dataGridView1.Columns[0].HeaderText = "Studunet ID";

dataGridView1.Columns[1].DataPropertyName = "IsActive";
dataGridView1.Columns[1].HeaderText = "Status";

The "IsActive" property is of boolean Type. When the "IsActive" cell is being displayed, it show true/false. I want to replace it with my own custom value.

I read this and this posts but I could not resolve my problem.

Upvotes: 8

Views: 10062

Answers (2)

Caius Jard
Caius Jard

Reputation: 74605

One of the funky things about a DataGridViewComboBoxColumn is that you can give it one data source that has a column of values to lookup and a column of values to show, and you can bind it to another column of values and then it will perform the lookup for you

So, suppose your collection q of Students (or whatever they are) has an IsActive true/false and you want this to show as "All the time", or "Not a chance".. Let's hash together a combobox that does this:

var cb = new DataGridViewComboBoxColumn();
cb.DisplayMember = "DisplayMe";   //the related text to show in the combo
cb.ValueMember = "ValueToLookup"; //the name in the combo's lookup list
cb.DataPropertyName = "IsActive"; //the name of your property on Student, to look up

cb.DataSource = "All the time,Not a Chance"
  .Split(',')
  .Select(s => new { DisplayMe = s, ValueToLookup = (s[0] == 'A') } )
  .ToList();

It doesn't really matter how we generat the combo's datasource; here I've made a string into a List<anonymous_string+bool> by splitting, then selecting a new anonymous type with the two property names I need; you can use anything that has some named properties - a List of KeyValuePair, Tuple, whatever..

The critical thing is that the combo can read the q.IsActive bool you cited in DataPropertyName, look that bool up in its list in the property named in the ValueMember, then display the property named in the DisplayMember. It works for editing too, so the user can choose a new item from the combo and the translation works back the other way - "what does the user choose? what is the value of its property named in ValueMember, put that value into the student IsActive property named in DataPropertyName".. And it doesnt stop at bools either; the value member can be anything - an int, date etc

Upvotes: 0

digEmAll
digEmAll

Reputation: 57210

You can use the CellFormatting event of the DataGridView, e.g.:

void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    var grid = (DataGridView)sender;
    if (grid.Columns[e.ColumnIndex].Name == "IsActive")
    {
        e.Value = (bool)e.Value ? "MY_TEXT_FOR_TRUE" : "MY_TEXT_FOR_FALSE";
        e.FormattingApplied = true;
    }
}

EDIT (as per comment):

It's very similar to what you're doing now, just remove the bound column and add a new column of the desired type and set the DataPropertyName properly e.g. :

this.dataGridView1.Columns.Remove("COL_TO_CUSTOMIZE");
var btnCol = new DataGridViewDisableButtonColumn();
btnCol.Name = "COL_TO_CUSTOMIZE";
btnCol.DataPropertyName = "COL_TO_CUSTOMIZE";
var col = this.dataGridView1.Columns.Add(btnCol);

Note that this append the column at the end, but you can decide the position of the column by using dataGridView.Columns.Insert method instead of Add.

Upvotes: 12

Related Questions