namespace _2011_07_23
{
public partial class MainFrm : Form
{
SqlConnection conn = BaseClass.ConnDB();
public void GetInfo()
{
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter("select userno as '员工编号',username as '员工姓名',phonenumber as '联系方式',failtimes as '不及格次数' ,ID as '系统编号' from T_Users where usertype=0", conn);
DataSet ds = new DataSet();
sda.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
conn.Close();
}
public MainFrm()
{
InitializeComponent();
}
private void MainFrm_Activated(object sender, EventArgs e)
{
GetInfo();
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (txtEmpNO.Text == "" || txtName.Text == "" || txtPhone.Text == "")
{
MessageBox.Show("请将数据填写完整");
}
else
{
string sql = "insert into T_Users(userno,username,usertype,department,failtimes,password,phonenumber) values('" + txtEmpNO.Text.Trim() + "','" + txtName.Text.Trim() + "'," + "0,'c#',0,'123456'," + "'" + txtPhone.Text.Trim() + "')";
BaseClass.InsertData(sql);
txtEmpNO.Text = "";
txtName.Text = "";
txtPhone.Text = "";
GetInfo();
}
}
private void btnMod_Click(object sender, EventArgs e)
{
panelAdd.Visible = false;
panelMod.Visible = true;
if (dataGridView1.SelectedRows.Count < 1)
{
MessageBox.Show("你未选中任何行");
}
else
{
int m_id = Convert.ToInt32(dataGridView1.SelectedCells[5].Value);
string sql = "select * from T_Users where ID=" + m_id;
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
textBoxNO.Text = sdr["userno"].ToString();
textBoxName.Text = sdr["username"].ToString();
textBoxPhone.Text = sdr["phonenumber"].ToString();
conn.Close();
}
}
private void MainFrm_Load(object sender, EventArgs e)
{
panelAdd.Visible = true;
panelMod.Visible =false;
}
private void btnAd_Click(object sender, EventArgs e)
{
panelAdd.Visible = true;
panelMod.Visible = false;
}
private void btnModify_Click(object sender, EventArgs e)
{
int m_id = Convert.ToInt32(dataGridView1.SelectedCells[5].Value);
string sql = "update T_Users set userno='"+textBoxNO.Text.Trim()+"',username='"+textBoxName.Text.Trim()+"',phonenumber='"+textBoxPhone.Text.Trim()+"'where ID="+m_id;
BaseClass.UpdateData(sql);
}
private void btnDel_Click(object sender, EventArgs e)
{
ArrayList arr = new ArrayList();
bool check=false;
for (int i = 0; i < dataGridView1.RowCount; i++)
{
check = Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value);
if (check) //判断该复选框是否被选中
{
arr.Add(Convert.ToInt32(dataGridView1.Rows[i].Cells[5].Value.ToString()));
}
}
if (arr.Count == 0)
{
MessageBox.Show("请选择你要删除的员工");
}
else
{
DialogResult ds = MessageBox.Show("确定要删除吗?", "操作提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (ds == DialogResult.Yes)
{
foreach (int org_id in arr)
{
string sql = "delete from T_Users where ID=" + org_id;
conn.Open();
SqlCommand cmd = new SqlCommand(sql,conn);
cmd.ExecuteNonQuery();
conn.Close();
}
GetInfo();
}
}
}
}
}