CheckBox and DataGridViewCheckBoxColumn customization

0
0

Hi,

can you please advise how to adjust CheckBox and DataGridViewCheckBoxColumn to accept Y / N values instead of 0 / 1 ?

Any feedback appreciated,

D

  • Dino Novak
    I am trying to use this as databinding is important, but it does not seem to work public class CheckBoxYN : Wisej.Web.CheckBox { private Binding oBinding; public virtual void DataBind(System.Data.DataTable Data, string Column) { this.Checked = false; this.oBinding = new Binding(“Checked”, Data, Column); this.oBinding.Format += new ConvertEventHandler(this.FormatHandler); this.oBinding.Parse += new ConvertEventHandler(this.ParseHandler); this.DataBindings.Add(this.oBinding); } protected void FormatHandler(object sender, ConvertEventArgs e) { if (e.Value.ToString() == “Y”) e.Value = true; else e.Value = false; } protected void ParseHandler(object sender, ConvertEventArgs e) { if ((bool)e.Value == true) e.Value = “Y”; else e.Value = “N”; } }
  • You must to post comments
0
0

For the DataGridViewCheckBoxColumn use the TrueValue and FalseValue properties.

 

dataGridView.Columns.Add(new DataGridViewCheckBoxColumn() { 
    TrueValue = "Y",
    FalseValue = "N",
});

 

For the CheckBox use the Parse and Format events like this:

private void Binding_Format(object sender, ConvertEventArgs e)
{
  e.Value = Object.Equals(e.Value, "Y");
}

private void Binding_Parse(object sender, ConvertEventArgs e)
{
 e.Value = (Object.Equals(e.Value, true) ? "Y" : "N");
}

Note: Attach the event handler before adding the binding object.

 

  • You must to post comments
0
0

This works for checkbox:

public class CheckBoxYN : Wisej.Web.CheckBox

    {

        public string YesNo

        {

            get

            {

                if (this.Checked)

                    return “Y”;

                else

                    return “N”;

            }

            set

            {

                if (value == “Y”)

                    this.Checked = true;

                else

                    this.Checked = false;

            }

        }

    }

and then in page/control/window where you are using this control you need to set databinding to it using:

this.chkExtenede.DataBindings.Add(“YesNo”, <binding source name>, <field to bind to name>);

  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.