我有这个SQL查询,当我在SQL Management Studio中运行它时可以使用。

select Products.ProductName, ProductTypes.ProductType, count(SaleID) numSales
from Products
join ProductTypes on ( ProductTypes.ProductTypeID= Products.ProductTypeID)
join Sales on ( Sales.ProductID = Products.ProductID)
group by Products.ProductName, ProductTypes.ProductType


它返回该表。

c# - C#Visual Studio数据网格 View 返回0,其中SQL计数对我而言意味着-LMLPHP

我正在将其与Visual Studio数据网格视图集成在一起,并且正在努力使numSales列正常工作,现在对于这样的所有事情它仅显示0。

c# - C#Visual Studio数据网格 View 返回0,其中SQL计数对我而言意味着-LMLPHP

这是我填充表格的代码

private void DisplayDashboard()
        {
            string numSalesQuery = "select Products.ProductName, ProductTypes.ProductType, count(SaleID) numSales " +
                "from Products join ProductTypes on (ProductTypes.ProductTypeID = Products.ProductTypeID)" +
                "join Sales on (Sales.ProductID = Products.ProductID)" +
                "group by Products.ProductName, ProductTypes.ProductType";

            List<_Dashboard> dashList = new List<_Dashboard>();
            try
            {
                // Automatically  open and close the connection
                using (var conn = ConnectionManager.DatabaseConnection())
                using (var cmd = new SqlCommand(numSalesQuery, conn))
                using (var rdr = cmd.ExecuteReader())
                {

                    while (rdr.Read())
                    {
                        //Define the list items
                        var dashboard = new _Dashboard(
                            rdr["ProductName"].ToString(),
                            rdr["ProductType"].ToString(),
                            int.Parse(rdr["numSales"].ToString()));
                        dashList.Add(dashboard);
                    }
                    dgvDashboard.DataSource = dashList;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unsuccessful" + ex);
            }

        }


感谢您提供有关此问题的帮助。

编辑:
这是_Dashboard类

namespace Acme_Project.Business_Logic_Layer
{
    public class _Dashboard
    {
        //Declare properties of a Customer

        public String ProductName { get; set; }
        public string ProductType { get; set; }
        public int NumSales { get; set; }

        //Declaring Default Constructor
        public _Dashboard() { }

        //Parameterised Constructor
        public _Dashboard(string productname, string productype, int numsales)
        {
            ProductName = productname;
            ProductType = productype;
            NumSales = NumSales;
        }
    }
}

最佳答案

在参数化构造函数中将NumSales = NumSales设置为NumSales = numsales

关于c# - C#Visual Studio数据网格 View 返回0,其中SQL计数对我而言意味着,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44987308/

10-11 07:55