Exemplo n.º 1
0
        private void btnDOAddOk_Click(object sender, EventArgs e)
        {
            String GroupName = "";
            String Name      = "";
            Int32  CardNum   = 0;
            Int32  IoID      = 0;
            String mIoType   = "";
            Int32  IoType    = 0;
            String mIoState  = "";
            Int32  IoState   = 0;
            Int32  ExtendNum = 0;

            try
            {
                if (dgvDOAdd.Rows.Count > 0)
                {
                    //检查数据
                    for (int i = 0; i < dgvDOAdd.Rows.Count - 1; i++)
                    {
                        //检查数据是否为空
                        if (dgvDOAdd.Rows[i].Cells[0].Value == null)
                        {
                            MessageBox.Show("第" + (i + 1).ToString() + "行的组名为空,请检查");
                            return;
                        }
                        if (dgvDOAdd.Rows[i].Cells[1].Value == null)
                        {
                            MessageBox.Show("第" + (i + 1).ToString() + "行的端口名称为空,请检查");
                            return;
                        }
                        if (dgvDOAdd.Rows[i].Cells[2].Value == null)
                        {
                            MessageBox.Show("第" + (i + 1).ToString() + "行的卡号未选择,请检查");
                            return;
                        }
                        if (dgvDOAdd.Rows[i].Cells[3].Value == null)
                        {
                            MessageBox.Show("第" + (i + 1).ToString() + "行的端口号未选择,请检查");
                            return;
                        }
                        if (dgvDOAdd.Rows[i].Cells[4].Value == null)
                        {
                            MessageBox.Show("第" + (i + 1).ToString() + "行的端口类型选择,请检查");
                            return;
                        }
                        if (dgvDOAdd.Rows[i].Cells[5].Value == null)
                        {
                            MessageBox.Show("第" + (i + 1).ToString() + "行的端口状态选择,请检查");
                            return;
                        }
                        if (dgvDOAdd.Rows[i].Cells[4].Value.ToString() == "扩展" &&
                            dgvDOAdd.Rows[i].Cells[6].Value == null)
                        {
                            MessageBox.Show("第" + (i + 1).ToString() + "行的扩展卡号选择,请检查");
                            return;
                        }
                        //检查是否已经存在重复数据
                        Name    = dgvDOAdd.Rows[i].Cells[1].Value.ToString();
                        CardNum = int.Parse(dgvDOAdd.Rows[i].Cells[2].Value.ToString());
                        IoID    = int.Parse(dgvDOAdd.Rows[i].Cells[3].Value.ToString());
                        mIoType = dgvDOAdd.Rows[i].Cells[4].Value.ToString();

                        if (dgvDOAdd.Rows[i].Cells[6].Value == null)
                        {
                            ExtendNum = 0;
                        }
                        else
                        {
                            ExtendNum = int.Parse(dgvDOAdd.Rows[i].Cells[6].Value.ToString());
                        }

                        if (mWDataToolClass.SelectShowDOCheck(" Name='" + Name + "'") == 1)
                        {
                            MessageBox.Show("第" + (i + 1).ToString() + "行的轴名称:" + Name + ";已存在,请检查");
                            return;
                        }
                        if (mIoType == "扩展")
                        {
                            IoType = WUseToolClass.getIoType(mIoType);

                            if (mWDataToolClass.SelectShowDOCheck(
                                    " CardNum=" + CardNum.ToString() +
                                    " and IoID=" + IoID.ToString() +
                                    " and IoType=" + IoType.ToString() +
                                    " and ExtendNum=" + ExtendNum.ToString()
                                    ) == 1)
                            {
                                MessageBox.Show("第" + (i + 1).ToString() + "行的卡号、端口号、端口类型和扩展卡号已存在,请检查");
                                return;
                            }
                        }
                        else
                        {
                            IoType = WUseToolClass.getIoType(mIoType);

                            if (mWDataToolClass.SelectShowDOCheck(
                                    " CardNum=" + CardNum.ToString() +
                                    " and IoID=" + IoID.ToString() +
                                    " and IoType=" + IoType.ToString()
                                    ) == 1)
                            {
                                MessageBox.Show("第" + (i + 1).ToString() + "行的卡号、端口号和端口类型已存在,请检查");
                                return;
                            }
                        }

                        //增加的数据判断
                        int numRepeatName = 0;
                        int numRepeatCA   = 0;
                        for (int j = 0; j < dgvDOAdd.Rows.Count - 1; j++)
                        {
                            if (i != j)
                            {
                                if (Name == dgvDOAdd.Rows[j].Cells[0].Value.ToString())
                                {
                                    numRepeatName++;
                                }
                                if (mIoType == "扩展")
                                {
                                    if (CardNum == int.Parse(dgvDOAdd.Rows[j].Cells[2].Value.ToString()) &&
                                        IoID == int.Parse(dgvDOAdd.Rows[j].Cells[3].Value.ToString()) &&
                                        IoType == int.Parse(dgvDOAdd.Rows[j].Cells[4].Value.ToString()) &&
                                        ExtendNum == int.Parse(dgvDOAdd.Rows[j].Cells[6].Value.ToString()))
                                    {
                                        numRepeatCA++;
                                    }
                                }
                                else
                                {
                                    if (CardNum == int.Parse(dgvDOAdd.Rows[j].Cells[2].Value.ToString()) &&
                                        IoID == int.Parse(dgvDOAdd.Rows[j].Cells[3].Value.ToString()) &&
                                        IoType == int.Parse(dgvDOAdd.Rows[j].Cells[4].Value.ToString()))
                                    {
                                        numRepeatCA++;
                                    }
                                }
                            }
                        }
                        if (numRepeatName > 0)
                        {
                            MessageBox.Show("第" + (i + 1).ToString() + "行的轴名称有重复,请检查");
                            return;
                        }
                        if (numRepeatCA > 0)
                        {
                            MessageBox.Show("第" + (i + 1).ToString() + "行的卡号、端口号和端口类型(扩展卡号)有重复,请检查");
                            return;
                        }
                    }
                    //把所有数据插入
                    for (int i = 0; i < dgvDOAdd.Rows.Count - 1; i++)
                    {
                        GroupName = dgvDOAdd.Rows[i].Cells[0].Value.ToString();
                        Name      = dgvDOAdd.Rows[i].Cells[1].Value.ToString();
                        CardNum   = int.Parse(dgvDOAdd.Rows[i].Cells[2].Value.ToString());
                        IoID      = int.Parse(dgvDOAdd.Rows[i].Cells[3].Value.ToString());
                        mIoType   = dgvDOAdd.Rows[i].Cells[4].Value.ToString();
                        mIoState  = dgvDOAdd.Rows[i].Cells[5].Value.ToString();
                        if (dgvDOAdd.Rows[i].Cells[6].Value == null)
                        {
                            ExtendNum = 0;
                        }
                        else
                        {
                            ExtendNum = int.Parse(dgvDOAdd.Rows[i].Cells[6].Value.ToString());
                        }

                        IoType  = WUseToolClass.getIoType(mIoType);
                        IoState = WUseToolClass.getIoState(mIoState);

                        if (mWDataToolClass.InsertDO(GroupName, Name, CardNum, IoID, IoType, IoState, ExtendNum) != 0)
                        {
                            MessageBox.Show("第" + (i + 1).ToString() + "行的数据添加失败");
                            return;
                        }
                    }
                    closePanelUI();
                    //刷新轴数据
                    mWDataToolClass.SelectShowDO(ref dgvDO);
                    dgvDOAdd.Rows.Clear();
                    MessageBox.Show("轴数据添加成功");
                }
                else
                {
                    MessageBox.Show("添加的数据为空");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }