473,406 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

C# Window App : Setting Datasource Dynamically

pod
298 100+
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

Expand|Select|Wrap|Line Numbers
  1.         void load_Labels()
  2.         {
  3.             try
  4.             {
  5.                 int rw = -1;
  6.                 string sSQL = @"SELECT * FROM tbl_Label WHERE ynLabel_archive = false   AND intLabel_phase = 1 ORDER BY intLabel_id ASC; "
  7.  
  8.                 newDS = classObj.GetDataSet(sSQL);
  9.  
  10.                 foreach (Control ctl in this.tabPage1.Controls)
  11.                 {
  12.                     if (ctl.Name.IndexOf("_textbox") > 0)
  13.                     {
  14.                         //this sets the Question ID
  15.                         rw = Convert.ToInt32(getQuePos(ctl.Name)) - 1;
  16.                         ctl.Text = newDS.Tables[0].Rows[rw].ItemArray[2].ToString();
  17.                     }
  18.                 }
  19.             }
  20.             catch (Exception ex)
  21.             {
  22.                 MessageBox.Show("load_Labels:" + ex.Message);
  23.             }
  24.         }
  25.  
  26.         void load_DropDowns()
  27.         {
  28.             try
  29.             {
  30.                 int rw = -1;
  31.                 string sSQL = @"SELECT intStatus_id FROM tbl_Status;";
  32.  
  33.                 newDS = classObj.GetDataSet(sSQL);
  34.  
  35.                 foreach (Control ctl in this.tabPage1.Controls)
  36.                 {
  37.                     if (ctl.Name.IndexOf("_combobox") > 0)
  38.                     {
  39.                         ctl.DataSource = newDS.Tables[0];
  40.                     }
  41.                 }
  42.             }
  43.             catch (Exception ex)
  44.             {
  45.                 MessageBox.Show("load_DropDowns():" + ex.Message);
  46.             }
  47.         }
  48.  
Oct 17 '07 #1
9 2191
Plater
7,872 Expert 4TB
Those controls don't support the DataSource property.
Oct 17 '07 #2
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:
Expand|Select|Wrap|Line Numbers
  1. foreach(object o in this.tabControl1.TabPages[0].Controls)
  2.             {
  3.                 if(o is System.Windows.Forms.ComboBox)
  4.                 {
  5.                     ((System.Windows.Forms.ComboBox)o).DataSource = getSource().Tables[0];
  6.                 }
  7.             }
Oct 17 '07 #3
pod
298 100+
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

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:
Expand|Select|Wrap|Line Numbers
  1. foreach(object o in this.tabControl1.TabPages[0].Controls)
  2.             {
  3.                 if(o is System.Windows.Forms.ComboBox)
  4.                 {
  5.                     ((System.Windows.Forms.ComboBox)o).DataSource = getSource().Tables[0];
  6.                 }
  7.             }
Oct 18 '07 #4
pod
298 100+
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
Expand|Select|Wrap|Line Numbers
  1. 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



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:
Expand|Select|Wrap|Line Numbers
  1. foreach(object o in this.tabControl1.TabPages[0].Controls)
  2.             {
  3.                 if(o is System.Windows.Forms.ComboBox)
  4.                 {
  5.                     ((System.Windows.Forms.ComboBox)o).DataSource = getSource().Tables[0];
  6.                 }
  7.             }
Oct 19 '07 #5
Plater
7,872 Expert 4TB
Use this isntead:
Expand|Select|Wrap|Line Numbers
  1. foreach(Control c in this.tabControl1.TabPages[0].Controls)
  2.  
Then you can get at like the .ID or .Name tag to see what they are called.
Oct 19 '07 #6
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
Expand|Select|Wrap|Line Numbers
  1. 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#:
Expand|Select|Wrap|Line Numbers
  1. ((System.Windows.Forms.ComboBox)o).Name
Expand|Select|Wrap|Line Numbers
  1. foreach(object o in this.tabControl1.TabPages[0].Controls)
  2. {
  3.     if(o is System.Windows.Forms.ComboBox)
  4.     {
  5.         string myNameIs = ((System.Windows.Forms.ComboBox)o).Name
  6.         ((System.Windows.Forms.ComboBox)o).DataSource = getSource().Tables[0];
  7.     }
  8. }
if you need to use that combo for several things besides setting the datasource, it would be a real pain typing ((System.Windows.Forms.ComboBox)o) every time, so what you can do is set a variable the onjects reference

C#:
Expand|Select|Wrap|Line Numbers
  1. System.Windows.Forms.ComboBox currentCombo = ((System.Windows.Forms.ComboBox)o);
cool huh!?

so now the code would look like :

Expand|Select|Wrap|Line Numbers
  1. foreach(object o in this.tabControl1.TabPages[0].Controls)
  2. {
  3.     if(o is System.Windows.Forms.ComboBox)
  4.     {
  5.         System.Windows.Forms.ComboBox currentCombo = ((System.Windows.Forms.ComboBox)o);
  6.         string myNameIs = currentCombo.Name;
  7.         currentCombo.DataSource = getSource().Tables[0];
  8.     }
  9. }
and you can use currentCombo, for anything you want from there on(well before the if "end bracket").
Oct 19 '07 #7
pod
298 100+
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#:
Expand|Select|Wrap|Line Numbers
  1. ((System.Windows.Forms.ComboBox)o).Name
.......
Oct 19 '07 #8
Plater
7,872 Expert 4TB
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.
Oct 19 '07 #9
pod
298 100+
I meant to say that from within the OBJECT foreach LOOP ...

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

((System.Windows.Forms.ComboBox)obj).DataSource

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

((System.Windows.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


You were casting it as an Object, not as a Control, casting as a Control would have given you access to .Name and such.
Oct 19 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Shravan | last post by:
Hi, I have a Windows Forms Custom DataGrid, which is put in a usercontrol, which on setting DataSource is setting focus to grid. The call stack for setting the focus is as follows. This is not...
2
by: VM | last post by:
I'm trying to dynamically display the rows in a datagrid while filling the datatable source. I'm doing it this way because we had already finished the app but this section sometimes took so long...
4
by: dyw55a | last post by:
Donna Mar 15, 10:11 am show options Newsgroups: microsoft.public.dotnet.framework.adonet From: "Donna" <dyw...@yahoo.com> - Find messages by this author Date: 15 Mar 2005 10:11:56 -0800...
4
by: Jim Katz | last post by:
I have an application that updates a strongly typed data set at run time. I'd like to dynamically create a table that connects to a run time data table. For displaying the data, this works well. ...
12
by: Joe | last post by:
I might be overworked so please excuse this stupid question... Say I do the following: DataTable table = new DataTable(); myDataAdaptor.Fill(table); dataGrid1.DataSource = table;
2
by: John Haycock | last post by:
Hi All I have a user control that uses a repeater to build a list of menu links: <itemtemplate> <li> <a href='news.aspx?newsID=<%# DataBinder.Eval(Container.DataItem,"newsID") %>'> <%#...
2
by: vbLeo | last post by:
Hello Guys, I'm trying to dynamically create forms and add controls to them. I'm specifically having a problem with establishing a data source for my textbox. I am able to add a remote data...
4
by: Brad Baker | last post by:
I'm trying to set a formview datasource parameter dynamically on page_load using the following code: public void Page_Load(object sender, EventArgs e) { SqlParameter param = new SqlParameter();...
3
by: nitinmukesh123 | last post by:
Hi I am facing a problem with setting the modal window title.. source of 1.html <html> <head> <script language="javascript"> function openNewWin() {...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.