Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I Fill combo box by this way

C#
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 in linq query


C#
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;


What I have tried:

there is error appear

Unable to cast object of type 'KimyatPro.BranchTbl' to type 'System.IConvertible'.
Posted
Updated 4-May-16 18:48pm
v2
Comments
Karthik_Mahalingam 4-May-16 21:38pm    
post the code for BranchTbl class

The error you are saying is probably due to that last line in your 2nd snippet. Because
just before assigning the datasource, you have to force query execution to get the data in memory using ToList():

C#
DGVEmployee.DataSource = EmployeeData.ToList();
 
Share this answer
 
v3
Comments
medo67866 4-May-16 16:28pm    
the problem in this line

int BranchID=Convert.ToInt32(CBBranchName.SelectedValue);
Ehsan Sajjad 4-May-16 16:40pm    
that line is not causing that exception
medo67866 4-May-16 17:23pm    
please before reply read and check

don't write spam solution
I Solve the problem by adding

1) properties
C#
public int BranchSelectedID { get; set; }


2) Calss

C#
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) Bind combo Box code

C#
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

C#
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);
               }
           }
       }
 
Share this answer
 
Comments
Ehsan Sajjad 5-May-16 6:05am    
this does not make sense to me, why you need to add a property for just getting the selected value from the control

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900