0

currently I have a datatable that is ordered by an 'enablelocation' field as a user can add items and reposition them. this datatable is then used to create user controls that are added to a panel.

Currently we are clearing the controls and then rebuilding the panel each time there is a change (whih isn't often, but it still happens) so I am looking for a way of updating the controls on the panel when there is a change.

the current code is this:

    private void UpdateCats(int formid) {
      //update dataset
  cassess.Tables["CaseAssessDefCats"].Clear();
  DataTable tcats = Requests.SQLGen.ProcessSQLCommand(gobj, null, "select id,name,shownohistory, case when EnableLocation is null then 1 else EnableLocation end as EnableLocation,FormID from CaseAssessDefCats where EnableLocation >= 0 and FormID = " + formid + " order by EnableLocation, Name", false, true);
  if (tcats != null && tcats.Rows.Count > 0) {
    tcats.TableName = "CaseAssessDefCats";
    cassess.Merge(tcats);
  }
     //end update dataset
  try {
    if (_processing) { //check if update is running
      return;
    }
    ClearAssessment(); //removes all the controls
    _processing = true;
    panMain.Refresh();
    panMain.SuspendLayout();
      //loop through datatable, re-adding them as controls
    for (int a = cassess.CaseAssessDefCats.Rows.Count - 1; a >= 0; a--) {

      trow = cassess.CaseAssessDefCats[a];
      tcat = new CaseAssessDefCat(cassess, trow, gobj);
      tcat.Tag = trow.ID;
      tcat.catUp += new EventHandler(CatUp);
      tcat.catDown += new EventHandler(CatDown);
      tcat.catDelete += new EventHandler(CatDelete);
      tcat.catEnter += new EventHandler(CatEnter);
      tcat.catLeave += new EventHandler(CatLeave);
      tcat.catEdit += new EventHandler(CatEdit);
      tcat.catNew += new EventHandler(CatNew);
      panMain.Controls.Add(tcat);
      tcat.Dock = DockStyle.Top;
    }
    panMain.ResumeLayout();
    _processing = false;
    return;
  } catch {
    panMain.ResumeLayout();
    _processing = false;
    return;
  }
}
Johan
  • 262
  • 1
  • 15
  • What do you mean update? Can't you just access each control and do what you like with it? Perhaps you need to do better at identifying each one so that you can find it later? – DonBoitnott Feb 25 '15 at 12:52
  • finding the control isn't the problem (tcat.Tag), it's the _order_ of the controls that would change based on the change to the underlying datatable. Currently we are clearing all the controls and rebuilding them based on the new order, what I am asking is how can I prevent the clear/rebuild step and just move the controls around. – James Johnson Feb 25 '15 at 13:07
  • Perhaps you could use a `FlowLayoutPanel` and [this answer](http://stackoverflow.com/questions/425867/reordering-of-controls-within-a-flow-layout-panel) (ignore the accepted answer and look at the second one with the example). – DonBoitnott Feb 25 '15 at 13:20

0 Answers0