Click here to Skip to main content
11,485,965 members (78,153 online)
Rate this: bad
good
Please Sign up or sign in to vote.
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.
Posted 19-Jan-13 8:11am
Edited 20-Jan-13 17:46pm
v4
Comments
Sergey Alexandrovich Kryukov at 19-Jan-13 19:21pm
   
System.Windows.Forms? Add the tag: "Forms" ("WinForms").
—SA
sokran960 at 20-Jan-13 3:46am
   
i'm sorry. Where should I put tags: "Forms"("WinForms").
Sergey Alexandrovich Kryukov at 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 at 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 at 20-Jan-13 21:46pm
   
@Sergey. Thx alot for your advice and addition Tags. And sorry for my not being know bout that. :)
Sergey Alexandrovich Kryukov at 20-Jan-13 23:07pm
   
You are welcome, no problem, no need to apologize...
—SA

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

  Print Answers RSS
0 C#developer.asp 429
1 Sergey Alexandrovich Kryukov 375
2 Maciej Los 365
3 Mehdi Gholam 250
4 OriginalGriff 144
0 Sergey Alexandrovich Kryukov 8,819
1 OriginalGriff 8,047
2 Sascha Lefèvre 3,199
3 Maciej Los 2,886
4 Richard Deeming 2,370


Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 20 Jan 2013
Copyright © CodeProject, 1999-2015
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