locked
Dropdown is not getting populated dynamically RRS feed

  • Question

  • User1216627406 posted

    Greetings,

    For most of my coding in asp.net, I have not really dabbled into windows application till now, although I didn't think there is much difference between web forms and windows forms.

    I am doing something really basic and I have difficulty with it.

    I am trying to dynamically populate a dropdown list box but so far, the dropdown is not getting populated with data.

    I even tried populated the dropdown box  statically and that too is not getting populated either.

    Can someone please guide me in what I am doing wrong?

    Here is what I have so far:

        public partial class Main : Form
        {
            public Main()
            {
                InitializeComponent();
            }
            private void Main_Load(object sender, System.EventArgs e)
            {
                GetTrans_Types();
            }
            public void GetTrans_Types()
            {
                DataTable dt = new DataTable();
                SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Members"].ConnectionString);
                 con.Open();
                    string SQLStr = "Select sID, sName from States";
                SqlCommand cmd = new SqlCommand(SQLStr, con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                DataRow dataRow = dt.NewRow();
                //dt["sID"] = "--Choose--";
                dt.Rows.InsertAt(dataRow, 0);
                TransTypes.DataSource = dt;
                TransTypes.DisplayMember = "sID";
                TransTypes.ValueMember = "sName";
    
                con.Close();
            }
        }
        
        //App.Config file:
        
        <?xml version="1.0" encoding="utf-8" ?>
        <configuration>
            <startup> 
                <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
            </startup>
          <connectionStrings>
            <add name="Members" connectionString="Data Source=tcp:xxx.xx.xxx.xxx,1433;Initial Catalog=myDB;Integrated Security=False;User ID=myusername;Password=mypassword" providerName="System.Data.SqlClient" />
          </connectionStrings>
    </configuration>

    Thanks in advance

    Tuesday, July 24, 2018 3:18 PM

All replies

  • User-1171043462 posted

    In Windows Form, we have Form_Load event as we have Page_Load in ASP.Net.

    To generate Form_Load event.

    Go to the Form design and double click on Form it will generated Form_Load event.

    Then call your DropDownList code inside the event.

    Still any problems refer my article: Bind (Populate) ComboBox from Database using DataTable (DataSet) in Windows Forms using C# and VB.Net

    Tuesday, July 24, 2018 3:34 PM
  • User1216627406 posted

    Thank you sir for your prompt response.

    Isn't form_load() event same as the one I used:

    private void Main_Load(object sender, System.EventArgs e)

    Does it have to specifically say form_load()?

    Tuesday, July 24, 2018 3:38 PM
  • User-1171043462 posted
    There is nothing like main load
    Tuesday, July 24, 2018 3:51 PM
  • User1216627406 posted

    That is strange.

    I have seen people use Form1_Load()

    What's the difference?

    This is for my learning experience.

    Thanks for your help.


    Even when I change it to Form_Load() or Form1_Load(), it still doesn't display the records in the dropdown box.

    Tuesday, July 24, 2018 3:55 PM
  • User-1171043462 posted

    If you simply write it won't work as it is not attached to the Form.

    You need to

    1. Double Click the Form in designer View so that it generates event

    OR

    2. Select Form press F4 to open Properties. There you will see Events Tab. In that write the name of Event for Load and press enter

    Tuesday, July 24, 2018 7:31 PM
  • User36583972 posted


    Hi simflex,

    I have seen people use Form1_Load()

    What's the difference?

    This is for my learning experience.

    Thanks for your help.


    Even when I change it to Form_Load() or Form1_Load(), it still doesn't display the records in the dropdown box.

    You have asked some question about the Windows Form Application. Generally, there is a big difference between Windows Form Application and Web Application.

    Combox is used on WinForm, DropDownList is used on Web pages, and the binding modes are slightly different.

    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
                DataTable table = new DataTable();
                table.Columns.Add("sID", typeof(int));
                table.Columns.Add("Name", typeof(string));
    
                table.Rows.Add(57, "000310097");
                table.Rows.Add(130, "000164345");
                table.Rows.Add(92, "000269518");
    
                //Insert the Default Item to DataTable.
                DataRow row = table.NewRow();
                row[0] = 0;
                row[1] = "Please select";
                table.Rows.InsertAt(row, 0);
              
                //Assign DataTable as DataSource.
                comboBox1.DataSource = table;
                comboBox1.ValueMember = "sID";
                comboBox1.DisplayMember = "Name";
                comboBox1.SelectedIndex = 0;
    
            }
        }
    

    Besides, You should visit the Windows Forms Forum for getting suitable help.


    Best Regards,

    Yong Lu

    Wednesday, July 25, 2018 8:23 AM