组合框 SelectedIndexChanged
ComboBox SelectedIndexChanged
我有2个table
table01
NameID,名称(PK = NameID)
1,约翰
2、玫瑰
10,Mr.X
11,Mrs.X
table02
CarID NameID CarName(PK = CarID,FK = NameID)关系
1,1,奔驰
2,1,宝马
3,yYy
4,10,xXx
5,11,zZz
在我的表单中,我有 2 个组合框
combobox1 数据 = table01 数据
private void Form1_Load(object sender, EventArgs e)
{
using (UnitOfWork db = new UnitOfWork())
{
//لود کردن کمبوباکس لایه
comboBox1.DataSource = db.table01Repository.Get();
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "NameID";
}
}
combobox2 数据 = table02 数据 if NameID = NameID
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedValue.ToString() != null)
{
using (UnitOfWork db = new UnitOfWork())
comboBox2.DataSource = db.table02Repository.GetNameIDByFilter(comboBox1.SelectedValue.ToString());
comboBox2.DisplayMember = "CarName";
comboBox2.ValueMember = "CarID";
}
}
现在我的问题
如果我将 combox1 更改为 1 ( john )
我的数据在combobox2中=
奔驰
宝马
xXx
zZz
这是因为在 SelectedIndexChanged 中(在 table02 中全部计数为 1)
我必须如何将我的代码更改为 1 = 1(仅 1)而不是 1 = 1+ 0 或 1 = 1 + 1 + 0
我的资料库
public IEnumerable<table02> GetNameIDByFilter(string parameter)
{
return db.table02.Where(g => g.NameID.ToString().Contains(parameter)).ToList();
}
我发现我的错误
IRepository
IEnumerable<table02> GetNameIDByFilter(string parameter);
存储库
public IEnumerable<table02> GetNameIDByFilter(string parameter)
{
if (!int.TryParse(parameter, out int value))
{
return new List<table02>();
}
return db.table02.Where(g => g.NameID == value).ToList();
}
表单中的代码
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedValue = comboBox1.SelectedValue.ToString();
using (UnitOfWork db = new UnitOfWork())
if (!string.IsNullOrEmpty(selectedValue))
{
comboBox2.DataSource = db.table02Repository.GetNameIDByFilter(selectedValue);
comboBox2.DisplayMember = "CarName";
comboBox2.ValueMember = "CarID";
}
我有2个table
table01
NameID,名称(PK = NameID)
1,约翰 2、玫瑰
10,Mr.X
11,Mrs.X
table02
CarID NameID CarName(PK = CarID,FK = NameID)关系
1,1,奔驰
2,1,宝马
3,yYy
4,10,xXx
5,11,zZz
在我的表单中,我有 2 个组合框
combobox1 数据 = table01 数据
private void Form1_Load(object sender, EventArgs e)
{
using (UnitOfWork db = new UnitOfWork())
{
//لود کردن کمبوباکس لایه
comboBox1.DataSource = db.table01Repository.Get();
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "NameID";
}
}
combobox2 数据 = table02 数据 if NameID = NameID
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedValue.ToString() != null)
{
using (UnitOfWork db = new UnitOfWork())
comboBox2.DataSource = db.table02Repository.GetNameIDByFilter(comboBox1.SelectedValue.ToString());
comboBox2.DisplayMember = "CarName";
comboBox2.ValueMember = "CarID";
}
}
现在我的问题 如果我将 combox1 更改为 1 ( john )
我的数据在combobox2中=
奔驰
宝马
xXx
zZz
这是因为在 SelectedIndexChanged 中(在 table02 中全部计数为 1)
我必须如何将我的代码更改为 1 = 1(仅 1)而不是 1 = 1+ 0 或 1 = 1 + 1 + 0
我的资料库
public IEnumerable<table02> GetNameIDByFilter(string parameter)
{
return db.table02.Where(g => g.NameID.ToString().Contains(parameter)).ToList();
}
我发现我的错误
IRepository
IEnumerable<table02> GetNameIDByFilter(string parameter);
存储库
public IEnumerable<table02> GetNameIDByFilter(string parameter)
{
if (!int.TryParse(parameter, out int value))
{
return new List<table02>();
}
return db.table02.Where(g => g.NameID == value).ToList();
}
表单中的代码
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedValue = comboBox1.SelectedValue.ToString();
using (UnitOfWork db = new UnitOfWork())
if (!string.IsNullOrEmpty(selectedValue))
{
comboBox2.DataSource = db.table02Repository.GetNameIDByFilter(selectedValue);
comboBox2.DisplayMember = "CarName";
comboBox2.ValueMember = "CarID";
}