Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity.Core.Objects;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CoffeeShopApp
{
    public partial class frmViewProduct : Form
    {
        CoffeeShopDBEntities cse = new CoffeeShopDBEntities();

        public frmViewProduct()
        {
            InitializeComponent();

            dgv.DataSource = cse.tblProducts.ToList();
            dgv.Columns["ProductType"].Visible = false;
            dgv.Columns["tbleProductType"].Visible = false;
            dgv.Columns["tblTransactionItems"].Visible = false;
        }

        private void cboProductType_SelectionChangeCommitted(object sender, EventArgs e)
        {
            ObjectQuery<tblProduct> filteredProducts = new ObjectQuery<tblProduct>(
                "select value product from tblProduct as product where product.ProductType =" + cboProductType.SelectedValue, cse);--->> I got error here with cse

            dgv.DataSource = filteredProducts;
        }
    }
}
Posted
Comments
Member 11232188 28-Nov-15 21:33pm    
I need help
So, clearly there is a problem in conversion. First execute the query and populate in a datatable. Use that as the datasource.
Member 11232188 29-Nov-15 0:51am    
How???

Try with below processes:

Through entity Entity SQL:
C#
//Querying with Object Services and Entity SQL
string sqlString = "SELECT VALUE st FROM CoffeeShopDBEntities.tblProduct AS st WHERE st.ProductType == '" + cboProductType.SelectedValue+ "'";

var objctx = (ctx as IObjectContextAdapter).ObjectContext;
		
ObjectQuery<tblproduct> filteredProducts = objctx.CreateQuery<tblproduct>(sqlString);
</tblproduct></tblproduct>


Through Native SQL:
C#
using (var ctx = new CoffeeShopDBEntities())
{
	var filteredProducts = ctx.tblProduct.SqlQuery("select value product from tblProduct as product where product.ProductType = '" + cboProductType.SelectedValue + "'").ToList<tblproduct>();
}
</tblproduct>

Note: Above code written as assuming the entity name as tblProduct. If it is different like "Products" in EF then change it.
 
Share this answer
 
See the example here - Databinding with DataGridView in ADO.NET[^].
 
Share this answer
 

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