Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何使用objectdatasource在gridview中将强类型ID列替换为描述列_Asp.net_Gridview_Objectdatasource - Fatal编程技术网

Asp.net 如何使用objectdatasource在gridview中将强类型ID列替换为描述列

Asp.net 如何使用objectdatasource在gridview中将强类型ID列替换为描述列,asp.net,gridview,objectdatasource,Asp.net,Gridview,Objectdatasource,我已经从SaleDAL类填充了gridview,假设下面是代码,请忽略任何语法错误 public class Product { public int ProductID { } public string ProductName { } } public class Sale { public int SaleID { } public int ProductID

我已经从SaleDAL类填充了gridview,假设下面是代码,请忽略任何语法错误



    public class Product
    {
      public int ProductID
      { }
      public string ProductName
      { }
    }

    public class Sale
    {
      public int SaleID
      { }
      public int ProductID
      { }
      public int Amount
      { }
    }
    // Data Access Layers
    public class ProductsDAL
    {
       public IList<Product> GetProducts()
       { }
    }
    public class SaleDAL
    {
      public IList<Sale> GetSales()
      { }
      public void AddSale()
      {}
    }
    // populate gridview, 
    // ObjectDataSource1 is configured with SaleDAL
    GridView1.DataSourceID = "ObjectDataSource1";


公共类产品
{
公共int ProductID
{ }
公共字符串产品名称
{ }
}
公务舱销售
{
萨利德公共酒店
{ }
公共int ProductID
{ }
公共整数金额
{ }
}
//数据访问层
公共类产品
{
公共IList GetProducts()
{ }
}
公共级SaleDAL
{
公共IList GetSales()
{ }
公开发售()
{}
}
//填充gridview,
//ObjectDataSource1是用SaleDAL配置的
GridView1.DataSourceID=“ObjectDataSource1”;

第二列是ProductID,我想用ProductName替换它,最简单的方法是什么,我不想编写其他类,请注意,稍后还会对其执行编辑操作

如果列表中包含
ProductName
,则需要配置
GridView
(关闭自动生成列)。添加新的boundfield/templatefield,并使用
ProductName
值分配数据字段属性。

最终我找到了解决方案

     List<Sale> sales = new List<Sale>();
     Sale sale = new Sale();
     List<Product> products = new List<Product>();
     products = (List<Product>) ProductDAL.GetProducts();

     //r is SqlDataReader.

     _sale.SaleID = Convert.ToInt32(r["SaleID"]);
     _sale.ProductID = Convert.ToInt32(r["ProductID"]);
     _sale.ProductInRelation = products.Find(
                                                 delegate(Product p){
                                                 return p.PoroductID == _sale.ProductID;
                                             });
     _sales.Add(_sale);

     //now in gridview you can write 

     <%# Eval("ProductInRelation.ProductName") %>
List sales=newlist();
销售=新销售();
列表产品=新列表();
products=(List)ProductDAL.GetProducts();
//r是SqlDataReader。
_sale.SaleID=Convert.ToInt32(r[“SaleID”);
_sale.ProductID=Convert.ToInt32(r[“ProductID]”);
_sale.ProductInRelation=产品。查找(
代表(产品p){
返回p.PoroductID==\u sale.ProductID;
});
_销售。添加(_sale);
//现在,您可以在gridview中编写

与上面的代码一样,gridview通过ObjectDataSource(SaleDAL没有字段ProductName)与SaleDAL连接。如果我将ProductName添加到templatefield,它将如何知道当前行,因为它没有填充。