Click here to Skip to main content
15,880,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can i add a drop down list to read from from a specific column in another form

What I have tried:

C#
private object btnTest_Click(object sender, EventArgs e)
        {
            //DataTable table = new DataTable();
            DataTable table = tableCollection[cboSheet.SelectedItem.ToString()];
            foreach (DataRow row in table.Rows)
            {
                foreach (DataColumn col in table.Columns)
                {
                    string value = row[col.ColumnName].ToString();

                    if (value.Equals("24"))
                    {
                        row["Column2"] = "25";
                    }
                    else if (value.Equals("October 2022"))
                    {
                        row["Column6"] = "November";
                    }

                }
            }
        }
Posted
Updated 9-Jan-23 18:49pm
v2

Exactly how depends on the "relationship" between the two forms.
Have a look at these, one of them will fit your circumstances.
The form that creates an instance of another:
C#
MyForm mf = new MyForm();
mf.Show();
Is the "parent", the other form is the "child".
(This doesn't imply any formal MDI relationship)

Transferring information between two forms, Part 1: Parent to Child[^]
Transferring information between two forms, Part 2: Child to Parent[^]
Transferring information between two forms, Part 3: Child to Child[^]
 
Share this answer
 
Comments
BillWoodruff 10-Jan-23 0:56am    
I have discussed my syntax issues with using 'Parent ... again, here :)

imho, a serious design error by MS, and, it generates confusion in students ... compared to words like "access" that do not have the "baggage" of ZOrder, TopMost, etc.

Also, exposing the public internals of a creating class to classes it creates is, imho, a serious violation of SOLID.
OriginalGriff 10-Jan-23 2:55am    
The "parent ... child" is a terminology problem, I agree - but what other terms are there that you can use with beginners -preferably without having you cram a wall of text in front of them so they know what you are talking about? :laugh:

I blame MDI myself ... :D

And public fields or controls? Bluuurgh! Properties and events, people -properties and events!
BillWoodruff 10-Jan-23 4:30am    
agreed: MDI is the evil demon that spawned this semantic plague ! :)

in the past, I was always surprised at the difficlty my students had getting "down" the difference between a class definition and an instance of the class. some had previous experience with VB or Java which seemed to contribute to their difficulty, and, if yoy do use inheritance from a "base form" in WinForms, you've got to do tricks to access the base form's controls that seem strange .. as strange as mucking with the base form desinger file to make stuff 'public.

did WPF fix all this ?

cheers, bill

p.s. for me "access to the instance" is a mantra.
A DataTable contains strongly typed Columns [^], you must declare the Column types when you create it: the default is type is 'string.

Your code shows no information about how you build the collection of DataTables 'tableCollection ... assuming each DataTable is on another Form: the Form with the DropDown needs to have a list of all DataTables bound to the DropDown..

That a main Form creates instances of other Forms does not mean those instances expose their Controls. I don't like using the word 'Parent for such created instances: the created instances do not "inherit" anything.

Making one Form the 'Parent of another ... by setting the Parent Property of the instance ... is a code practice that is usually a mistake, and will not help you with the issue you describe. A created Form that is 'TopLevel' cannot have a Form 'Parent.

'Parent is often mistakenly thought to refer to olf MDI app architecture.

See the articles OriginalGriff links to in his solution for examples of how to expose contents of one form to another.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900