开发者

Populate a combo box with one data tabel and save the choice in another

开发者 https://www.devze.com 2022-12-29 04:10 出处:网络
I have two data tables in a data set for example lets say Servers: Server | Ip | Users: Username | Password | Server |

I have two data tables in a data set for example lets say

Servers:
| Server | Ip |

Users:
| Username | Password | Server |

and there is a foreign key on users that points to servers.

How do I make a combo box that list all of the servers. Then how do I make the selected server in that combo box be saved in the data set in the Server column of the users table.

EDIT -- I 开发者_如何学Chavent tested it yet but is this the right way?

this.bsServers.DataMember = "Servers";
this.bsServers.DataSource = this.dataSet;
this.bsUsers.DataMember = "FK_Users_Servers";
this.bsUsers.DataSource = this.bsServers;
this.serverComboBox.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.bsUsers, "Server", true));
this.serverComboBox.DataSource = this.bsServers;
this.serverComboBox.DisplayMember = "Server";
this.serverComboBox.ValueMember = "Server";


I don't think so. You need to populate your bsUsers, and specify how do you want to select Users from this list (another combobox?).

bsServers.DataMember = "Servers";
bsServers.DataSource = new[]
    {
       new Servers {Server = "1", Ip = "1.1.1.1"},
       new Servers {Server = "2", Ip = "2.2.2.2"},
       new Servers {Server = "3", Ip = "3.3.3.3"},
       new Servers {Server = "4", Ip = "4.4.4.4"}
    };
bsUsers.DataSource = new[]
     {
         new Users {Server = "1", Username = "aaa", Password="kjkhkhk"},
         new Users {Server = "1", Username = "bbb", Password="3534564"},
         new Users {Server = "3", Username = "ccc", Password="egrhtyj"},
         new Users {Server = "2", Username = "ddd", Password="6576878"}
     };
serverComboBox.DataBindings.Add("SelectedValue", bsUsers, "Server", true);
serverComboBox.DataSource = bsServers;
serverComboBox.DisplayMember = "Server";
0

精彩评论

暂无评论...
验证码 换一张
取 消