1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using 库存管理系统.Tool; 10 using System.Data.SqlClient; 11 using System.IO; 12 13 namespace 库存管理系统 14 { 15 public partial class frmMain : Form 16 { 17 18 private DataSet dsDept=new DataSet();//存放所有的部门信息 19 private DataSet dsUserType;//存放所有的用户类型信息 20 private DataSet dsSearchInfo = new DataSet(); 21 private CurrencyManager manger; 22 public EventHandler ShowPic { get; set; } 23 private DataTable dtInfo=new DataTable(); 24 private bool isBinded = false;//是否绑定过了 25 public frmMain() 26 { 27 InitializeComponent(); 28 } 29 30 31 private void 查询ToolStripMenuItem1_Click(object sender, EventArgs e) 32 { 33 frmSearch fs = new frmSearch(); 34 fs.ShowDialog(); 35 } 36 37 private void frmMain_FormClosed(object sender, FormClosedEventArgs e) 38 { 39 Application.Exit(); 40 } 41 42 private void frmMain_Load(object sender, EventArgs e) 43 { 44 toolStripStatusLabel1.Text = "欢迎您," + UserInfo.UserType + " " + UserInfo.UserName; 45 InitCombox(); 46 } 47 48 private void 关于ToolStripMenuItem_Click(object sender, EventArgs e) 49 { 50 MessageBox.Show("本程序由 微笑~蓝极光 编写\r\n 库存管理系统 ver0.31","关于",MessageBoxButtons.OK); 51 } 52 53 private void 新增ToolStripMenuItem1_Click(object sender, EventArgs e) 54 { 55 56 } 57 58 private void 当前页查询ToolStripMenuItem_Click(object sender, EventArgs e) 59 { 60 panelCurSearch.Visible = true; 61 panelRight.Visible = false; 62 } 63 64 private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) 65 { 66 if(DialogResult.OK == MessageBox.Show("您确定要退出吗?","提示",MessageBoxButtons.OKCancel)){ 67 Application.Exit(); 68 69 } 70 } 71 72 private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) 73 { 74 string text = tvOptions.SelectedNode.Text; 75 switch (text) 76 { 77 case "浏览": 78 //列出用户的所有信息 79 panelCurSearch.Visible = false; 80 panelRight.Visible = true; 81 DgvShow(); 82 83 break; 84 case "查询": 85 panelCurSearch.Visible = true; 86 panelRight.Visible = false; 87 break; 88 case "新页面查询": 89 frmSearch fs = new frmSearch(); 90 fs.ShowDialog(); 91 break; 92 case "新增": 93 break; 94 case "我的信息": 95 panelCurSearch.Visible = true; 96 txtID.Text = ""; 97 cmbDept.SelectedIndex = 0; 98 string Name = UserInfo.UserName; 99 txtName.Text = Name; 100 int DeptId = (int)cmbDept.SelectedValue; 101 ShowSearchResult(); 102 break; 103 } 104 } 105 106 107 //绑定数据到控件 108 private void BindToText() 109 { 110 if(isBinded){//如果绑定就移除绑定 111 txtLID.DataBindings.RemoveAt(0); 112 txtLName.DataBindings.RemoveAt(0); 113 cmbDept.DataBindings.RemoveAt(0); 114 txtAge.DataBindings.RemoveAt(0); 115 txtSalary.DataBindings.RemoveAt(0); 116 cmbLType.DataBindings.RemoveAt(0); 117 cmbLDept.DataBindings.RemoveAt(0); 118 cmbLGender.DataBindings.RemoveAt(0); 119 picBox.DataBindings.RemoveAt(0); 120 } 121 //绑定数据到控件 122 dtInfo=dsSearchInfo.Tables[0]; 123 txtLID.DataBindings.Add("Text", dtInfo,"EmpId"); 124 txtLName.DataBindings.Add("Text", dtInfo, "EmpName"); 125 cmbDept.DataBindings.Add("SelectedValue", dtInfo, "DeptId"); 126 cmbLGender.DataBindings.Add("Text", dtInfo, "EmpGender"); 127 txtAge.DataBindings.Add("Text", dtInfo, "EmpAge"); 128 txtSalary.DataBindings.Add("Text", dtInfo, "EmpSalary"); 129 cmbLType.DataBindings.Add("SelectedValue", dtInfo, "TypeId"); 130 cmbLDept.DataBindings.Add("SelectedValue", dtInfo, "DeptId"); 131 //将数据库中的图片byte[] 存储到图片控件(pictureBox)的tag标签中,CurrencyManager的positionChanged事件中更新图片控件的Image,实现图片显示: 132 picBox.DataBindings.Add("Tag", dtInfo, "ImageLogo"); 133 134 manger = this.BindingContext[dsSearchInfo.Tables[0]] as CurrencyManager; 135 manger.Position = 0; 136 } 137 138 139 140 //初始化combox的值 141 private void InitCombox() 142 { 143 144 cmbGender.Items.Add("不选"); 145 cmbGender.Items.Add("男"); 146 cmbGender.Items.Add("女"); 147 cmbGender.SelectedIndex = 0; 148 149 dsDept = SqlHelper.ExecuteDataSet("select DeptId,DeptName from T_Department"); 150 //绑定部门信息 151 cmbLDept.DataSource = SqlHelper.ExecuteDataSet("select DeptId,DeptName from T_Department").Tables[0]; 152 cmbLDept.ValueMember = "DeptId"; 153 cmbLDept.DisplayMember = "DeptName"; 154 //绑定管理员类型信息 155 cmbLType.DataSource = SqlHelper.ExecuteDataSet("select TypeId,TypeName from T_Type").Tables[0]; 156 cmbLType.ValueMember = "TypeId"; 157 cmbLType.DisplayMember = "TypeName"; 158 159 160 List<DeptNameInfo> depts = new List<DeptNameInfo>(); 161 depts.Add(new DeptNameInfo() { DeptName ="选择部门", DeptId =-2 }); 162 foreach (DataRow row in dsDept.Tables[0].Rows) 163 { 164 depts.Add(new DeptNameInfo() {DeptName=(string)row["DeptName"] ,DeptId=(int)row["DeptId"]}); 165 } 166 cmbDept.DataSource = depts; 167 cmbDept.ValueMember = "DeptId"; 168 cmbDept.DisplayMember = "DeptName"; 169 170 dsUserType = SqlHelper.ExecuteDataSet("select TypeId,TypeName from T_Type"); 171 172 } 173 174 private void DgvShow() 175 { 176 string sql = "select EmpId 'ID',EmpName '姓名',DeptName '部门名称',EmpGender '性别',EmpAge '年龄',EmpSalary '薪水',TypeName '用户类型' from T_Department,T_Employee,T_Type where T_Department.DeptId=T_Employee.DeptId and T_Type.TypeId=T_Employee.TypeId "; 177 dgvInfo.DataSource = SqlHelper.ExecuteDataSet(sql).Tables[0]; 178 } 179 180 private void btnSearch_Click(object sender, EventArgs e) 181 { 182 183 ShowSearchResult(); 184 MyShowPic(sender,e); 185 } 186 187 private void ShowSearchResult() 188 { 189 string ID = txtID.Text.Trim(); 190 191 string Name = txtName.Text.Trim(); 192 int DeptId = (int)cmbDept.SelectedValue; 193 string Gender = cmbGender.Text.Trim(); 194 string SignId = ""; 195 string SignDeptId = ""; 196 string SignGender = ""; 197 198 // 199 //控制符号 200 if (ID.Length < 1) 201 { 202 SignId = "!"; 203 } 204 if (DeptId < 0) 205 { 206 SignDeptId = "!"; 207 } 208 if (Gender == "不选") 209 { 210 SignGender = "!"; 211 } 212 213 string Sql = "select * from T_Employee where EmpId " + SignId + "= @EmpId and DeptId " + SignDeptId + "= @DeptId and EmpGender " + SignGender + "= @EmpGender and EmpName like '%" + Name + "%' "; 214 215 dsSearchInfo = SqlHelper.ExecuteDataSet(Sql, new SqlParameter("@EmpId", ID), new SqlParameter("@DeptId", DeptId), new SqlParameter("@EmpGender", Gender)); 216 BindToText(); 217 manger.PositionChanged += MyShowPic; 218 isBinded = true; 219 ShowInfo(); 220 } 221 222 private void btnFirst_Click(object sender, EventArgs e) 223 { 224 manger.Position = 0; 225 ShowInfo(); 226 } 227 228 private void btnLast_Click(object sender, EventArgs e) 229 { 230 manger.Position = dtInfo.Rows.Count - 1; 231 ShowInfo(); 232 } 233 234 private void btnPrev_Click(object sender, EventArgs e) 235 { 236 if (!isFirst()) 237 { 238 manger.Position -= 1; 239 ShowInfo(); 240 241 } 242 else 243 { 244 MessageBox.Show("已经到第一行!"); 245 } 246 } 247 248 private void btnNext_Click(object sender, EventArgs e) 249 { 250 if (!isLast()) 251 { 252 manger.Position += 1; 253 ShowInfo(); 254 } 255 else 256 { 257 MessageBox.Show("已经到最后一行!"); 258 } 259 } 260 261 private bool isLast() 262 { 263 264 return manger.Position >= dtInfo.Rows.Count - 1; 265 } 266 267 private bool isFirst() 268 { 269 270 return manger.Position <= 0; 271 } 272 273 private void ShowInfo() 274 { 275 int TotalCount=0; 276 int CurCount = 0; 277 if(dtInfo.Rows.Count>0){ 278 TotalCount = dtInfo.Rows.Count; 279 } 280 if (manger.Position>0) 281 { 282 CurCount = manger.Position; 283 } 284 labInfo.Text = "(" + (CurCount+1) + "/" + TotalCount + ")"; 285 } 286 287 private void 我的信息ToolStripMenuItem_Click(object sender, EventArgs e) 288 { 289 string ID = txtID.Text.Trim(); 290 291 string Name = UserInfo.UserName; 292 txtName.Text = Name; 293 int DeptId = (int)cmbDept.SelectedValue; 294 panelCurSearch.Visible = true; 295 ShowSearchResult(); 296 } 297 298 private void dgvInfo_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) 299 { 300 DataGridViewCell cell= dgvInfo.CurrentRow.Cells[0]; 301 if (cell.Value == null || ((string)(cell.Value))=="") 302 { 303 return; 304 } 305 string EmpId = (string)cell.Value; 306 txtID.Text = EmpId; 307 txtName.Text = ""; 308 cmbGender.SelectedIndex = 0; 309 cmbDept.SelectedIndex = 0; 310 panelCurSearch.Visible = true; 311 panelRight.Visible = false; 312 ShowSearchResult(); 313 314 } 315 316 //显示图片 317 public void MyShowPic(object sender, EventArgs e) 318 { 319 if (picBox.Tag != null && !(picBox.Tag is DBNull)) 320 { 321 byte[] imgSourse = (byte[])picBox.Tag; 322 323 MemoryStream ms = new MemoryStream(imgSourse); 324 picBox.Image = Image.FromStream(ms); 325 ms.Close(); 326 327 } 328 else 329 { 330 331 332 } 333 334 335 } 336 337 338 339 } 340 }
图片的显示效果:(实现的下边查找之后的数据分条导航显示)