Vydělávej až 160.000 Kč měsíčně! Akreditované rekvalifikační kurzy s garancí práce od 0 Kč. Více informací.
Hledáme nové posily do ITnetwork týmu. Podívej se na volné pozice a přidej se do nejagilnější firmy na trhu - Více informací.

Diskuze: WinForms: Databind Relation na dvoch comboboxc

Aktivity
Avatar
makis824
Člen
Avatar
makis824:27.11.2014 15:51

Ahoj,

uz dlhsie riesim problem, ako prepojit 2/alebo viac komboboxov na zaklade DataBinding. Ma s tym pls niekto skusenost ? Viem vytvorit riesenie na zaklade eventov (co je troche krkolomne. Nizsie uvadzam kod, ktory mi stale nefunguje:

DataSet ds = new DataSet();
DataTable colors = new DataTable() { TableName = "colors" };
DataTable types = new DataTable() { TableName = "types" };

colors.Columns.Add("id");
colors.Columns.Add("color");
types.Columns.Add("id");
types.Columns.Add("id_color");
types.Columns.Add("type");

ds.Tables.Add(colors);
ds.Tables.Add(types);
ds.AcceptChanges();

colors.Rows.Add("1", "Red");
colors.Rows.Add("2", "Green");
types.Rows.Add("1", "1", "A");
types.Rows.Add("2", "1", "B");
types.Rows.Add("3", "2", "C");
types.Rows.Add("4", "2", "D");

ds.Relations.Add(new DataRelation("rel",
    ds.Tables["colors"].Columns["id"],
    ds.Tables["types"].Columns["id_color"]));

comboBox1.DataSource = ds.Tables["colors"];
comboBox1.DisplayMember = "color";
comboBox1.ValueMember = "id";

comboBox2.DataSource = ds;
comboBox2.DisplayMember = "colors.rel.type";
comboBox2.ValueMember = "colors.rel.id";

Vychadzal som z http://joshblair.blogspot.sk/…in-cnet.html

Za pomoc vopred dakujem..

R.

 
Odpovědět
27.11.2014 15:51
Avatar
makis824
Člen
Avatar
Odpovídá na makis824
makis824:27.11.2014 16:09

..uvadzam funkcny kod:

private BindingSource bindingSource1 = new BindingSource();
private BindingSource bindingSource2 = new BindingSource();
InitializeComponent();
            DataSet ds = new DataSet();
            DataTable colors = new DataTable() { TableName = "colors" };
            DataTable types = new DataTable() { TableName = "types" };

            colors.Columns.Add("id", typeof(Int32));
            colors.Columns.Add("color");
            types.Columns.Add("id", typeof(Int32));
            types.Columns.Add("id_color", typeof(Int32));
            types.Columns.Add("type");

            ds.Tables.Add(colors);
            ds.Tables.Add(types);
            ds.AcceptChanges();

            colors.Rows.Add("1", "Red");
            colors.Rows.Add("2", "Green");
            types.Rows.Add("1", "1", "A");
            types.Rows.Add("2", "1", "B");
            types.Rows.Add("3", "2", "C");
            types.Rows.Add("4", "2", "D");

            ds.Relations.Add(new DataRelation("rel",
                ds.Tables["colors"].Columns["id"],
                ds.Tables["types"].Columns["id_color"]));

            this.bindingSource1.DataSource = ds;
            this.bindingSource1.DataMember = "colors";
            this.bindingSource2.DataSource = this.bindingSource1;
            this.bindingSource2.DataMember = "rel";

            comboBox1.DataSource = this.bindingSource1;
            comboBox1.DisplayMember = "color";
            comboBox1.ValueMember = "id";

            comboBox2.DataSource = this.bindingSource2;
            comboBox2.DisplayMember = "type";
            comboBox2.ValueMember = "id";

..vid. : http://www.vbforums.com/showthread.php?…)

R.

 
Nahoru Odpovědět
27.11.2014 16:09
Děláme co je v našich silách, aby byly zdejší diskuze co nejkvalitnější. Proto do nich také mohou přispívat pouze registrovaní členové. Pro zapojení do diskuze se přihlas. Pokud ještě nemáš účet, zaregistruj se, je to zdarma.

Zobrazeno 2 zpráv z 2.