Click here to Skip to main content
15,886,755 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am using singleton pattern (partially) I want to fill combo-box on one form from another form. I am using following code

C#
for (int i = 0; i < dt.Rows.Count; i++)
           {
               if ("cmb.Name" == "cmbServer1")
               {
                   GridWindow.Instance.cmbDatabase1.Items.Add(dt.Rows[i]["database_name"]);
               }

               else
               {
                   GridWindow.Instance.cmbDatabase2.Items.Add(dt.Rows[i]["database_name"]);
               }


But I am not able to fill the combobox,Kindly let me know if you have solution for this

Thanks in advance
Ramesh
Posted
Updated 28-Mar-15 5:22am
v2

1 solution

You don't tell what exactly is going wrong there but I have some suggestions anyway:

Here you compare two constant strings, it will never be true:
C#
if ("cmb.Name" == "cmbServer1")

Probably you meant to do this:
C#
if (cmb.Name == "cmbServer1")


I would advise against hard-coded references to a Form from another Form. It will be no issue in a very small project but if you add more Forms to your project or stick to that "strategy" when doing bigger projects, it will eventually result in a maintenance nightmare. There was recently a new article on this topic published here, take a look: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows[^]
 
Share this answer
 

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