C# 作业三

1,在窗体程序界面拖拽一个DataGridView控件,并且放置好位置

2,修改该DataGridView控件的名称和对应的窗体适配属性

3表格布局的基础设置

 

 #region   表格设置
 
        /// <summary>
        /// 添加表格标题
        /// </summary>
        private void AddTableTitle()
        {
            dtAllInfoData.Columns.Add("Number");
            dtAllInfoData.Columns.Add(ID);
            dtAllInfoData.Columns.Add(ParameterDesc);
            dtAllInfoData.Columns.Add(ParameterAddress);
        }
 
        /// <summary>
        /// 每一行都包含编辑删除按钮
        /// </summary>
        private void SingleRowOfEditAndDelete()
        {
            dlink1.Text = "修改";
            dlink1.Name = "Eidt";
            dlink1.HeaderText = "操作";
            dlink1.UseColumnTextForLinkValue = true;
            dlink1.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dlink2.DisplayIndex = 4;
            GridView_EqParameter.Columns.Add(dlink1);
           
            dlink2.Text = "删除";
            dlink2.Name = "Delete";
            dlink2.HeaderText = "操作";
            dlink2.UseColumnTextForLinkValue = true;
            dlink2.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;          
            dlink2.DisplayIndex = 5;
            GridView_EqParameter.Columns.Add(dlink2);
 
            dbutton.Text = "查看";
            dbutton.Name = "Look";
            dbutton.HeaderText = "操作";
            dbutton.UseColumnTextForButtonValue = true;
            dbutton.AutoSizeMode =
            DataGridViewAutoSizeColumnMode.AllCells;
            dbutton.FlatStyle = FlatStyle.Standard;
            dbutton.CellTemplate.Style.BackColor = Color.Honeydew;
            dbutton.DisplayIndex = 6;
            GridView_EqParameter.Columns.Add(dbutton);
 
        }
 
        /// <summary>
        /// 表格标题名称
        /// </summary>
        private void TableTitleName()
        {
            //修改表格标题头显示名称
            GridView_EqParameter.Columns[0].HeaderText = "序号";
            //GridView_EqParameter.Columns[1].HeaderText = "参数编号";
            GridView_EqParameter.Columns[2].HeaderText = "PLC检测参数名称";
            GridView_EqParameter.Columns[3].HeaderText = "PLC检测参数地址";
 
            //表格样式
            GridView_EqParameter.Columns[0].Width = 30;
            GridView_EqParameter.Columns[1].Width = 30;
            GridView_EqParameter.Columns[ID].Visible = false;//隐藏数据库的ID列
            GridView_EqParameter.Columns[2].Width = 80;
            GridView_EqParameter.Columns[3].Width = 80;
            GridView_EqParameter.Columns[4].Width = 30;
            GridView_EqParameter.Columns[5].Width = 30;
            GridView_EqParameter.Columns[6].Width = 20;
            //根据数据内容自动调整列、行宽
            //GridView_EqParameter.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader;
            //GridView_EqParameter.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders;
            GridView_EqParameter.ColumnHeadersDefaultCellStyle.Font= new Font("黑体", 15, FontStyle.Bold);
            GridView_EqParameter.RowsDefaultCellStyle.Font = new Font("黑体",13, FontStyle.Regular);
        }
 
        #endregion

数据加载

 

 

 #region   数据操作
 
        /// <summary>
        /// 加载当前设备参数类型的所有信息
        /// </summary>
        /// <param name="curEqParameterTypeName">当前设备参数类型名称</param>
        private void LoadCurEqParameterTypeAllInfos(string curEqParameterTypeName)
        {
            //清空缓存
            dtAllInfoData.Clear();
            if (!string.IsNullOrEmpty(curEqParameterTypeName))
            {
                //获取当前设备参数类型
                curEqParameterType = curEqParameterTypeName;
                Global_Parameter.curEqParameterType = curEqParameterType;
                //获取当前设备ID
                curEquipmentID = Global_Parameter.curEquipmentID;
 
                if (!string.IsNullOrEmpty(curEquipmentID))
                {
                    int eq_ID = int.Parse(curEquipmentID);
                    //加载角度信息
                    List<EqParameterData> eqParameterDatas = new List<EqParameterData>();
                    int numbers = CommonEqParameterOperation.GetInstance().QueryCurEqParameterOfTypeTotalNumbers(eq_ID, curEqParameterType);
                    eqParameterDatas = CommonEqParameterOperation.GetInstance().QueryCurEqParameterOfTypeAllInfos(eq_ID, curEqParameterType, 0, numbers);
                    int count = eqParameterDatas.Count;
                    if (count > 0)
                    {
                        for (int i = 0; i < count; i++)
                        {
                            row = dtAllInfoData.NewRow();
                            row["Number"] = i + 1;
                            row[ID] = eqParameterDatas[i].ID;
                            row[ParameterDesc] = eqParameterDatas[i].ParameterDesc;
                            row[ParameterAddress] = eqParameterDatas[i].ParameterAddress;
                            dtAllInfoData.Rows.Add(row);
 
                        }
                        if (row != null)
                        {
                            GridView_EqParameter.DataSource = dtAllInfoData;
                            if (firstOne == 1)
                            {
                                //每一行都包含编辑删除按钮
                                SingleRowOfEditAndDelete();
                            }
                            firstOne = 0;
                        }
 
                        //表格标题名称
                        TableTitleName();
 
                    }
                    else
                    {
                        MessageBox.Show("当前没有参数类型的数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
                else
                {
                    MessageBox.Show("请选择设备!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            else
            {
                MessageBox.Show("当前设备类型参数为NULL!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            
        }
 
 
        #endregion

获取当前选中数据行编辑信息

/// <summary>
        /// 获取到当前选中行的数据信息
        /// </summary>
        /// <param name="queryValueName">查询值的数据库字段名称</param>
        /// <returns></returns>
        private string GetCurSelectedRowData(string queryValueName)
        {
            string value = string.Empty;
            if (!string.IsNullOrEmpty(queryValueName))
            {
                value= GridView_EqParameter.Rows[GridView_EqParameter.CurrentRow.Index].Cells[queryValueName].Value.ToString();
            }
            return value;
        }

 编辑、删除当前选中行的事件方法

首先选中表格控件(DataGridView)的事件属性,双击如下所示的红框内容编写事件方法

 编写对应的事件方法

 //编辑、删除事件方法
        private void GridView_EqParameter_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int col = e.ColumnIndex;
 
            //获取到当前设备参数的ID
            curSelectedEqParameterID = GetCurSelectedRowData(ID);
            //编辑修改
            if (col == 4)
            {
                if (!string.IsNullOrEmpty(curSelectedEqParameterID))
                {
                    curSelectedEqParameterName = GetCurSelectedRowData(ParameterDesc);
                    curSelectedEqParameterAddress = GetCurSelectedRowData(ParameterAddress);
 
                    if (!string.IsNullOrEmpty(curSelectedEqParameterName))
                    {
                        Global_Parameter.curEqParameterID = curSelectedEqParameterID;
                        Global_Parameter.curEqParameterDesc = curSelectedEqParameterName;
                        Global_Parameter.curEqParameterAddress = curSelectedEqParameterAddress;
 
                        //打开编辑区域面板
                        EqParameterUpdate eqParameterUpdate = new EqParameterUpdate();
                        eqParameterUpdate.Show();
                    }
                    else
                    {
                        MessageBox.Show("设备参数信息获取失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    MessageBox.Show("当前设备参数ID为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
            }
            //删除
            if (col==5)
            {
                if (!string.IsNullOrEmpty(curSelectedEqParameterID))
                {
                    DialogResult dr = MessageBox.Show("你确定删除吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    if (dr == DialogResult.OK)
                    {
                        //删除信息
                        bool isSuccess = CommonEqParameterOperation.GetInstance().DeleteEqParameterInfo(curSelectedEqParameterID);
                        if (isSuccess)
                        {
                            MessageBox.Show("删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            EverydayLog.Write("删除成功!删除设备参数ID="+ curSelectedEqParameterID);
                            //刷新数据
                            RefreshData(curEqParameterType,true);
                        }
                        else
                        {
                            MessageBox.Show("删除失败!数据库连接失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            EverydayLog.Write("删除失败!数据库连接失败!");
                        }
                    }
                  
                }
                else
                {
                    MessageBox.Show("当前设备参数ID为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
            }
 
        }

结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值