FrmComponentList.cs 9.0 KB
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using OnlineStore.LoadCSVLibrary;
using OnlineStore.Common;

namespace OnlineStore.AutoCountClient
{
    internal partial class FrmComponentList : FrmBase
    {
        public static  FrmComponentList instance = new FrmComponentList();
        private  FrmComponentList()
        {
            InitializeComponent();
        }

        private void FrmPointType_Load(object sender, EventArgs e)
        {  
        }
        

        
        private DataGridViewRow SetRowInfo(DataGridViewRow view, ComponetInfo com)
        {
            if (view == null)
            {
                view = new DataGridViewRow();
                view.CreateCells(dgvList);
            }
            view.Cells[Column_PN.Index].Value = com.PartNum.ToString(); 
            view.Cells[this.Column_Name.Index].Value = com.ProductName.ToString();
            view.Cells[Column_F.Index].Value = com.Encapsulations.ToString();
            view.Cells[this.Column_Des.Index].Value = com.Describe.ToString();
            
            return view;
        }
         
        private void btnSave_Click(object sender, EventArgs e)
        {
            ////if (groupInfo.Text.StartsWith("新增"))
            ////{
            ////    //AddCom();
            ////}
            ////else
            //{
            //    if (dgvList.SelectedRows != null && dgvList.SelectedRows.Count > 0)
            //    {
            //        int rowIndex = dgvList.SelectedRows[0].Index;
               
            //        DataGridViewRow row = dgvList.Rows[rowIndex];
            //        ComponetInfo obj = getRowPointInfo(row);
            //        if (obj == null)
            //        {
            //            MessageBox.Show(ResourceCulture.GetString(ResourceCulture.SelectC,"请选择元器件!"));
            //            return;
            //        }
            //        if (cmbPositionNumList.SelectedIndex < 0)
            //        {
            //            MessageBox.Show(ResourceCulture.GetString(ResourceCulture.SelectCPosition,"请选择元器件位置!"));
            //            cmbPositionNumList.Focus();
            //            return;
            //        }
            //        TSAVPosition position = (TSAVPosition)cmbPositionNumList.SelectedItem;
                 
            //        obj.ComponentName = FormUtil.getValue(txtName);
            //        obj.ComponentDes = FormUtil.getValue(txtDes);
            //        obj.ComCount = FormUtil.GetIntValue(txtCount);
            //        obj.Notes = FormUtil.getValue(txtNotes);
            //        obj.PartNum = FormUtil.getValue(txtPartNum);

            //        obj.PositionNum = position.PositionNum;

            //        if (obj.ComponentName.Equals(""))
            //        {
            //            MessageBox.Show(ResourceCulture.GetString(ResourceCulture.WriteComName,"请输入元器件名称!"));
            //            txtName.Focus();
            //            return;
            //        }
            //        //if (obj.PositionNum.Equals(""))
            //        //{
            //        //    MessageBox.Show("请输入元器件所在位置!");
            //        //    this.txtPosition.Focus();
            //        //    return;
            //        //}
            //        if (obj.ComCount<=0)
            //        {
            //            MessageBox.Show(ResourceCulture.GetString(ResourceCulture.WriteComNum,"请输入元器件数量!"));
            //            txtCount.Focus();
            //            return;
            //        }

            //        CSVReaderBomManager.SaveComponet(cmbList.Text, obj);
            //        //ComponentManager.Update(obj);
            //        SetRowInfo(dgvList.Rows[rowIndex], obj);
            //        MessageBox.Show(ResourceCulture.GetString(ResourceCulture.ComSaveOk,"元器件【{0}】保存成功!", obj.PartNum + "-" + obj.ComponentName ));
            //        groupInfo.Text = ResourceCulture.GetString(ResourceCulture.ComInfo,"元器件【{0}】的基本信息",obj.PartNum + "-" + obj.ComponentName);
            //    }
            //}
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows != null && dgvList.SelectedRows.Count > 0)
            {
                int rowIndex = dgvList.SelectedRows[0].Index;
                DeleteCom(rowIndex);
            }
        }

        private ComponetInfo getRowPointInfo(DataGridViewRow row)
        {
            ComponetInfo point = new ComponetInfo();
            try
            {
                if (row.Cells[Column_PN.Name].Value == null)
                {
                    return null;
                } 
                point.PartNum = row.Cells[this.Column_PN.Name].Value.ToString();
                point.Encapsulations = row.Cells[this.Column_F.Name].Value.ToString(); 
                point.ProductName = row.Cells[this.Column_Name.Name].Value.ToString();
                point.Describe = row.Cells[this.Column_Des.Name].Value.ToString(); 
             
            }
            catch (Exception ex)
            {
                LogUtil.error( "保存数据出错:" + ex.ToString()); 
                return null;
            }
            return point;
        }
        private void showDetail(int rowIndex)
        {
            DataGridViewRow row = dgvList.Rows[rowIndex];
            ComponetInfo obj = getRowPointInfo(row);
            if (obj == null)
            {
                MessageBox.Show( "请选择元器件!" );
                return;
            }
            txtPartNum.Tag = obj;
            txtName.Text = obj.ProductName;
            txtDes.Text = obj.Describe;
            txtFengzhuang.Text = obj.Encapsulations.ToString();
            txtPartNum.Text = obj.PartNum;  
            int index = -1;
         
            groupInfo.Text =   "元器件【"+ obj.PartNum + "】的基本信息";
        }
        private void DeleteCom(int rowIndex)
        {
            DataGridViewRow row = dgvList.Rows[rowIndex];
            ComponetInfo obj = getRowPointInfo(row);
            if (MessageBox.Show(
              "确认要删除元器件【" + obj.PartNum + "】吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }
            else
            {
                CSVBomManager.RemoveCom(obj);
                this.dgvList.Rows.RemoveAt(rowIndex);
                MessageBox.Show("元器件【" + obj.PartNum + "】删除成功!", "提示");
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            //this.Close();
            this.Visible = false;
            //FrmMenu.frmMain.Visible = true;
        }

        private void dgvList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1 && e.ColumnIndex >= 0)
            {
                string name = this.dgvList.Columns[e.ColumnIndex].Name;
                if (name.Equals(this.Column_Del.Name))
                {
                    DeleteCom(e.RowIndex);
                }  
            }
        }

        private void dgvList_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows != null && dgvList.SelectedRows.Count > 0)
            {
                int rowIndex = dgvList.SelectedRows[0].Index;
                showDetail(rowIndex);
            }
        }

        private void btnDownLoad_Click(object sender, EventArgs e)
        {
            //SaveFileDialog sfd = new SaveFileDialog();
            //sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); 
            //sfd.FileName = ResourceCulture.GetString( "元器件库模板");
            //sfd.Filter = @"csv|*.csv";
            //DialogResult result = sfd.ShowDialog();

            //if (result.Equals(DialogResult.OK))
            //{
            //    string filePath = sfd.FileName;
            //    //if (Directory.Exists(filePath))
            //    {
            //        string sourcePath = Application.StartupPath + ConfigAppSettings.GetValue(Setting_Init.Componet_Template);
            //        if (File.Exists(filePath))
            //        {
            //            File.Delete(filePath);
            //        }
            //        //复制文件 
            //        File.Copy(sourcePath, filePath); 
            //        MessageBox.Show(ResourceCulture.GetString(ResourceCulture.DemoSave,"模板已保存在:" )+ filePath);
            //    }
            //}
        }
 
      
        private void FrmComponentList_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Visible = false;
            //FrmMenu.frmMain.Visible = true;
            e.Cancel = true;
        }

        private void FrmComponentList_Shown(object sender, EventArgs e)
        { 
           
        }

      
    }
}