C#部件故障对应矩阵【初学者】

                               表一   部件故障对应表

 

                        表二     部件故障对应矩阵

 

需要将表一的内容转换成表二,目的是让用户一目了然,更加直接清楚的了解表一内容

Frm_BujianGuzhangJuZhen的写法:

public partial class Frm_BujianGuzhangJuZhen : Frm_Base
    {
        public Frm_BujianGuzhangJuZhen()
        {
            InitializeComponent();
        }

        IList<ColClass> collist = new List<ColClass>();

        /// <summary>
        ///  初期化加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Frm_BujianGuzhangJuZhen_Load(object sender, EventArgs e)
        {
            toolStrip_btn.BackgroundImage = RFIDMg.Properties.Resources.tooldown;
            this.panel_cx.BackgroundImage = null;
            //数据初始化
            InitList();
            //加载数据
            GetData();
        }

        /// <summary>
        /// 初始化表列
        /// </summary>
        public void InitList()
        {
            collist.Clear();
            this.table_data.BeginUpdate();
            this.table_data.ColumnModel.Columns.Clear();
            #region 创建列标题
            //第一列隐藏是自动增长列,便于修改/删除
            pubtable.ColAddNew(table_data, collist, "txt", "序号", "xuhao", 50, true);
            pubtable.ColAddNew(table_data, collist, "txt", "部件编码", "bujianCode", 70, true);
            pubtable.ColAddNew(table_data, collist, "txt", "部件名称", "bujianName", 100, true);
            #endregion
            //设置属性
            AddNewColumn();
            pubtable.TableInit(this.table_data);
            this.table_data.EndUpdate();
        }

        /// <summary>
        /// 加载新的动态列
        /// </summary>
        public void AddNewColumn()
        {
            IList list = null;
            //根据条件查询
            string bujianCode = this.txtBujianCode.Text;
            string bujianName = this.txtBujianName.Text;
            T_Base_BujianGuzhangDYInfoImp imp = new T_Base_BujianGuzhangDYInfoImp();
            list = (IList)imp.GetListGuZhangStyle(bujianCode, bujianName);
            #region 动态创建列标题
            foreach (T_Base_BujianGuzhangDYInfo info in list)
            {
                if (info.guzhangCount!="")
                {
                    for (int i = 1; i <= Convert.ToInt32(info.guzhangCount); i++)
                    {
                        pubtable.ColAddNew(table_data, collist, "txt", "故障" + NumberToChinese(i), "GuzhangCode" + i, 90, true);
                    }
                }
            }
            #endregion
        }

        /// <summary>
        /// 数字转中文
        /// </summary>
        /// <param name="number">待转义的数字</param>
        /// <returns>中文</returns>
        public string NumberToChinese(int number)
        {
            string res = string.Empty;
            string str = number.ToString();
            string schar = str.Substring(0, 1);
            switch (schar)
            {
                case "1":
                    res = "";
                    break;
                case "2":
                    res = "";
                    break;
                case "3":
                    res = "";
                    break;
                case "4":
                    res = "";
                    break;
                case "5":
                    res = "";
                    break;
                case "6":
                    res = "";
                    break;
                case "7":
                    res = "";
                    break;
                case "8":
                    res = "";
                    break;
                case "9":
                    res = "";
                    break;
                default:
                    res = "";
                    break;
            }
            if (str.Length > 1)
            {
                switch (str.Length)
                {
                    case 2:
                        res += "";
                        break;
                    case 3:
                        res += "";
                        break;
                    default:
                        res += "";
                        break;
                }
                if (str.Substring(0, 1) == "1" && str.Length == 2)
                {
                    res = "" + NumberToChinese(int.Parse(str.Substring(1, str.Length - 1)));//10~19以“十”开头
                }
                else
                {
                    res += NumberToChinese(int.Parse(str.Substring(1, str.Length - 1)));
                }
            }
            return res;
        }

        /// <summary>
        /// 加载表数据信息
        /// </summary>
        public void GetData()
        {
            string bujianCode = txtBujianCode.Text;
            string bujianName = txtBujianName.Text;
            IList list = null;
            T_Base_BujianGuzhangDYInfoImp imp = new T_Base_BujianGuzhangDYInfoImp();
            list = (IList)imp.GetGuZhangListByCodeName(bujianCode, bujianName);
            //数据加载到xptable中
            LoadData(list);
            this.table_data.TableModel.Selections.Clear();
        }

        /// <summary>
        /// 加载表数据
        /// </summary>
        /// <param name="list"></param>
        public void LoadData(IList list)
        {
            this.table_data.BeginUpdate();
            this.table_data.TableModel.Rows.Clear();
            int i = 1;
            CellStyle cellStyle = new CellStyle();
            cellStyle.ForeColor = Color.Navy;
            cellStyle.Font = new Font("Arial", 9, FontStyle.Bold);
            string newBuJianCode = "";
            int j = 1;
            //循环数据集进行加载数据
            foreach (T_Base_BujianGuzhangDYInfo info in list)
            {
                j++;

                if (newBuJianCode != info.bujianCode)
                {
                    j = 1;
                    newBuJianCode = info.bujianCode;
                    #region 动态加载每列数据
                    Type type = info.GetType();
                    Row row = new Row();
                    foreach (ColClass colinfo in collist)
                    {
                        Cell cellnew = new Cell();
                        if (colinfo.fcolstyle == "btn")
                        {
                            //按钮直接取ffield变量 不是从数据库读取
                            cellnew.Text = colinfo.ffield;
                            cellnew.ForeColor = ColorTranslator.FromHtml("#0052b3");
                        }
                        else
                        {
                            if (colinfo.ffield == "xuhao")
                            {
                                //序号为前台生成的
                                cellnew.Text = i.ToString();
                                cellnew.ForeColor = ColorTranslator.FromHtml("#0052b3");
                            }
                            else if (colinfo.ffield == "check")
                            {
                                //选择框前台 默认 否
                                cellnew.Checked = false;
                                cellnew.CellStyle = cellStyle;
                            }
                            else
                            {
                                if (colinfo.ffield.Contains("GuzhangCode"))
                                {
                                    cellnew.Text = "";
                                }
                                else
                                {
                                    //数据库字段 
                                    string fvalue = type.GetProperty(colinfo.ffield).GetValue(info, null).ToString();
                                    cellnew.Text = fvalue;
                                }
                            }
                        }
                        row.Cells.Add(cellnew);
                    }
                    this.table_data.TableModel.Rows.Add(row);
                    if (info.guzhangCode.ToString() != "")
                    {
                        int idIndex = pubtable.ColIndex(collist, "GuzhangCode1");
                        this.table_data.TableModel.Rows[i - 1].Cells[idIndex].Text = info.guzhangName;
                    }
                    i = i + 1;
                    #endregion
                }
                else
                {
                    if (info.guzhangCode.ToString()!="")
                    {
                        int idIndex = pubtable.ColIndex(collist, "GuzhangCode" +j);
                        this.table_data.TableModel.Rows[i - 2].Cells[idIndex].Text = info.guzhangName;
                    }
                }
            }
            this.table_data.EndUpdate();
            this.lblCount.Text = " 记录数:" +table_data.RowCount.ToString();
        }

        /// <summary>
        /// 数据查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearch_Click(object sender, EventArgs e)
        {
            InitList();
            GetData();
        }

        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }

主要函数的引用

        /// <summary>
        /// 获得故障列表、数量
        /// </summary>
        /// <param name="bujianCode">部件编码</param>
        /// <returns>返回故障数,列表</returns>
        public IList<T_Base_BujianGuzhangDYInfo> GetListGuZhangStyle(string bujianCode,string bujianName)
        {
            IList<T_Base_BujianGuzhangDYInfo> list = new List<T_Base_BujianGuzhangDYInfo>();
            string strwhere = "";
            if (bujianCode != "")
            {
                strwhere += " and BJ.bujianCode = '" + bujianCode + "' ";
            }
            if (bujianName != "")
            {
                strwhere += " and BJ.bujianName  like '%" + bujianName + "%' ";
            }
            string sqlstr = @"SELECT MAX(GZList.guzhangQty) as maxGZCount
                            FROM (SELECT a.bujianCode, count(a.bujianCode) as guzhangQty
                            from T_Base_BujianGuzhangDY a 
                            group by a.bujianCode) GZList
                            left join T_Base_bujian BJ  on GZList.bujianCode = BJ.bujianCode "
                            + "where 1=1 " + strwhere;
                            
            IDataReader Irdr = DBO.ExecuteReader(CommandType.Text, sqlstr, null);

            try
            {
                while (Irdr.Read())
                {
                    T_Base_BujianGuzhangDYInfo model = new T_Base_BujianGuzhangDYInfo();
                    model.guzhangCount = Irdr["maxGZCount"].ToString();

                        list.Add(model);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                Irdr.Close();
            }
            return list;

        }
 

        /// <summary>
        /// 根据部件编码获取所有故障情况
        /// </summary>
        /// <param name="bujianCode">部件编码</param>
        /// <param name="bujianName">部件名称</param>
        /// <returns>部件故障IList表</returns>
        public IList<T_Base_BujianGuzhangDYInfo> GetGuZhangListByCodeName(string bujianCode, string bujianName)
        {
            string strwhere = "";
            if (bujianCode != "")
            {
                strwhere += " and BJ.bujianCode = '" + bujianCode + "' ";
            }
            if (bujianName != "")
            {
                strwhere += " and BJ.bujianName like '%" + bujianName + "%' ";
            }
            string sqlstr = @"SELECT BJ.bujianCode,BJ.bujianName,DY.guzhangCode,GZ.guzhangName
                                FROM dbo.T_Base_Bujian BJ
                                LEFT JOIN dbo.T_Base_BujianGuzhangDY DY ON BJ.bujianCode=DY.bujianCode
                                LEFT JOIN dbo.T_Base_Guzhang GZ ON DY.guzhangCode=GZ.guzhangCode
                                 where 1=1 " + strwhere+
                                "order by BJ.bujianCode,DY.guzhangCode";
            IDataReader Irdr = DBO.ExecuteReader(CommandType.Text, sqlstr, null);
            IList<T_Base_BujianGuzhangDYInfo> list = new List<T_Base_BujianGuzhangDYInfo>();
            try
            {
                while (Irdr.Read())
                {
                    T_Base_BujianGuzhangDYInfo model = new T_Base_BujianGuzhangDYInfo();
                    model.bujianCode = Irdr["bujianCode"].ToString();
                    model.bujianName = Irdr["bujianName"].ToString();
                    model.guzhangCode = Irdr["guzhangCode"].ToString();
                    model.guzhangName = Irdr["guzhangName"].ToString();
                    
                    list.Add(model);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                Irdr.Close();
            }
            return list;
        }

 

posted @ 2017-07-23 14:05  奔驰毛毛虫  阅读(438)  评论(0编辑  收藏  举报