Exemplo n.º 1
0
        private void btnAxisAddOk_Click(object sender, EventArgs e)
        {
            String GroupName = "";
            String Name      = "";
            Int32  CardNum   = 0;
            Int32  AxisNum   = 0;
            Int32  Pulse     = 0;
            Int32  Acc       = 0;
            Int32  Speed     = 0;
            Int32  ResetNum  = 0;

            try
            {
                if (dgvAxisAdd.Rows.Count > 0)
                {
                    //检查数据
                    for (int i = 0; i < dgvAxisAdd.Rows.Count - 1; i++)
                    {
                        //检查数据是否为空
                        if (dgvAxisAdd.Rows[i].Cells[0].Value == null)
                        {
                            MessageBox.Show("第" + (i + 1).ToString() + "行的组名称为空,请检查");
                            return;
                        }
                        if (dgvAxisAdd.Rows[i].Cells[1].Value == null)
                        {
                            MessageBox.Show("第" + (i + 1).ToString() + "行的轴名称为空,请检查");
                            return;
                        }
                        if (dgvAxisAdd.Rows[i].Cells[2].Value == null)
                        {
                            MessageBox.Show("第" + (i + 1).ToString() + "行的卡号未选择,请检查");
                            return;
                        }
                        if (dgvAxisAdd.Rows[i].Cells[3].Value == null)
                        {
                            MessageBox.Show("第" + (i + 1).ToString() + "行的轴号未选择,请检查");
                            return;
                        }
                        if (dgvAxisAdd.Rows[i].Cells[4].Value != null && !int.TryParse(dgvAxisAdd.Rows[i].Cells[4].Value.ToString(), out Speed))
                        {
                            MessageBox.Show("第" + (i + 1).ToString() + "行的脉冲当量不是整数,请检查");
                            return;
                        }
                        if (dgvAxisAdd.Rows[i].Cells[5].Value != null && !int.TryParse(dgvAxisAdd.Rows[i].Cells[5].Value.ToString(), out Acc))
                        {
                            MessageBox.Show("第" + (i + 1).ToString() + "行的加速度不是整数,请检查");
                            return;
                        }
                        if (dgvAxisAdd.Rows[i].Cells[6].Value != null && !int.TryParse(dgvAxisAdd.Rows[i].Cells[6].Value.ToString(), out Speed))
                        {
                            MessageBox.Show("第" + (i + 1).ToString() + "行的速度不是整数,请检查");
                            return;
                        }
                        if (dgvAxisAdd.Rows[i].Cells[7].Value == null)
                        {
                            MessageBox.Show("第" + (i + 1).ToString() + "行的复位顺序未选择,请检查");
                            return;
                        }
                        //检查是否已经存在重复数据
                        Name    = dgvAxisAdd.Rows[i].Cells[1].Value.ToString();
                        CardNum = int.Parse(dgvAxisAdd.Rows[i].Cells[2].Value.ToString());
                        AxisNum = int.Parse(dgvAxisAdd.Rows[i].Cells[3].Value.ToString());
                        if (mWDataToolClass.SelectShowAxisCheck(" Name='" + Name + "'") == 1)
                        {
                            MessageBox.Show("第" + (i + 1).ToString() + "行的轴名称:" + Name + ";已存在,请检查");
                            return;
                        }
                        if (mWDataToolClass.SelectShowAxisCheck(" CardNum=" + CardNum + " And AxisNum=" + AxisNum) == 1)
                        {
                            MessageBox.Show("第" + (i + 1).ToString() + "行的卡号和轴号已存在,请检查");
                            return;
                        }
                        //增加的数据判断
                        int numRepeatName = 0;
                        int numRepeatCA   = 0;
                        for (int j = 0; j < dgvAxisAdd.Rows.Count - 1; j++)
                        {
                            if (i != j)
                            {
                                if (Name == dgvAxisAdd.Rows[j].Cells[1].Value.ToString())
                                {
                                    numRepeatName++;
                                }
                                if (CardNum == int.Parse(dgvAxisAdd.Rows[j].Cells[2].Value.ToString()) &&
                                    AxisNum == int.Parse(dgvAxisAdd.Rows[j].Cells[3].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 < dgvAxisAdd.Rows.Count - 1; i++)
                    {
                        GroupName = dgvAxisAdd.Rows[i].Cells[0].Value.ToString();
                        Name      = dgvAxisAdd.Rows[i].Cells[1].Value.ToString();
                        CardNum   = int.Parse(dgvAxisAdd.Rows[i].Cells[2].Value.ToString());
                        AxisNum   = int.Parse(dgvAxisAdd.Rows[i].Cells[3].Value.ToString());
                        if (dgvAxisAdd.Rows[i].Cells[4].Value == null)
                        {
                            Pulse = 100;
                        }
                        else
                        {
                            Pulse = int.Parse(dgvAxisAdd.Rows[i].Cells[4].Value.ToString());
                        }
                        if (dgvAxisAdd.Rows[i].Cells[5].Value == null)
                        {
                            Acc = 100;
                        }
                        else
                        {
                            Acc = int.Parse(dgvAxisAdd.Rows[i].Cells[5].Value.ToString());
                        }
                        if (dgvAxisAdd.Rows[i].Cells[6].Value == null)
                        {
                            Speed = 10;
                        }
                        else
                        {
                            Speed = int.Parse(dgvAxisAdd.Rows[i].Cells[6].Value.ToString());
                        }
                        ResetNum = int.Parse(dgvAxisAdd.Rows[i].Cells[7].Value.ToString());

                        if (mWDataToolClass.InsertAxis(GroupName, Name, CardNum, AxisNum, Pulse, Acc, Speed, ResetNum) != 0)
                        {
                            MessageBox.Show("第" + (i + 1).ToString() + "行的数据添加失败");
                            return;
                        }
                    }
                    closePanelUI();
                    //刷新轴数据
                    mWDataToolClass.SelectShowAxis(ref dgvAxis);
                    dgvAxisAdd.Rows.Clear();
                    MessageBox.Show("轴数据添加成功");
                }
                else
                {
                    MessageBox.Show("添加的数据为空");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }