• ComputerInfo


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    namespace OEMS
    {
        public partial class ComputerInfo : Form
        {
            DataTable dt = new DataTable();
            SqlDataAdapter sda = new SqlDataAdapter();
            DataSet ds = new DataSet();//定义全局变量,注意要new,不然后面引用的时候会出现ds为null
            DataSet dshardware = new DataSet();
            DataSet dswindow = new DataSet();
            string ComputerIDforDel;


            string connstring = System.Configuration.ConfigurationManager.AppSettings["OEMSconnstring"];
           // string connstring = @"server=192.168.1.12;database=OEMS;uid=nb;pwd=1";//连接字符串
            public ComputerInfo()
            {
                InitializeComponent();
            }
            private void FindToolStripMenuItem_Click(object sender, EventArgs e)
            {
                clear();
                find();
                RowsStateChanged();


            }//find按钮
            private void find()
            {
                pnHardwareInfo.Focus();//查找框获得焦点,以移除dgv的选中状态,来使得dgv的编辑框内容接受
                txtHardwareComputerID.Enabled = false;
                txtWindowInfoComputerID.Enabled = false;//禁用ID编辑框,为避免ID错乱
                string findvalue = txtFindsomething.Text.Trim();//获得文本框的值
                string cbbvalue = cbbfind.SelectedItem.ToString();//获得下拉框的选中值
                ds = appCode.DALL.findResult(findvalue, cbbvalue);
                dt = ds.Tables[0];
                ds.Tables[0].TableName = "ComputerInfo";
                dataGridView1.DataSource = dt;
            }
            //鼠标点击单元格事件
            private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
            {//鼠标点击单元格事件
                RowsStateChanged();//调用方法

            }
            private void getHardwareInfo(string ComputerID)
            {
                dshardware = appCode.DALL.findHardwareInfo(ComputerID);//获取Hardwareinfo信息
                if (dshardware.Tables[0].Rows.Count > 0)//如果该ds不为空,即没有输入hardwareInfo信息的时候,不做处理
                {
                    txtHardwareComputerID.Text = dshardware.Tables[0].Rows[0]["ComputerID"].ToString();
                    txtCpu.Text = dshardware.Tables[0].Rows[0]["Cpu"].ToString();
                    txtMotherboard.Text = dshardware.Tables[0].Rows[0]["Motherboard"].ToString();
                    txtMotherboardDate.Text = Convert.ToDateTime(dshardware.Tables[0].Rows[0]["MotherboardDate"]).Date.ToShortDateString();//取短时间
                    txtRAM.Text = dshardware.Tables[0].Rows[0]["Ram"].ToString();
                    txtHardDisk.Text = dshardware.Tables[0].Rows[0]["HardDisk"].ToString();
                    txtMonitor.Text = dshardware.Tables[0].Rows[0]["Monitor"].ToString();
                    txtMonitorDate.Text = Convert.ToDateTime(dshardware.Tables[0].Rows[0]["MonitorDate"]).Date.ToShortDateString();//取短时间
                }
                else
                {
                    txtboxtdefoult();
                }
            }//获取到HardwareInfo的dataset并读取内容到txtbox
            private void getHardwareTextBoxInfo()
            {
                if (dshardware.Tables[0].Rows.Count == 0)//这里判断如果dshardware里面有Rows,代表是修改数据,等于0就是没有Rows,就要添加
                {
                    //DataTable dtHardware = new DataTable();
                    dshardware.Tables[0].Rows.Add();//添加新行,由于在dgv的单元格点击事件中有查询,所以此出已经有表存在,但是没有Rows             
                }
                dshardware.Tables[0].Rows[0]["ComputerID"] = txtHardwareComputerID.Text;
                dshardware.Tables[0].Rows[0]["Cpu"] = txtCpu.Text;
                dshardware.Tables[0].Rows[0]["Motherboard"] = txtMotherboard.Text;
                dshardware.Tables[0].Rows[0]["MotherboardDate"] = txtMotherboardDate.Text;//取短时间
                dshardware.Tables[0].Rows[0]["Ram"] = txtRAM.Text;
                dshardware.Tables[0].Rows[0]["HardDisk"] = txtHardDisk.Text;
                dshardware.Tables[0].Rows[0]["Monitor"] = txtMonitor.Text;
                dshardware.Tables[0].Rows[0]["MonitorDate"] = txtMonitorDate.Text;
            }
            private void getWindowInfo(string ComputerID)
            {
                dswindow = appCode.DALL.findWindowInfo(ComputerID);//获取windowInfo信息.
                if (dswindow.Tables[0].Rows.Count > 0)//如果该ds为空,即没有输入windowInfo信息的时候,不做处理
                {
                    txtWindowInfoComputerID.Text = dswindow.Tables[0].Rows[0]["ComputerID"].ToString();
                    txtIPAdress.Text = dswindow.Tables[0].Rows[0]["IPAddress"].ToString();
                    txtMACAdress.Text = dswindow.Tables[0].Rows[0]["MACAddress"].ToString();
                    txtComputerName.Text = dswindow.Tables[0].Rows[0]["ComputerName"].ToString();
                }
            }//获取到WindowInfo的dataset并读取内容到txtbox
            private void getWindowTextBoxInfo()
            {
                if (dswindow.Tables[0].Rows.Count == 0)
                {
                    dswindow.Tables[0].Rows.Add();
                }
                dswindow.Tables[0].Rows[0]["ComputerID"] = txtWindowInfoComputerID.Text;
                dswindow.Tables[0].Rows[0]["IPAddress"] = txtIPAdress.Text;
                dswindow.Tables[0].Rows[0]["MACAddress"] = txtMACAdress.Text;
                dswindow.Tables[0].Rows[0]["ComputerName"] = txtComputerName.Text;

            }
            private void clear()//清楚文本框内容
            {
                txtHardwareComputerID.Clear();
                txtCpu.Clear();
                txtMotherboard.Clear();
                txtMotherboardDate.Text = DateTime.Now.Date.ToShortDateString();
                txtRAM.Clear();
                txtHardDisk.Clear();
                txtMonitor.Clear();
                txtMonitorDate.Text = DateTime.Now.Date.ToShortDateString();
                txtWindowInfoComputerID.Clear();
                txtIPAdress.Clear();
                txtMACAdress.Clear();
                txtComputerName.Clear();//每次激活此事件前先清理文本框的内容.
            }
            private void txtboxtdefoult()//清楚文本框内容
            {
                txtHardwareComputerID.Text = "";
                txtCpu.Text = "";
                txtMotherboard.Text = "";
                txtMotherboardDate.Text = "";
                txtRAM.Text = "";
                txtHardDisk.Text = "";
                txtMonitor.Text = "";
                txtMonitorDate.Text = "";
                txtWindowInfoComputerID.Text = "";
                txtIPAdress.Text = "";
                txtMACAdress.Text = "";
                txtComputerName.Text = "";
            }
            private void AddToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (ds.Tables.Count != 0)//如果当前ds有内容
                {
                    txtHardwareComputerID.Enabled = true;
                    txtWindowInfoComputerID.Enabled = true;//启用ID编辑框,为add新数据
                    ckbMoreInfo.Checked = true;//用来显示隐藏区域的内容
                    ((DataTable)dataGridView1.DataSource).Rows.Add(dt.NewRow());
                    dt.Rows[dt.Rows.Count - 1]["DateOfSet"] = DateTime.Now;//给默认字段赋值
                    dt.Rows[dt.Rows.Count - 1]["DateOfpur"] = "1999-9-9";//给默认字段赋值
                    dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[1];//新增行获得焦点
                    dataGridView1.BeginEdit(false);//新添加的行第一个单元格进入编辑状态
                }
                else//否则.不做任何处理.
                {
                    return;
                }
            }
            private void DeleteToolStripMenuItem_Click(object sender, EventArgs e)
            {
                pnHardwareInfo.Focus();
                if (ds.Tables.Count == 0)
                {
                    return;
                }
                int i = dataGridView1.CurrentCell.RowIndex;//得到当前的行号
                ComputerIDforDel = dataGridView1.Rows[i].Cells["ComputerID"].Value.ToString();//获取当前要删除行的主键
                if ((MessageBox.Show("Are You Sure You Want To Delete? " + dataGridView1.Rows[i].Cells[2].Value
                    + " ?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question)) == DialogResult.Yes)
                {

                    if (i != -1)
                    {
                        dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
                        //ckbIsNoDelete.Checked = true;
                        clear();
                        try
                        {
                            appCode.DALL.DeleteInfo(ComputerIDforDel, "HardwareInfo");
                            appCode.DALL.DeleteInfo(ComputerIDforDel, "WindowInfo");
                            appCode.others.UpdateByDataSet(ds, "ComputerInfo", connstring);
                            MessageBox.Show("Successfully Saved!", "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                           
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message.ToString(), "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            return;
                        }
                    }
                }
            }
            private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
            {
                txtFindsomething.Focus();
                int i;
                int j;
                int k;
                if (ckbMoreInfo.Checked)//选中了显示全部的选项
                {
                    if (ckbIsNoDelete.Checked)//判断是不是要错删除操作
                    {
                        //try
                        //{
                        //    appCode.DALL.DeleteInfo(ComputerIDforDel, "HardwareInfo");
                        //    appCode.DALL.DeleteInfo(ComputerIDforDel, "WindowInfo");
                        //    appCode.others.UpdateByDataSet(ds, "ComputerInfo", connstring);
                        //    MessageBox.Show("Successfully Saved!", "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        //    return;
                        //}
                        //catch (Exception ex)
                        //{
                        //    MessageBox.Show(ex.Message.ToString(), "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        //    return;
                        //}
                    }
                    //走到这里说明不是删除操作,是更新操作,这里没有判断是否有更改txtbox的值,全部提交
                    if (ds.HasChanges())//如果dataset有更改过,要这一步主要是用来确定是不是添加操作,如果是添加操作,先执行了这一步,下面那2个表才有外键关联.
                    {

                        i = appCode.others.UpdateByDataSet(ds, "ComputerInfo", connstring);
                        if (i != 0)
                        {
                            MessageBox.Show("Save Failed...", "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            return;
                        }
                    }
                    try
                    {
                        getHardwareTextBoxInfo();//将文本框的值写入Dataset
                        getWindowTextBoxInfo();
                    }
                    catch
                    {
                        MessageBox.Show("You Set A Wrong Date!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    };
                    j = appCode.others.UpdateByDataSet(dshardware, "HardwareInfo", connstring);//更新dataset
                    k = appCode.others.UpdateByDataSet(dswindow, "WindowInfo", connstring);
                    if (k == 0 && j == 0)
                    {
                        MessageBox.Show("Successfully Saved!", "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);


                    }
                    else
                    {
                        MessageBox.Show("Save Failed...", "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }

                }
                else
                {

                    MessageBox.Show("This Is Not Delete Time!Save Failed...", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }
            private void ckbMoreInfo_CheckedChanged(object sender, EventArgs e)
            {
                if (ckbMoreInfo.Checked == false)
                {
                    this.Width = 512;
                    clear();
                    this.CenterToScreen();
                }
                else
                {
                    this.Width = 922;
                    this.CenterToScreen();
                    RowsStateChanged();

                }
            }
            private void ComputerInfo_Load(object sender, EventArgs e)
            {           
                find();
            }
            private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
            {
                //string id = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1].Value.ToString();
                //txtHardwareComputerID.Text = id;
                //txtWindowInfoComputerID.Text = id;
            }
            private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
            {
                //try
                //{
                //    rowindex = dataGridView1.SelectedCells[0].RowIndex;
                //    columnindex = dataGridView1.SelectedCells[0].ColumnIndex;
                //}
                //catch
                //{


                //}

            }
            private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
            {

            }
            private void RowsStateChanged()//行获得焦点,焦点更改事件
            {
                dshardware.Tables.Clear();
                dswindow.Tables.Clear();
                try
                {
                    if (ckbMoreInfo.Checked)
                    {

                        //clear();//清楚当前文本框的内容
                        int index = dataGridView1.CurrentCell.RowIndex;//当前获得光标的是哪一行
                        string ComputerID = dataGridView1.Rows[index].Cells["ComputerID"].Value.ToString();//获得当前行的主键是
                        getHardwareInfo(ComputerID);
                        dshardware.Tables[0].TableName = "HardwareInfo";
                        getWindowInfo(ComputerID);
                        dswindow.Tables[0].TableName = "WindowInfo";
                        if (dshardware.Tables[0].Rows.Count==0)//如果没有查询到数据,将ID编辑框打开,以便可以输入数据
                        {
                            txtHardwareComputerID.Enabled = true;
                            txtWindowInfoComputerID.Enabled = true;//启用ID编辑框,为add新数据
                        }

                    }
                }
                catch
                {


                }
            }

            private void QuitToolStripMenuItem_Click(object sender, EventArgs e)
            {
                this.Close();
            }
            //删除的时候创建删除hardinfo的sql语句.在删除确认的时候先clear,执行删除hardinfo的方法.最后调用computerinfo的update方法

        }
    }

  • 相关阅读:
    c 中 static 关键字的作用
    关于声明变量关键字 extern 的搜索知识点
    思考在路上-虚拟机redhat系统安装tools
    一个小程序猿思考之路-头文件中#ifndef/#define/#endif作用和用法
    const 修饰的小看点(自己积点小知识)
    用css3实现闪烁效果
    icon font
    跟踪对象属性值的修改, 设置断点(Break on property change)
    setTimeout(fn, 0)引发的JavaScipt线程的思考
    "float: left;" div 不换行显示
  • 原文地址:https://www.cnblogs.com/unintersky/p/2913362.html
Copyright © 2020-2023  润新知