-1

i have populated gridview from SaleDAL class, assuming following is the code, Please ignore any syntax error



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

2nd column is ProductID, i want to replace it with ProductName, what is the easiest way, i dont wnat to write an other class, please note that later edit action will also be performed on it.

sairfan
  • 970
  • 2
  • 12
  • 20

2 Answers2

0

If your list contains ProductName then you need to configure your GridView (Turn off autogenerated columns). Add new boundfield/templatefield and assign datafield property with ProductName value.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • as in above code, gridview is connected with SaleDAL through ObjectDataSource (SaleDAL does not have field ProductName) some how if i add ProductName to templatefield how it will know the current row, coz its not filled. – sairfan Jan 26 '12 at 12:15
0

finally i found the solution,

     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") %>
sairfan
  • 970
  • 2
  • 12
  • 20