using System;
using DevExpress.XtraGrid;
using System.Data;
using System.Windows.Forms;

namespace CustomerServiceSystem.ClassModule
{
 /// <summary>
 /// DataGridArray 的摘要说明。
 /// </summary>
 public class DataGridArray:System.Collections.CollectionBase
 {
  private DevExpress.XtraGrid.GridControl myPreOrderFood;
  private DevExpress.XtraGrid.GridControl myGrid;
  private DevExpress.XtraTab.XtraTabPage HostForm;
  private DevExpress.XtraGrid.Views.Grid.GridView myView; 
  private DevExpress.XtraEditors.PictureEdit foodPicture;
  //这里用索引器来实现控件数组的编号
  public DevExpress.XtraGrid.GridControl this [int Index]
  {
   get
   {
    return (DevExpress.XtraGrid.GridControl) this.List[Index];
   }
  }
  private string motherType,childType;
  private static int myIndex;
  DataSet gridSet;
  public DataGridArray(DevExpress.XtraTab.XtraTabPage host,string mtype,string ctype,int j,DevExpress.XtraGrid.GridControl gdcPreOrderFood,DevExpress.XtraEditors.PictureEdit ptbFoodPicture)
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
   HostForm = host;
   motherType=mtype;
   childType=ctype;
   myIndex=j;
   myPreOrderFood=gdcPreOrderFood;
   foodPicture=ptbFoodPicture;
  }
  public void AddNewGrid()
  {
   
   myGrid = new GridControl();
   this.List.Add(myGrid);
   myGrid.Dock=System.Windows.Forms.DockStyle.Fill;
   myGrid.Tag = myIndex;
   DataAccess gridAccess=new DataAccess();   
   myView=new DevExpress.XtraGrid.Views.Grid.GridView();
   myGrid.MainView=myView;
   myView.GroupPanelText="菜品列表";
   myView.OptionsBehavior.Editable=false;   
   myGrid.DataSource=gridAccess.Select("select 编号,名称 from Food where 大类别='"+motherType+"' and 小类别='"+childType+"'").Tables[0].DefaultView;
   HostForm.Controls.Add(myGrid);
  //要捕获的事件
   myGrid.Click += new System.EventHandler(myGrid_Click);
   myGrid.MouseUp += new System.Windows.Forms.MouseEventHandler(this.myGrid_MouseUp);
  }

  private void myGrid_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   
  }

  private void myGrid_Click(object sender, System.EventArgs e)
  {
     DataAccess gridAccess=new DataAccess();
   gridSet=gridAccess.Select("select * from Food where 编号='"+myView.GetFocusedRowCellValue("编号").ToString()+"' and 大类别='"+motherType+"' and 小类别='"+childType+"'");
   DataColumn gridColumn=new DataColumn();
   gridColumn.ColumnName="数量";
   gridColumn.DefaultValue=(object)1;
   gridSet.Tables[0].Columns.Add(gridColumn);
   myPreOrderFood.DataSource=gridSet.Tables[0].DefaultView;
   //-------------图片--------------
   string strPicPath=GetPictureDirectory()+gridSet.Tables[0].Rows[0][8].ToString();
   foodPicture.Image=System.Drawing.Image.FromFile(strPicPath);
   foodPicture.ToolTip=gridSet.Tables[0].Rows[0][9].ToString();
  }

  /// <summary>
  ///
  /// </summary>
  /// <returns></returns>
  private string GetPictureDirectory()
  {
   string pictureDirectory,mainfolder;
   mainfolder=Application.StartupPath;
   mainfolder=mainfolder.Substring(0,mainfolder.LastIndexOf("\\"));
   mainfolder=mainfolder.Substring(0,mainfolder.LastIndexOf("\\"));
   if ((mainfolder.LastIndexOf("\\"))>0)
   {
    mainfolder+=@"\FoodPicture\";
   }
   pictureDirectory=mainfolder;
   return pictureDirectory;
  }
 }
}