简体   繁体   中英

two different comboxBoxes show the same value as one of them changes

I am programming with c# in visual studio,I have two combobox named 1) senderReferringComboBox and 2) recieverReferringComboBox I am populating these two comboBox like this

   DataSet myDataSet = new DataSet();
   string sql = " Select dbo.Position.ID, dbo.Position.usr_Id as UserId, dbo.Position.Title + ' (' + dbo._User.LastName + ' ' + dbo._User.FirstName + ')' AS UserPosition" +
                " FROM  dbo._User INNER JOIN " +
                " dbo.Position ON dbo._User.ID = dbo.Position.usr_Id " +
                " WHERE (dbo._User.IsActive = 1) and dbo._User.Id !=" + UIHelper.CurrentUser.Id +
                " ORDER BY dbo.Position.Title";

   DataTable table = client.GetDataTable(null, sql, null);
   myDataSet.Tables.Add(table);

   DataView dvw1 = myDataSet.Tables[0].DefaultView;
   DataView dvw2 = myDataSet.Tables[0].DefaultView;

   senderReferringComboBox.DataSource = dvw1;
   senderReferringComboBox.DisplayMember = "UserPosition";
   senderReferringComboBox.ValueMember = "UserId";
   senderReferringComboBox.SelectedIndex = -1;

   recieverReferringComboBox.DataSource = dvw2;
   recieverReferringComboBox.DisplayMember = "UserPosition";
   recieverReferringComboBox.ValueMember = "UserId";
   recieverReferringComboBox.SelectedIndex = -1;  

when I change a value in senderReferringCombox the same value will be set on recieverReferringComboBox . I have set two different views ? why does it happen? how can I prevent it?

You need to create new views for each ComboBox DataSource because if you use DefaultView you get the same view both times.

Change your code like this to make it work:

DataView dvw1 = new DataView(myDataSet.Tables[0]);
DataView dvw2 = new DataView(myDataSet.Tables[0]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM