Click here to Skip to main content
12,956,512 members (80,959 online)

Combobox anotherform: Cannot bind to the new display member.

sokran960 asked:

Open original thread
Hi. I'm quite new for c++/cli. I tried to make a ComboBox1 on Form2 which ComboBox1 Bind to MySql Database Table.

Form2 Code
            DTab2 = gcnew DataTable();
            this->comboBox1->DataSource = DTab2->DefaultView;
            this->comboBox1->DisplayMember = "Name";
            this->comboBox1->ValueMember = "ID";
....
    public:property DataTable ^CB_Prop
           {
               void set(DataTable ^value){
                    DTab2 = value;}
               DataTable ^get(){
                    return DTab2;}
           }
 
   private:
        /// Required designer variable.
        DataTable ^DTab2;
...



Form1 Code
#include "Form2.h"
...
                _Form2 = gcnew Form2();
	        _Form2->Hide();
 
		String ^ConnString = "server=localhost;user=root;database=MyDB; "
                       "port=#port;password=passwd;";
		MySqlConnection ^Conn = gcnew MySqlConnection(ConnString);
		MySqlDataAdapter ^MyAdapt = gcnew MySqlDataAdapter();
		MyAdapt->SelectCommand = gcnew MySqlCommand("SELECT ID, Name FROM "
                                         "MyTable", Conn);
            DataTable ^DTab1 = gcnew DataTable();
            MyAdapt->Fill(DTab1);
		_Form2->CB_Prop = DTab1;
	}
...
	private:
		/// Required designer variable.
		Form2 ^_Form2;
...
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
        _Form2->Show();}


The code was Build succeeded but I got error
"An unhandled exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll"
"Additional information: Cannot bind to the new display member"

I tried to replace

this->comboBox1->ValueMember = "ID";


Form1 and Form2 succeeded initialize but there no items in Combobox1.

I make modification on Form 2 and decide not using DataTable instead DataSet. So I replace DataTable at Form1 and Form2 With DataSet with addition MySqlDataAdapter at Form2
Form2(void)
        {
            InitializeComponent();
            String^ ConnString2 = "server=localhost;user=root;database=MyDB; "
                     "port=#port;password=passwd;";
            MySqlConnection ^Conn2 = gcnew MySqlConnection(ConnString2);
            MySqlDataAdapter^ MyAdt2 = gcnew MySqlDataAdapter("SELECT ID, Name "
                            "FROM MyTable", Conn2);
            DSet2 = gcnew System::Data::DataSet();
            MyAdt2->Fill(DSet2, "MyTable");
            
            this->comboBox1->DataSource = DSet->Tables["MyTable"];
            this->comboBox1->DisplayMember = "Name";
            this->comboBox1->ValueMember = "ID";
        }
...
    public:property DataSet ^CB_Prop
           {
               void set(DataSet ^value) {
                   DSet2 = value;}
 
               DataSet ^get() {
                   return DTab2;}
           }

It work and the ComboBox1 filled correctly. But I don't know it's the good way or not.
Tags: Windows, Forms

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).



Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 26 Mar 2009
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100