BindingSource for Winfroms on framework v4.5.2

Costas

Administrator
Staff member
C#:
//nitially we declare the bingingsource to form :
private BindingSource bindSource;

public frmConstructor()
{
    InitializeComponent();

    //listbox bindingsource DATASOURCE a List<T>
    General.db = new List<PRJsettings>();

    //instantiate the bingingsource
    bindSource = new BindingSource();
    //set the bingingsource DATASOURCE (List<T>)
    bindSource.DataSource = General.db;
    //set the listbox DATASOURCE (bingingsource )
    lst.DataSource = bindSource;
    lst.DisplayMember = "URL";


    //editable controls binding
    //https://stackoverflow.com/q/11485377
    //bool - Binding.FormattingEnabled Property - https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.binding.formattingenabled?view=netframework-4.5.2
    txtURL.DataBindings.Add(new Binding("Text", this.bindSource, "URL", false));
    txtAuthor.DataBindings.Add(new Binding("Text", this.bindSource, "author", false));
    chkMono.DataBindings.Add(new Binding("Checked", this.bindSource, "isMono", false));
    txtParentElement.DataBindings.Add(new Binding("Text", this.bindSource, "parentElementSelector", false));
    txtTitleElemet.DataBindings.Add(new Binding("Text", this.bindSource, "titleElementSelector", false));
    txtLinkElement.DataBindings.Add(new Binding("Text", this.bindSource, "linkElementSelector", false));
    txtMonoSelector.DataBindings.Add(new Binding("Text", this.bindSource, "monoElementSelector", false));

    //achieve #one-way data binding# by setting DataSourceUpdateMode
    //setting Never ensures that changes made in the control won't be propagated back to the data source.
    txtURL.DataBindings.Add(new Binding("Text", this.bindSource, "URL", false, DataSourceUpdateMode.Never));

public class PRJsettings
{
    public string URL { get; set; }
    public string author { get; set; }
    public bool isMono { get; set; }

    public string monoElementSelector { get; set; }

    public string parentElementSelector { get; set; }
    public string titleElementSelector { get; set; }
    public string linkElementSelector { get; set; }
}
}

once the General.db bound to BingingSource, we never null it, use only
  • General.db.Clear();
  • General.db.AddRange(x);
  • General.db.Remove(x);
after the use on any method of those, the
bindSource.ResetBindings(false);
has to follow, so reflect the changes to controls.

get the selected with
bindSource.Current

/cast it to entity
PRJsettings = (PRJsettings) bindSource.Current;

To stop & resume the same BingingSource
// To stop data binding temporarily
bindingSource.SuspendBinding();

// the actions here

// To resume data binding
bindingSource.ResumeBinding();

To stop a BindingSource :
C#:
//Call the SuspendBinding() method on the BindingSource that you want to stop.
//This will suspend the BindingSource so that it no longer updates the control.
bindSource.SuspendBinding();

//start new
bindSource = new BindingSource();
bindSource.DataSource = myNewList;

//Set the DataSource property of the control to the new BindingSource:
lst.DataSource = bindSource;


WARNING : the bound controls must be always visible otherwise the binding breaks!! wtf!?


Look Under the Hood of Windows Forms Data Binding (mirror)



JSONSerialization

C#:
//Deserialize
General.db.AddRange(JsonSerializer.Deserialize<List<PRJsettings>>(File.ReadAllText(filename, Encoding.UTF8)));

//Serialize
File.WriteAllText(cdlg.FileName, General.db.Serialize(), Encoding.UTF8);


public static class JsonSerializer
{
    public static string Serialize<T>(this T data)
    {
        try
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
            var stream = new MemoryStream();
            serializer.WriteObject(stream, data);
            string jsonData = Encoding.UTF8.GetString(stream.ToArray(), 0, (int)stream.Length);
            stream.Close();
            return jsonData;
        }
        catch(Exception x)
        {
            //General.Mes(x.InnerException.ToString(), System.Windows.Forms.MessageBoxIcon.Stop);
            return "";
        }
    }

    public static T Deserialize<T>(this string jsonData)
    {
        try
        {
            DataContractJsonSerializer slzr = new DataContractJsonSerializer(typeof(T));
            var stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonData));
            T data = (T)slzr.ReadObject(stream);
            stream.Close();
            return data;
        }
        catch (Exception x)
        {
            //General.Mes(x.InnerException.ToString(), System.Windows.Forms.MessageBoxIcon.Stop);
            return default(T);
        }
    }
}
 
Top