Question

I have to bind datarows to my controls. So far so good. The problem now is, that my datarow contains only strings in the column I have to bind, but of course the property "checked" of a Checkbox takes only boolean arguments.

Is there a way to use DataBinding here? Maybe with some kind of converter in between?

Thanks

Was it helpful?

Solution

Use the ConvertEventHandler Delegate to change types for DataBinding.

Example

    Binding binding = new Binding("checked", dt, "string_field");
    binding.Format += new ConvertEventHandler(binding_Format);
    binding.Parse += new ConvertEventHandler(binding_Parse);
    this.checkbox1.DataBindings.Add(binding); 

    void binding_Format(object sender, ConvertEventArgs e)
    {
        if (e.Value.ToString() == "yep") e.Value = true;
        else e.Value = false;
    }

    void binding_Parse(object sender, ConvertEventArgs e)
    {
        if ((bool)e.Value) e.Value = "yep";
        else e.Value = "nope";
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top