473,602 Members | 2,811 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# Window App : Setting Datasource Dynamically

pod
298 Contributor
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 2200
Plater
7,872 Recognized Expert Expert
Those controls don't support the DataSource property.
Oct 17 '07 #2
mikeyeli
63 New Member
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 Contributor
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 Contributor
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 Recognized Expert Expert
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
mikeyeli
63 New Member
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.Window s.Forms.ComboBo x)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 Contributor
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 Recognized Expert Expert
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 Contributor
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


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
1761
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 happened always whenever DataSource is set, only called sometimes, in a series of setting DataSource, I could get this setting focus only one time . Can anybody help me how to stop this one. system.windows.forms.dll!
2
2035
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 that we had to add multithreading. I do it with this code (this code is not in the class that generates the form and the controls): public DataTable OpenAuditAZMFileToView(string sFileName, ref DataGrid dataGrid_auditAddress) { DataRow rowAudit;
4
6188
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 Local: Tues, Mar 15 2005 10:11 am Subject: VB.NET dynamically create datagrid and set its datasource Reply | Reply to Author | Forward | Print | Individual Message | Show original | Report Abuse
4
13481
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. I just set the GridView.DataSource once, and call DataBind(); I'd like to drive the application from the GridView control, by including command buttons that allow editing of the data. Starting out simple, I have a DataTable with a boolean...
12
16746
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
5564
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") %>'> <%# DataBinder.Eval(Container.DataItem, "newsHeadline") %> </a> </li>
2
3255
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 control called dSlider that appears to function properly. The textbox txtID also properly positions itself as directed. But I can't dynamically get it to bind to my coded data controller as a dataSource. VB6 tells me that the "object variable or with...
4
6991
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(); param.ParameterName = "@department_id"; param.Value = "e62bbc7d623f44a68e101cba90e839s3"; formview_datasource.SelectParameters.Add(param); }
3
4325
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() { window.showModalDialog("2.html", self, "status: Yes;dialogWidth:500px;dialogHeight:400px"); }
0
7993
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8404
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8054
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8268
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5867
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5440
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3944
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2418
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.