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 -
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);
-
}
-
}
-
9 2200 Plater 7,872
Recognized Expert Expert
Those controls don't support the DataSource property.
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: -
foreach(object o in this.tabControl1.TabPages[0].Controls)
-
{
-
if(o is System.Windows.Forms.ComboBox)
-
{
-
((System.Windows.Forms.ComboBox)o).DataSource = getSource().Tables[0];
-
}
-
}
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: -
foreach(object o in this.tabControl1.TabPages[0].Controls)
-
{
-
if(o is System.Windows.Forms.ComboBox)
-
{
-
((System.Windows.Forms.ComboBox)o).DataSource = getSource().Tables[0];
-
}
-
}
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 - 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: -
foreach(object o in this.tabControl1.TabPages[0].Controls)
-
{
-
if(o is System.Windows.Forms.ComboBox)
-
{
-
((System.Windows.Forms.ComboBox)o).DataSource = getSource().Tables[0];
-
}
-
}
Plater 7,872
Recognized Expert Expert
Use this isntead: -
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.
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 - 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#: - ((System.Windows.Forms.ComboBox)o).Name
- 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#: - System.Windows.Forms.ComboBox currentCombo = ((System.Windows.Forms.ComboBox)o);
cool huh!?
so now the code would look like : - 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").
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#: - ((System.Windows.Forms.ComboBox)o).Name
.......
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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!
|
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;
|
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
|
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...
|
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;
| |
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>
|
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...
|
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);
}
|
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");
}
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
| |