//保存 private void btnsave_Click(object sender, EventArgs e) { try { if (adduser == true) { if (CheckDatas.Rows[0].Cells[0].Value.ToString() == string.Empty) { MessageBox.Show("用户名不能为空", "提示"); return; } if (CheckDatas.Rows[0].Cells[1].Value.ToString() == string.Empty) { MessageBox.Show("密码不能为空", "提示"); return; } if (CheckDatas.Rows[0].Cells[2].Value.ToString() == string.Empty) { MessageBox.Show("用户类型不能为空", "提示"); return; } int save = 0; save = sql.AddUser(CheckDatas.Rows[0].Cells[0].Value.ToString(), CheckDatas.Rows[0].Cells[1].Value.ToString(), CheckDatas.Rows[0].Cells[2].Value.ToString()); if (save == 1) { MessageBox.Show("新增用户名成功"); } adduser = false; //用户不能编辑单元格 CheckDatas.EditMode = DataGridViewEditMode.EditProgrammatically; string err = string.Empty; StringBuilder sb = new StringBuilder(); displaytable.Clear(); sb.Append("order by ID"); DataTable dt = sql.GetUser(sb.ToString(), out err); if (dt != null) { for (int i = 0; i < dt.Rows.Count; i++) { AddItemToTable(dt.Rows[i][0].ToString(), dt.Rows[i][1].ToString(), dt.Rows[i][2].ToString()); } } CheckDatas.DataSource = displaytable; CheckDatas.Columns[0].Width = 180; CheckDatas.Columns[1].Width = 180; CheckDatas.Columns[2].Width = 180; } else { MessageBox.Show("请新增再保存数据", "提示"); } } catch (Exception ex) { dy.savediary(DateTime.Now.ToString(), "保存数据错误:" + ex.Message, "错误"); MessageBox.Show(ex.Message, "保存数据"); } }