c# - show Yes/NO instead True/False in datagridview -


there datagridview in form shows content of table of database, 1 column of table type boolean, in datagridview shows true/false, want customize show yes/no. way suggest?

when comes custom formatting, 2 possible solutions comes in mind.

1.handle cellformatting event , format own.

void datagridview1_cellformatting(object sender, datagridviewcellformattingeventargs e) {      if (e.columnindex == yourcolumnindex)      {          if (e.value 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 this, , handle cellformatting event make 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 icustomformatter)      {          e.value = (e.cellstyle.formatprovider.getformat(typeof(icustomformatter)) icustomformatter).format(e.cellstyle.format, e.value, e.cellstyle.formatprovider);          e.formattingapplied = true;      }  } 

edit can subscribe cellformatting event this

datagridview1.cellformatting += datagridview1_cellformatting; 

hope helps


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -