C# Window App : Setting Datasource Dynamically

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pod
    Contributor
    • Sep 2007
    • 298

    C# Window App : Setting Datasource Dynamically

    Hello


    I have a form which contains several textboxes and comboboxes

    I am able to loop through all the controls on this form and set the value (ctl.Text) to the Textboxes

    but if I try to set the datasource (ctl.DataSource ), I get an error during the build.

    (when I type a DOT after ctl, I can see the Text property but not the Datasource one)

    Here are my two functions, load_Labels works fine but I can't even build the app when I uncomment the datasource line in the load_DropDowns function

    I also tried casting but it doesn't like it either.

    If you can find the error in my code, I would really appreciate your feedback

    Thanks,

    Perry

    Code:
            void load_Labels()
            {
                try
                {
                    int rw = -1;
                    string sSQL = @"SELECT * FROM tbl_Label WHERE ynLabel_archive = false   AND intLabel_phase = 1 ORDER BY intLabel_id ASC; "
    
                    newDS = classObj.GetDataSet(sSQL);
    
                    foreach (Control ctl in this.tabPage1.Controls)
                    {
                        if (ctl.Name.IndexOf("_textbox") > 0)
                        {
                            //this sets the Question ID
                            rw = Convert.ToInt32(getQuePos(ctl.Name)) - 1;
                            ctl.Text = newDS.Tables[0].Rows[rw].ItemArray[2].ToString();
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("load_Labels:" + ex.Message);
                }
            }
    
            void load_DropDowns()
            {
                try
                {
                    int rw = -1;
                    string sSQL = @"SELECT intStatus_id FROM tbl_Status;";
    
                    newDS = classObj.GetDataSet(sSQL);
    
                    foreach (Control ctl in this.tabPage1.Controls)
                    {
                        if (ctl.Name.IndexOf("_combobox") > 0)
                        {
                            ctl.DataSource = newDS.Tables[0];
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("load_DropDowns():" + ex.Message);
                }
            }
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Those controls don't support the DataSource property.

    Comment

    • mikeyeli
      New Member
      • Oct 2007
      • 63

      #3
      i say you try a different way of doing the same thing..

      Plater is right.
      there is no DataSource property for "Control".
      so thats the problem.. you must cast it to ComboBox first.
      just do it like my example code below:

      try:
      [CODE=C#]
      foreach(object o in this.tabControl 1.TabPages[0].Controls)
      {
      if(o is System.Windows. Forms.ComboBox)
      {
      ((System.Window s.Forms.ComboBo x)o).DataSource = getSource().Tab les[0];
      }
      }[/CODE]

      Comment

      • pod
        Contributor
        • Sep 2007
        • 298

        #4
        that's exactly what I needed to know, I have not tried it yet but it showed me more on how to program with Objects

        Thanks

        Perry

        Originally posted by mikeyeli
        i say you try a different way of doing the same thing..

        Plater is right.
        there is no DataSource property for "Control".
        so thats the problem.. you must cast it to ComboBox first.
        just do it like my example code below:

        try:
        [CODE=C#]
        foreach(object o in this.tabControl 1.TabPages[0].Controls)
        {
        if(o is System.Windows. Forms.ComboBox)
        {
        ((System.Window s.Forms.ComboBo x)o).DataSource = getSource().Tab les[0];
        }
        }[/CODE]

        Comment

        • pod
          Contributor
          • Sep 2007
          • 298

          #5
          this works well, I can capture every object (or should I say instance) and set the datasource to it. But I need to capture the name of the instance as well and I can't if I use
          Code:
          foreach(object o in this.tabControl1.TabPages[0].Controls)
          could someone tell me how I can capture the name of the instance and still be able to set the datasource ?

          Thanks



          Originally posted by mikeyeli
          i say you try a different way of doing the same thing..

          Plater is right.
          there is no DataSource property for "Control".
          so thats the problem.. you must cast it to ComboBox first.
          just do it like my example code below:

          try:
          [CODE=C#]
          foreach(object o in this.tabControl 1.TabPages[0].Controls)
          {
          if(o is System.Windows. Forms.ComboBox)
          {
          ((System.Window s.Forms.ComboBo x)o).DataSource = getSource().Tab les[0];
          }
          }[/CODE]

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Use this isntead:
            Code:
            foreach(Control c in this.tabControl1.TabPages[0].Controls)
            Then you can get at like the .ID or .Name tag to see what they are called.

            Comment

            • mikeyeli
              New Member
              • Oct 2007
              • 63

              #7
              Originally posted by pod
              this works well, I can capture every object (or should I say instance) and set the datasource to it. But I need to capture the name of the instance as well and I can't if I use
              Code:
              foreach(object o in this.tabControl1.TabPages[0].Controls)
              could someone tell me how I can capture the name of the instance and still be able to set the datasource ?

              Thanks
              remember object has no Name property, but ComboBox does, so as simple as before.

              C#:
              Code:
              ((System.Windows.Forms.ComboBox)o).Name
              Code:
              foreach(object o in this.tabControl1.TabPages[0].Controls)
              {
              	if(o is System.Windows.Forms.ComboBox)
              	{
              		string myNameIs = ((System.Windows.Forms.ComboBox)o).Name
              		((System.Windows.Forms.ComboBox)o).DataSource = getSource().Tables[0];
              	}
              }
              if you need to use that combo for several things besides setting the datasource, it would be a real pain typing ((System.Window s.Forms.ComboBo x)o) every time, so what you can do is set a variable the onjects reference

              C#:
              Code:
              System.Windows.Forms.ComboBox currentCombo = ((System.Windows.Forms.ComboBox)o);
              cool huh!?

              so now the code would look like :

              Code:
              foreach(object o in this.tabControl1.TabPages[0].Controls)
              {
              	if(o is System.Windows.Forms.ComboBox)
              	{
              		System.Windows.Forms.ComboBox currentCombo = ((System.Windows.Forms.ComboBox)o);
              		string myNameIs = currentCombo.Name;
              		currentCombo.DataSource = getSource().Tables[0];
              	}
              }
              and you can use currentCombo, for anything you want from there on(well before the if "end bracket").

              Comment

              • pod
                Contributor
                • Sep 2007
                • 298

                #8
                Mikeyeli, Yo da Man!

                I tried casting it but was using the wrong property, control instead of combobox

                Thanks,

                Perry :-D



                [QUOTE=mikeyeli]remember object has no Name property, but ComboBox does, so as simple as before.

                C#:
                Code:
                ((System.Windows.Forms.ComboBox)o).Name
                .......

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  Originally posted by pod
                  Mikeyeli, Yo da Man!

                  I tried casting it but was using the wrong property, control instead of combobox
                  You were casting it as an Object, not as a Control, casting as a Control would have given you access to .Name and such.

                  Comment

                  • pod
                    Contributor
                    • Sep 2007
                    • 298

                    #10
                    I meant to say that from within the OBJECT foreach LOOP ...

                    foreach (object obj in this.tabCtl.Tab Pages[0].Controls)
                    ...I was able to get to the DATASOURCE property.

                    ((System.Window s.Forms.ComboBo x)obj).DataSour ce

                    But instead of keeping the same casting[I]...to get the NAME property, I used

                    ((System.Window s.Forms.Control )obj).Name ..... within the same foreach loop

                    I am slowly getting used to the Object way of thinking, I keep stumbling at every step, but with your help, I am getting it.

                    Thank you both for your help, it is greatly appreciated

                    Perry :-D


                    Originally posted by Plater
                    You were casting it as an Object, not as a Control, casting as a Control would have given you access to .Name and such.

                    Comment

                    Working...