【发布时间】:2020-05-25 12:57:23
【问题描述】:
信息
我有 3 张桌子
tblCountry
tblStateName
tblState
tblStatetblStateName 的子级 (StateName_ID)(为了更好的处理)
tblStatetblCountry (Country_ID) 的子级(级联都知道)
我用 2 个组合框创建 1 个表单
我使用此代码加载 2 组合框 DataValue
private void Form01_Load(object sender, EventArgs e)
{
//Load CountryComboBox Source from Table01
using (UnitOfWork db = new UnitOfWork())
{
// At first assign properties DisplayMember and ValueMember.
cmbCountry.DisplayMember = "Country";
cmbCountry.ValueMember = "Country_ID";
// And then assign DataSource property of the CountryComboBox .
cmbCountry.DataSource = db.CountryRepository.Get();
}
}
//Load StateComboBox From Table02
private void cmbCountry_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedValue = cmbCountry.SelectedValue.ToString();
using (UnitOfWork db = new UnitOfWork())
if (!string.IsNullOrEmpty(selectedValue))
{
// At first assign properties DisplayMember and ValueMember.
cmbState.DisplayMember = "StateName";
cmbState.ValueMember = "State_ID";
// And then assign DataSource property of the cmbState.
var result = (from state in db.StateRepository.GetNameIDByFilter(selectedValue)
join stateName in db.StateNameRepository.Get() on state.StateName_ID equals stateName.Statename_ID
select new
{
State_ID = state.State_ID,
StateName = stateName.StateName
}).ToList();
cmbState.DataSource = result;
}
加载表单后我看到了这个结果
如果我选择 Country01,我会在 cmbState 中看到 4 个结果(它运行良好且工作正常)
我的问题
但如果我选择“Country02”(这个 Value 不是 tblState 中的孩子)但我看到 First Value From tblState 这样
我需要什么?
如何在 cmbState 中选择 Country02 并查看 Null ? 或更好(如果 State 表中不存在 Country_ID 则返回 Null)并且 cmbState 为 Null
(SRY 为我的英语不好)
【问题讨论】:
标签: c# winforms linq join combobox