Пример #1
0
        private void dgv_File_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex < 3 || e.RowIndex < 0 || e.ColumnIndex > 4 || e.RowIndex == dgv_File.RowCount)
            {
                return;                                                                                             //点击的不是按钮
            }
            //获取该项id
            int id = int.Parse(dgv_File.Rows[e.RowIndex].Cells[0].Value.ToString());

            if (e.ColumnIndex == 3)                                //编辑
            {
                object data = measureManager.GetMeasuringUnit(id); //查找该id对应的项
                if (data == null)
                {
                    MessageBox.Show("查无此项修改失败"); return;
                }                                                         //查无此项修改失败


                #region 反射生成窗体
                Type   formType = (measureManager.GetMeasuringUnit(id) as MeasuringUnit).formType;      //拿到窗体类型
                object form     = Activator.CreateInstance(formType, this, hWindow_Final1.Image, data); //通过窗体类型创建窗体
                //PropertyInfo propertyInfo = formType.GetProperty("Parent");//找到Paraent属性
                //propertyInfo.SetValue(form, splitContainer1.Panel2, null);//赋值Paraent属性
                MethodInfo methodInfo = formType.GetMethod("ShowDialog", new Type[] { }); //找到ShowDialog方法

                methodInfo.Invoke(form, null);                                            //执行ShowDialog方法
                #endregion
            }
            if (e.ColumnIndex == 4)//删除
            {
                if (MessageBox.Show("确定删除吗?", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    //执行删除操作
                    if (measureManager.RemoveMeasuringUnit(id) == null)
                    {
                        dgv_File.Rows.Remove(dgv_File.Rows[e.RowIndex]);//从表格中删除该行
                        return;
                    }
                }
            }
        }