Databinding Question

Databinding Question

Old forum URL: forums.lhotka.net/forums/t/4796.aspx


Husker Dan posted on Monday, May 05, 2008

I have an object model that look like this

ParentList

   Parent

      ChildList

         Child

On my Windows Form I have an Infragistics Ultra Tree on the left side of my form that I bind to my object. That displays just fine. The user can select a child and when they do, there are edit controls on the right side of my form that the I want to bind the selected child to do. However, I can't seem to get this to work.

I have a BindingSource I use for the tree and it's data source is set to the parent list. I have a BindingSource I want to use for the edit controls but I can't figure out what to set it to so I can see the child. All I can get is the Child List.

Any help would be appreciated.

Thanks!

Patrick.Roeper replied on Monday, May 05, 2008

Dan,

My first suggestion would be to drop the tree control and use an ultraGrid without the column headers; the 2 controls will look identical. I ran into a lot of issues in the past with databinding and drag/drop stuff with the tree control, and tech support always recommended to use a Grid instead.

After you switch to the grid control, you should be able to see your childList as a 2nd band of the grid. If this is not the case let me know.

Patrick

Husker Dan replied on Monday, May 05, 2008

Patrick,

Thanks for the response.

However, I don't think that my original post was clear. I am not having an issue with the tree. It display just fine. The issue I am having (and maybe it is a tree isue) it linking the selected child to the edit controls on the right side of my form via Binding Sources.

The user selects a child but the edit controls are not binding to that child correctly. All I seem to get is some child list level junk.

Is that the same problem that led you to use the tree?

 

Thanks again!

Dan

pillesoft replied on Monday, May 05, 2008

Dear Dan,

i use similar GUI, but i use normal treeview.
so, if your problem doesn't relate to tree my solution may help you.
    private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
    {
      if (_opermode != OperationMode.VIEW)
      {
        if (MessageBox.Show(this, "Cancel Data Input?", this.Text,
            MessageBoxButtons.YesNo,
            MessageBoxIcon.Warning,
            MessageBoxDefaultButton.Button2) == DialogResult.Yes)
        {
          if (e.Node.Tag is WorkpackRO)
            CancelWorkpack();
          else if (e.Node.Tag is ProjectRO)
            CancelProject();
        }
      }
      else
      {
        if (e.Node.Tag is WorkpackRO)
        {
          panelProject.Visible = false;

          int id = ((WorkpackRO)e.Node.Tag).Id;

          ReBindWorkpack(id);
          splitContainer2.Visible = true;
        }
        else if (e.Node.Tag is ProjectRO)
        {
          splitContainer2.Visible = false;
          panelProject.Visible = true;

          int id = ((ProjectRO)e.Node.Tag).Id;

          ReBindProject(id);
        }
      }
    }

the ReBindxxxx() methods could be interesting:
    private void ReBindWorkpack(int id)
    {
      this.workpackBindingSource.RaiseListChangedEvents = false;
      this.workpackBindingSource.DataSource = null;
      _workpack = Workpack.GetWorkpack(id);
      this.workpackBindingSource.DataSource = _workpack;
      this.workpackBindingSource.RaiseListChangedEvents = true;
      this.workpackBindingSource.ResetBindings(false);

      this.childrenWPsBindingSource.RaiseListChangedEvents = false;
      this.childrenWPsBindingSource.DataSource = null;
      WorkpackChildren chilwps = _workpack.ChildrenWPs;
      this.childrenWPsBindingSource.DataSource = chilwps;
      this.childrenWPsBindingSource.RaiseListChangedEvents = true;
      this.childrenWPsBindingSource.ResetBindings(false);

    }
    private void ReBindProject(int id)
    {
      this.projectBindingSource.RaiseListChangedEvents = false;
      this.projectBindingSource.DataSource = null;
      _project = Project.GetProject(id);
      this.projectBindingSource.DataSource = _project;
      this.projectBindingSource.RaiseListChangedEvents = true;
      this.projectBindingSource.ResetBindings(false);
    }

as you see i make a rebinding always
this works for me.

Husker Dan replied on Monday, May 05, 2008

Thanks for the response.

I see what your code is doing. However, I have one question. I have read in more than one post that we need to keep the binding source controls linked together. So one binding source would become the datasource for another binding source. Your code does not seem to do this.

So, what is the rule when it comes to multiple binding sources?

 

pillesoft replied on Tuesday, May 06, 2008

Dear Dan,

i don't know what you mean, i'm relatively new in c# coming from VFP world.
i don't know how correct my solution, unfortunately i have sometimes databinding problem (you can find some posts from me regarding databinding)

this solution works so far for me, however if somebody has a more accurate solution i'm ready to change also.

Ivan

Copyright (c) Marimer LLC