本文介绍了检索组合框选定值时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过这种方式填写组合框



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";





我想在linq查询中选择组合值







I Want to take combo box Selected Value in linq query


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;





我有什么试过:



出现错误



无法投射'KimyatPro'类型的对象.BranchTbl'键入'System.IConvertible'。



What I have tried:

there is error appear

Unable to cast object of type 'KimyatPro.BranchTbl' to type 'System.IConvertible'.

推荐答案

DGVEmployee.DataSource = EmployeeData.ToList();


public int BranchSelectedID { get; set; }





2)Calss





2) Calss

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 construct that initiate the Class
           {
               ID = BranchID;
               Name = BranchName;
           }

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





3)绑定组合盒代码





3) Bind combo Box code

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));
           }

       }





之后绑定网格视图取决于组合Box选择的索引已更改





After that bind Grid View Depending on the Combo Box Selected Index Changed

private void CBBranchName_SelectedIndexChanged(object sender, EventArgs e)
       {

           int SelectedBranch = CBBranchName.SelectedIndex;

           BranchItem Selected = CBBranchName.Items[SelectedBranch] as BranchItem;
           if (Selected!=null)
           {
              BranchSelectedID = Selected.ID;
           }
           DGVEmployee.Rows.Clear();
           DataGridViewRow row = new DataGridViewRow();
           row.CreateCells(DGVEmployee);
           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)
               {
                   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);
               }
           }
       }


这篇关于检索组合框选定值时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:06
查看更多