Beispiel #1
0
        /// <summary>
        /// 保存到数据库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    //判断是否被选中
                    Employee employee   = new Employee();
                    var      properties = employee.GetType().GetProperties();
                    employee.CreateTime   = DateTime.Now;
                    employee.CreateUserId = AppClientContext.CurrentUser.Id;
                    employee.UpdateUserId = AppClientContext.CurrentUser.Id;
                    employee.UpdateTime   = DateTime.Now;
                    foreach (var item in properties)
                    {
                        if (dicColumn.ContainsKey(item.Name))
                        {
                            string type = item.PropertyType.Name;
                            if (item.Name == "BirthDay" || item.Name == "WorkTime" || item.Name == "CardDate")
                            {
                                if (dataGridView1.Rows[i].Cells[dicColumn[item.Name]].Value != null)
                                {
                                    DateTime dt        = new DateTime();
                                    bool     isSuccess = DateTime.TryParse(dataGridView1.Rows[i].Cells[dicColumn[item.Name]].Value.ToString(), out dt);
                                    if (isSuccess)
                                    {
                                        item.SetValue(employee, dt, null);
                                    }
                                    else
                                    {
                                        MessageBox.Show(dicColumn[item.Name] + "时间格式转换失败!!!", "Error");
                                        return;
                                    }
                                }
                            }
                            else if (item.Name == "DepartmentId")
                            {
                                if (dataGridView1.Rows[i].Cells[dicColumn[item.Name]].Value != null)
                                {
                                    item.SetValue(employee, dicDepartment[dataGridView1.Rows[i].Cells[dicColumn[item.Name]].Value.ToString()], null);
                                }
                                else
                                {
                                    MessageBox.Show(dicColumn[item.Name] + "不能为空!!!", "Error");
                                    return;
                                }
                            }
                            else
                            {
                                if (item.Name == "Address" || item.Name == "EmployStatusValue" || item.Name == "PharmacistsTitleTypeValue" || item.Name == "PharmacistsTitleTypeValue")
                                {
                                    if (dataGridView1.Rows[i].Cells[dicColumn[item.Name]].Value == null)
                                    {
                                        MessageBox.Show(dicColumn[item.Name] + "不能为空!!!", "Error");
                                        return;
                                    }
                                }
                                switch (type)
                                {
                                case "String":
                                    if (dataGridView1.Rows[i].Cells[dicColumn[item.Name]].Value != null)
                                    {
                                        item.SetValue(employee, dataGridView1.Rows[i].Cells[dicColumn[item.Name]].Value.ToString(), null);
                                    }
                                    break;

                                case "Int32":
                                    if (dataGridView1.Rows[i].Cells[dicColumn[item.Name]].Value != null)
                                    {
                                        item.SetValue(employee, Convert.ToInt32(dataGridView1.Rows[i].Cells[dicColumn[item.Name]].Value.ToString()), null);
                                    }
                                    break;
                                }
                            }
                        }
                    }
                    string msg = string.Empty;
                    PharmacyDatabaseService.AddEmployee(out msg, employee);
                    if (msg.Length > 0)
                    {
                        MessageBox.Show("员工录入保存失败!!!", "Error");
                        Log.Error(msg);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
                Log.Error(ex);
            }
        }