简体   繁体   English

从具有数据源的组合框中获取选定的值是匿名类型

[英]get selected value from combobox with data source is anonymous type

I Fill combo box by this way 用这种方式填充组合框

KimyatEntities db = new KimyatEntities();
var BranchData = db.SpSelectBranch();
CBBranchName.DataSource = BranchData.ToList();
CBBranchName.DisplayMember = "Branch_Name";
CBBranchName.ValueMember = "Branch_ID";

I Want to take combo box Selected Value by linq query 我想通过LINQ查询采取组合框选择值

int BranchID=Convert.ToInt32(CBBranchName.SelectedValue);



var EmployeeData = from E in db.EmployeeTbls
                   join B in db.BranchTbls
                   on E.Branch_ID equals B.Branch_ID
                   where E.Branch_ID == BranchID
                   select new { E.Employee_Name, E.Hire_Date, B.Branch_Name };

DGVEmployee.DataSource = EmployeeData.ToList();

Try this: 尝试这个:

var EmployeeData = from E in db.EmployeeTbls
                         join B in db.BranchTbls
                         on E.Branch_ID equals B.Branch_ID
                         where E.Branch_ID == BranchID
                         select new { Branch_ID= E.Branch_ID , Branch_Name= B.Branch_Name };

I solved the problem by adding: 我通过添加以下内容解决了该问题:

1) Properties 1)属性

public int BranchSelectedID { get; set; }

2) Class 2)班级

private class BranchItem
{
    // I Take this class variable to take the Selected value from combo box control
    public int ID; 
    public string Name;

    public BranchItem(int BranchID, string BranchName) //This is a construct that initiates the class 
    {
        ID = BranchID;
        Name = BranchName;
    }

    public override string ToString()
    {
        return Name; //this for text that appear in Combo Box control
    }
}

3) Bind combo Box code 3)绑定组合框代码

void bindBranch_ComboBox()
{
    db = new KimyatEntities();
    CBBranchName.Items.Clear();
    var BranchData = (from B in db.BranchTbls
                      select new { B.Branch_ID, B.Branch_Name }).ToList();
    foreach (var item in BranchData)
    {
        CBBranchName.Items.Add(new BranchItem(item.Branch_ID, item.Branch_Name));
    }

}

After that bind Grid View Depending on the Combo Box Selected Index Changed 之后,根据组合框绑定网格视图,选定索引已更改

private void CBBranchName_SelectedIndexChanged(object sender, EventArgs e)
{
    //The SelectedIndexChanged event

    int SelectedBranch = CBBranchName.SelectedIndex;

    BranchItem Selected = CBBranchName.Items[SelectedBranch] as BranchItem;
    if (Selected!=null)
    {
       BranchSelectedID = Selected.ID;
    }
    DGVEmployee.Rows.Clear();
    var EmployeeData = from E in db.EmployeeTbls
                       join B in db.BranchTbls
                       on E.Branch_ID equals B.Branch_ID
                       where E.Branch_ID == BranchSelectedID
                       select new { E.Employee_ID, E.Employee_Name, E.Hire_Date, B.Branch_Name };
    if (EmployeeData != null)
    {
        foreach (var item in EmployeeData)
        {
          DataGridViewRow row = new DataGridViewRow();
          row.CreateCells(DGVEmployee);
            row.Cells[0].Value = item.Employee_ID;
            row.Cells[1].Value = item.Employee_Name;
            row.Cells[2].Value = item.Hire_Date;
            row.Cells[3].Value = item.Branch_Name;
            DGVEmployee.Rows.Add(row);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM