简体   繁体   中英

C# custom datagridview columns

How can I customize my DataGridView's columns (insert image, set text with differenet font...) based on the current values of the columns ?

For example:

if the column's value = 0, I want to display the word " NO " and if the column's value = 1, I want to display the word " YES " (or put a different image)

In JAVA I use CellRenderer , and if I change the column's value from "0" to "1" the renderer itself take the charge to change the word automaticly from " NO " to " YES ".

How can I do the same in C# ?

You should use CellFormatting event ( msdn ).

Example:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (this.dataGridView1.Columns[e.ColumnIndex].HeaderText == "Value")
    {
        if (e.Value != null)
        {
            int intValue = (int)e.Value;
            e.Value = (intValue == 0) ? "NO" : "YES";
        }
    }
}

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