Click here to Skip to main content
15,868,217 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
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
C#
            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
C#
#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.
Posted
Updated 20-Jan-13 16:46pm
v4
Comments
Sergey Alexandrovich Kryukov 19-Jan-13 19:21pm    
System.Windows.Forms? Add the tag: "Forms" ("WinForms").
—SA
sokran960 20-Jan-13 3:46am    
i'm sorry. Where should I put tags: "Forms"("WinForms").
Sergey Alexandrovich Kryukov 20-Jan-13 13:10pm    
Right now, they are shown on top as "See more". You click "Improve question" to go to your initial input form; on the top, there is the input field "Tags". (I added some.)
—SA
Sergey Alexandrovich Kryukov 20-Jan-13 13:15pm    
And I deleted your "Solution 1". You should not add anything as a "solution" in this case. "Add your solution here" is reserved for the cases when you try to help some other member in response the a question. In this case, use "Improve question" and add you sample code or whatever else there.

In other cases, add comment to any other posts, reply to existing comments.

"Add your solution here" is often abused, so members get abuse reports for that; many heavy abusers already lost their memberships (probably they were trying to cheat to boost their reputation points, who knows why...), you don't want such things.

—SA
sokran960 20-Jan-13 21:46pm    
@Sergey. Thx alot for your advice and addition Tags. And sorry for my not being know bout that. :)

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