Child 组合框 Return 如果 Child table 中不存在数据值组合框,则为 Null (LINQ)
Child Cobobox Return Null if DataValue Combobox NotExist in Child table ( LINQ )
信息
我有3个table
tblCountry
tblStateName
tblState
tblState
Child of tblStateName
(StateName_ID
) (为了更好的处理)
tblState
tblCountry
Child (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;
}
加载表单后我看到了这个结果
如果我 Select Country01
,我在 cmbState
中看到 4 个结果(很好,工作正常)
我的问题
但是如果我 Select 'Country02'(这个 Value
不是 child 在 tblState 中)但是我看到第一个 Value
从 tblState 像这样
我需要什么?
如何 select Country02 并在 cmbState 中查看 Null?
或更好( Return Null If Country_ID not Exist in State table )并且 cmbState 为 Null
( SRY 我的英语不好)
如果result
为null或空,请在重新加载数据后清除cmbState
,如以下代码:
if(result == null || result.Count == 0)
{
cmbState.DataSource = null;
cmbState.DataBindings.Clear();
}else
{
cmbState.DataSource = result;
}
希望对您有所帮助。
信息
我有3个table
tblCountry
tblStateName
tblState
tblState
Child of tblStateName
(StateName_ID
) (为了更好的处理)
tblState
tblCountry
Child (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;
}
加载表单后我看到了这个结果
如果我 Select Country01
,我在 cmbState
中看到 4 个结果(很好,工作正常)
我的问题
但是如果我 Select 'Country02'(这个 Value
不是 child 在 tblState 中)但是我看到第一个 Value
从 tblState 像这样
我需要什么?
如何 select Country02 并在 cmbState 中查看 Null? 或更好( Return Null If Country_ID not Exist in State table )并且 cmbState 为 Null
( SRY 我的英语不好)
如果result
为null或空,请在重新加载数据后清除cmbState
,如以下代码:
if(result == null || result.Count == 0)
{
cmbState.DataSource = null;
cmbState.DataBindings.Clear();
}else
{
cmbState.DataSource = result;
}
希望对您有所帮助。