C # : Linq 쿼리에서 ComboBox Valuember 설정 방법
-
14-11-2019 - |
문제
OK 그래서 콤보 폭스가있는 콤보 블록스 데이터 소스는 LINQ 쿼리의 결과입니다
//load QA names
var qaNames =
from a in db.LUT_Employees
where a.position == "Supervisor" && a.department == "Quality Assurance"
select new { a, Names = a.lastName + ", " + a.firstName };
cboQASupervisor.DataSource = qaNames;
cboQASupervisor.DisplayMember = "Names";
.
문제 IM은 다음 코드의 다음 줄을 추가하려고 할 때
cboQASupervisor.ValueMember = "ID";
.
익명 유형을 캐스팅 할 수 없었던 런타임에 오류가 발생합니다. 어떻게 수정합니까?
수정 :
오류는 다음과 같습니다.
새 값 구성원에 바인딩 할 수 없습니다. 매개 변수 이름 : 값
해결책
You specify ID as the value field, but you don't have ID property in your anonymous type.
Assuming you have ID in your LUT_Employees object:
var qaNames = (
from a in db.LUT_Employees
where a.position == "Supervisor" && a.department == "Quality Assurance"
select new { a.ID, Names = a.lastName + ", " + a.firstName })
.ToList();
cboQASupervisor.DataSource = qaNames;
cboQASupervisor.DisplayMember = "Names";
cboQASupervisor.ValueMember = "ID";
다른 팁
You can try this:
var qaNames =
from a in db.LUT_Employees
where a.position == "Supervisor" && a.department == "Quality Assurance"
select new { Id = a.ID, Names = a.lastName + ", " + a.firstName };
cboQASupervisor.DataSource = qaNames.ToList();
cboQASupervisor.DisplayMember = "Names";
cboQASupervisor.ValueMember = "Id";
Add .ToList()
to your code in the datasource line.
제휴하지 않습니다 StackOverflow