I have a WinForm. At this form I show data from table row. I have tables Comments and Users:
Comments :{ ID(int),Text(NVarchar), UserId(int) }
Users: { ID(int), Name(NVarChar) }
At a form I have TextBox and Combobox. In Textbox I won't show "Text" from Comments (user can edit this field, data must be saved in a table) and Name from table Users (where Comments.UserId = Users.ID).
I have following code:
FormDataClassesDataContext dc = new FormDataClassesDataContext();
_comment = (from comment in dc.Comments
where comment.ID == 1
select comment).FirstOrDefault();
_user = (from users in dc.Users
where users.ID == _comment.UserId
select users).FirstOrDefault();
textBoxComment.DataBindings.Add("Text", _comment, "Text"); // <-OK
comboBoxAssessor.DataSource = dc.Users;
comboBoxAssessor.DisplayMember = "Name";
comboBoxAssessor.ValueMember = "ID";
comboBoxAssessor.DataBindings.Add("SelectedItem", _comment, "UserId");
and then
dc.SubmitChanges();
but I become "Unable to pass "MyApp.Forms.User" to "Int32"
The selected item of a combobox is the current object in its data source, in this case: a User
. You must use SelectedValue
:
comboBoxAssessor.DataBindings.Add("SelectedValue", _comment, "UserId");
which returns the value of the property you define in the ValueMember
.
SelectedItem should be of type User if I don't make a mistake. And you bind Comment.UserId (int) to it.
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.