Click here to Skip to main content
11,647,828 members (61,524 online)
Rate this: bad
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;}
        /// Required designer variable.
        DataTable ^DTab2;

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

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
            String^ ConnString2 = "server=localhost;user=root;database=MyDB; "
            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 7:11am
Edited 20-Jan-13 16:46pm
Sergey Alexandrovich Kryukov at 19-Jan-13 19:21pm
System.Windows.Forms? Add the tag: "Forms" ("WinForms").
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.)
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.

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...

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

  Print Answers RSS
0 500
1 F-ES Sitecore 410
2 DamithSL 290
3 CPallini 290
4 OriginalGriff 277
0 OriginalGriff 1,342
1 994
2 DamithSL 971
3 Sergey Alexandrovich Kryukov 873
4 CPallini 795

Advertise | Privacy | Mobile
Web01 | 2.8.150804.4 | 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