C# 客户代码
1 using drp.Common; 2 using drp.DB; 3 using System; 4 using System.Collections; 5 using System.Collections.Generic; 6 using System.ComponentModel; 7 using System.Data; 8 using System.Drawing; 9 using System.Linq; 10 using System.Reflection; 11 using System.Text; 12 using System.Threading.Tasks; 13 using System.Windows.Forms; 14 15 namespace drp 16 { 17 public partial class FrmClient : Form 18 { 19 DataTable dtList; 20 DataTable dtLevel1, dtLevel2, dtType1, dtType2; 21 BindingSource bs = new BindingSource(); 22 string strColumns; 23 //是否添加状态 24 int addFlag = 0; 25 //判断类型下拉框的值是否改变 26 string oriCategory2, oriLevel1, oriLevel2; 27 int i = 0; 28 29 public FrmClient() 30 { 31 //1、设置窗体的双缓冲,为解决表卡顿问题 32 this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint, true); 33 this.UpdateStyles(); 34 35 InitializeComponent(); 36 //2、利用反射设置DataGridView的双缓冲,为解决表卡顿问题 37 Type dgvType = this.dataGridView1.GetType(); 38 PropertyInfo pi = dgvType.GetProperty("DoubleBuffered", 39 BindingFlags.Instance | BindingFlags.NonPublic); 40 pi.SetValue(this.dataGridView1, true, null); 41 #region 初始化客户类型下拉列表 42 ArrayList mylist = new ArrayList(); 43 mylist.Add(new DictionaryEntry("0", "全部")); 44 mylist.Add(new DictionaryEntry("1", "商业")); 45 mylist.Add(new DictionaryEntry("3", "门店")); 46 mylist.Add(new DictionaryEntry("2", "医院")); 47 mylist.Add(new DictionaryEntry("6", "连锁总部")); 48 cbxCategory.DataSource = mylist; 49 cbxCategory.DisplayMember = "Value"; 50 cbxCategory.ValueMember = "Key"; 51 cbxCategory.SelectedIndex = 0; 52 #endregion 53 strColumns = " id, name, category, level1, regioncode, address, key1, key2, key3, key0, isflow, ischain, registerednumber"; 54 55 strColumns = @" id,name,category,regioncode,regionname,isflow,key1,level1,level2, 56 type1,type2,type3,depart1,depart2,depart3,depart4,depart5,key2,key3,key0,registerednumber, 57 alias1,alias2,sapid,address,pinyin,telphone,postcode,isagreement,ischain, 58 parentid,createby,createtime,modifyby,modifytime,note "; 59 60 } 61 62 private void button4_Click(object sender, EventArgs e) 63 { 64 this.Close(); 65 } 66 //详情页关闭按钮 67 private void button10_Click(object sender, EventArgs e) 68 { 69 this.Close(); 70 } 71 72 private void btnSearch_Click(object sender, EventArgs e) 73 { 74 string clientChar; 75 string sql; 76 string strWhere; 77 sql = "select " + strColumns; 78 sql += " from client where 1=1 "; 79 //名字关键字 80 strWhere = GetWhere(); 81 if (strWhere == "-1") 82 { 83 strWhere = ""; 84 txtName.Focus(); 85 return; 86 } 87 QueryData(sql + strWhere); 88 } 89 90 private void checkBox5_CheckedChanged(object sender, EventArgs e) 91 { 92 if (this.cbxDate.Checked == true) 93 { 94 dateBegin.Enabled = true; 95 dateEnd.Enabled = true; 96 } 97 else 98 { 99 dateBegin.Enabled = false; 100 dateEnd.Enabled = false; 101 } 102 } 103 104 //绑定数据至bindingnavigator1 105 private void BindData() 106 { 107 this.dataGridView1.Columns[0].DataPropertyName = dtList.Columns["id"].ToString(); 108 this.dataGridView1.Columns[1].DataPropertyName = dtList.Columns["name"].ToString(); 109 this.dataGridView1.Columns[2].DataPropertyName = dtList.Columns["category"].ToString(); 110 this.dataGridView1.Columns[3].DataPropertyName = dtList.Columns["regioncode"].ToString(); 111 this.dataGridView1.Columns[4].DataPropertyName = dtList.Columns["regionname"].ToString(); 112 this.dataGridView1.Columns[5].DataPropertyName = dtList.Columns["isflow"].ToString(); 113 this.dataGridView1.Columns[6].DataPropertyName = dtList.Columns["key1"].ToString(); 114 115 //客户ID 116 this.txtId2.DataBindings.Add("Text", bs, "id"); 117 //客户名称 118 this.txtName2.DataBindings.Add("Text", bs, "name"); 119 //机构代码证号 120 this.txtRegisteredNumber.DataBindings.Add("Text", bs, "registerednumber"); 121 //地址 122 this.txtAddress.DataBindings.Add("Text", bs, "address"); 123 //地区号 124 this.txtRegionCode.DataBindings.Add("Text", bs, "regioncode"); 125 this.txtRegionName.DataBindings.Add("Text", bs, "regionname"); 126 ; //创建时间 127 this.txtCreateTime.DataBindings.Add("Text", bs, "createtime"); 128 this.txtCreateBy.DataBindings.Add("Text", bs, "createby"); 129 this.txtModifyTime.DataBindings.Add("Text", bs, "modifytime"); 130 this.txtModifyBy.DataBindings.Add("Text", bs, "modifyby"); 131 this.txtAlia1.DataBindings.Add("Text", bs, "alias1"); 132 this.txtAlia2.DataBindings.Add("Text", bs, "alias2"); 133 this.txtKey1.DataBindings.Add("Text", bs, "key1"); 134 this.txtKey2.DataBindings.Add("Text", bs, "key2"); 135 this.txtKey3.DataBindings.Add("Text", bs, "key3"); 136 this.txtKey0.DataBindings.Add("Text", bs, "key0"); 137 this.cbxCategory2.DataBindings.Add("SelectedValue", bs, "category"); 138 this.cbxLevel1.DataBindings.Add("SelectedValue", bs, "level1"); 139 this.cbxLevel2.DataBindings.Add("SelectedValue", bs, "level2"); 140 this.cbxType1.DataBindings.Add("SelectedValue", bs, "type1"); 141 //是否发流向 142 this.lbIsflow.DataBindings.Add("Text", bs, "isflow"); 143 //是否连锁 144 this.lbIsChain.DataBindings.Add("Text", bs, "ischain"); 145 //是否协议客户 146 this.lbIsAgreement.DataBindings.Add("Text", bs, "isagreement"); 147 //(string)((DictionaryEntry)cbxCategory.SelectedItem).Key 148 } 149 //查询数据 150 private void QueryData(string sql) 151 { 152 dtList = DBHelperPg.ExecuteDataTable(sql); 153 bs.DataSource = dtList; 154 bindingNavigator1.BindingSource = bs; 155 dataGridView1.DataSource = bs; //把数据源绑定在dataGridView1上 156 //绑定每列的值显示在DatagridView 157 } 158 159 private void btnAdd_Click(object sender, EventArgs e) 160 { 161 162 } 163 164 private void btnSave_Click(object sender, EventArgs e) 165 { 166 if (addFlag == 1) 167 { 168 txtCreateBy.Text = GlobalParam.UserName; 169 txtCreateTime.Text = DateTime.Now.ToString(); 170 } 171 else 172 { 173 //接受修改 174 txtModifyBy.Text = GlobalParam.UserName; 175 txtModifyTime.Text = DateTime.Now.ToString(); 176 } 177 bs.EndEdit(); 178 DBHelperPg.UpdateDataTable(dtList, "client", strColumns); 179 MessageBox.Show("保存成功!"); 180 EditBoxState(false); 181 addFlag = 0; 182 //将保存按钮改为 不可用 183 this.btnSave.Enabled = false; 184 this.btnEdit.Enabled = true; 185 } 186 //取查询条件 187 private string GetWhere() 188 { 189 string clientChar; 190 string strWhere = ""; 191 //名字关键字 192 clientChar = txtName.Text.Trim(); 193 if (!(string.IsNullOrEmpty(clientChar)) && clientChar.Length > 0) 194 { 195 strWhere = " and name like '%" + clientChar + "%'"; 196 } 197 198 //判断客户编号 199 if (string.IsNullOrEmpty(txtId.Text.Trim())) 200 { 201 txtId.Text = "0"; 202 } 203 int clientId = Convert.ToInt32(txtId.Text.Trim()); 204 if (clientId != 0) 205 { 206 strWhere += " and id=" + txtId.Text.Trim(); 207 } 208 //判断客户大类,读取 key 和 value 209 string clientCategory = (string)((DictionaryEntry)cbxCategory.SelectedItem).Key; 210 if (clientCategory != "0") 211 { 212 strWhere += " and category ='" + clientCategory + "' "; 213 } 214 215 //判断地区编号 216 if (!string.IsNullOrEmpty(txtRegion.Text.Trim())) 217 { 218 string strRegionCode = txtRegion.Text.Trim(); 219 if (strRegionCode.Substring(4, 2) == "00") 220 221 { 222 //如果选的是上级,则用like 模糊查询 223 strRegionCode = strRegionCode.Replace("00", "%"); 224 strWhere += " and regioncode like '" + strRegionCode + "' "; 225 } 226 else 227 { 228 strWhere += " and regioncode='" + txtRegion.Text.Trim() + "' "; 229 } 230 231 } 232 //判断部门 233 if (!string.IsNullOrEmpty(txtDepart.Text.Trim())) 234 { 235 strWhere += " and (depart1='" + txtDepart.Text.Trim() + "' "; 236 strWhere += " or depart2 ='" + txtDepart.Text.Trim() + "') "; 237 238 } 239 if (cbxDate.Checked == true) 240 { 241 strWhere += " and createtime>='" + dateBegin.Text + "' "; 242 strWhere += " and createtime<='" + dateEnd.Text + "' "; 243 } 244 MessageBox.Show(strWhere); 245 return strWhere + " limit 200"; 246 } 247 //表单 load 事件 248 private void FrmClient_Load(object sender, EventArgs e) 249 { 250 //默认取前20条记录 251 // BindData("select id,name, category,level1,regioncode,address,key1,key2,key3,key0,isflow,ischain,registerednumber from client limit 10 "); 252 QueryData("select " + strColumns + " from client limit 20 "); 253 BindData(); 254 255 BindCategory2(); 256 //将详情页的文本框下拉框等设置为不能修改 257 EditBoxState(false); 258 BindLevel1(); 259 this.btnSave.Enabled = false; 260 261 } 262 263 //编辑按钮 264 private void btnEdit_Click(object sender, EventArgs e) 265 { 266 if (addFlag == 1) 267 { 268 MessageBox.Show("正在添加数据,请保存再使用;"); 269 } 270 271 EditBoxState(true); 272 this.btnEdit.Enabled = false; 273 this.btnSave.Enabled = true; 274 //清空文本框 275 //ClearBox(); 276 } 277 278 //绑定客户类型 2 Category2 279 private void BindCategory2() 280 { 281 DataTable dt = new DataTable(); 282 DataColumn dc1 = new DataColumn("id"); 283 DataColumn dc2 = new DataColumn("name"); 284 dt.Columns.Add(dc1); 285 dt.Columns.Add(dc2); 286 287 DataRow dr1 = dt.NewRow(); 288 dr1["id"] = 1; 289 dr1["name"] = "商业"; 290 291 DataRow dr2 = dt.NewRow(); 292 dr2["id"] = 2; 293 dr2["name"] = "医院"; 294 295 DataRow dr3 = dt.NewRow(); 296 dr3["id"] = 3; 297 dr3["name"] = "门店"; 298 DataRow dr4 = dt.NewRow(); 299 dr4["id"] = 6; 300 dr4["name"] = "连锁总部"; 301 dt.Rows.Add(dr1); 302 dt.Rows.Add(dr2); 303 dt.Rows.Add(dr3); 304 dt.Rows.Add(dr4); 305 306 cbxCategory2.DataSource = dt; 307 cbxCategory2.ValueMember = "id"; 308 cbxCategory2.DisplayMember = "name"; 309 } 310 //页签切换事件,用来更改商业 ,医院,门店等 不同的分类LABELA名字 311 private void tcClient_SelectedIndexChanged(object sender, EventArgs e) 312 { 313 switch (this.tcClient.SelectedIndex) 314 { 315 case 0: 316 break; 317 case 1: 318 switch (cbxCategory2.Text) 319 { 320 case "商业": 321 break; 322 case "医院": 323 break; 324 } 325 break; 326 327 } 328 //因为数据库设计的是0/1,而checkbox要求是true/false,暂未好的办法 329 if (lbIsflow.Text == "1") //是否发流向 330 { 331 chkIsflow.Checked = true; 332 } 333 else 334 { 335 chkIsflow.Checked = false; 336 } 337 if (lbIsChain.Text == "1") //是否连锁 338 { 339 chkIsChain.Checked = true; 340 } 341 else 342 { 343 chkIsChain.Checked = false; 344 } 345 if (lbIsAgreement.Text == "1") //是否协议 346 { 347 chkIsAgreement.Checked = true; 348 } 349 else 350 { 351 chkIsAgreement.Checked = false; 352 } 353 354 } 355 356 private void chkIsflow_CheckedChanged(object sender, EventArgs e) 357 { 358 if (chkIsflow.Checked == true) //是否发流向 359 { 360 lbIsflow.Text = "1"; 361 } 362 else 363 { 364 lbIsflow.Text = "0"; 365 } 366 } 367 368 private void chkIsChain_CheckedChanged(object sender, EventArgs e) 369 { 370 if (chkIsChain.Checked == true) //是否连锁 371 { 372 lbIsChain.Text = "1"; 373 } 374 else 375 { 376 lbIsChain.Text = "0"; 377 } 378 } 379 380 private void chkIsAgreement_CheckedChanged(object sender, EventArgs e) 381 { 382 if (chkIsAgreement.Checked == true) //是否协议客户 383 { 384 lbIsAgreement.Text = "1"; 385 } 386 else 387 { 388 lbIsAgreement.Text = "0"; 389 } 390 } 391 392 //获取指定容器控件,控制字段编辑或修改 393 private void ClearBox() 394 { 395 396 foreach (Control c in this.groupBox1.Controls) 397 { 398 if (c.GetType().ToString().Contains("TextBox")) 399 { 400 ((TextBox)c).Text = ""; 401 MessageBox.Show(c.GetType().ToString()); 402 } 403 if (c.GetType().ToString().Contains("ComboBox")) 404 { 405 ((ComboBox)c).Text = ""; 406 } 407 if (c.GetType().ToString().Contains("CheckBox")) 408 { 409 ((CheckBox)c).Checked = false; 410 } 411 } 412 } 413 //导航栏的删除事件,重写;首先要在bindingNavigator属性中,删除DeleteItem方法 414 private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e) 415 { 416 if (MessageBox.Show("确认删除吗?", "删除前确认", MessageBoxButtons.OKCancel) == DialogResult.OK) 417 { 418 dtList.Rows[bindingNavigator1.BindingSource.Position].Delete(); 419 } 420 } 421 422 private void btnDelete_Click(object sender, EventArgs e) 423 { 424 425 } 426 //添加按钮 427 private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e) 428 { 429 addFlag = 1; 430 //将所有文本框置为可以修改 431 EditBoxState(true); 432 this.btnSave.Enabled = true; 433 this.cbxLevel1.Enabled = false; 434 this.cbxLevel2.Enabled = false; 435 this.cbxType1.Enabled = true; 436 this.cbxType2.Enabled = true; 437 438 } 439 //更改文本框,下拉框,复选框的Enableed状态; 440 private void EditBoxState(bool bl) 441 { 442 foreach (Control c in this.groupBox1.Controls) 443 { 444 if (c.GetType().ToString().Contains("TextBox")) 445 { 446 ((TextBox)c).Enabled = bl; 447 } 448 if (c.GetType().ToString().Contains("ComboBox")) 449 { 450 ((ComboBox)c).Enabled = bl; 451 } 452 if (c.GetType().ToString().Contains("CheckBox")) 453 { 454 ((CheckBox)c).Enabled = bl; 455 } 456 } 457 } 458 459 460 //地区选择 461 private void txtRegionCode_DoubleClick(object sender, EventArgs e) 462 { 463 FrmSelectRegion frmSelectRegion = new FrmSelectRegion(); 464 if (frmSelectRegion.ShowDialog() == DialogResult.OK) 465 { 466 txtRegionCode.Text = frmSelectRegion.regionId; 467 txtRegionName.Text = frmSelectRegion.regionName; 468 frmSelectRegion.Close(); 469 } 470 } 471 472 473 474 475 476 private void textBox4_DoubleClick(object sender, EventArgs e) 477 { 478 FrmSelectDepart frmSelectDepart = new FrmSelectDepart(); 479 if (frmSelectDepart.ShowDialog() == DialogResult.OK) 480 { 481 txtDepart1.Text = frmSelectDepart.departId; 482 lbDepart1Name.Text = frmSelectDepart.departName; 483 frmSelectDepart.Close(); 484 } 485 } 486 //临床部选择 487 private void txtDepart2_DoubleClick(object sender, EventArgs e) 488 { 489 FrmSelectDepart frmSelectDepart = new FrmSelectDepart(); 490 if (frmSelectDepart.ShowDialog() == DialogResult.OK) 491 { 492 txtDepart2.Text = frmSelectDepart.departId; 493 lbDepart2Name.Text = frmSelectDepart.departName; 494 frmSelectDepart.Close(); 495 } 496 497 } 498 499 private void txtRegion_DoubleClick(object sender, EventArgs e) 500 { 501 FrmSelectRegion frmSelectRegion = new FrmSelectRegion(); 502 if (frmSelectRegion.ShowDialog() == DialogResult.OK) 503 { 504 txtRegion.Text = frmSelectRegion.regionId; 505 frmSelectRegion.Close(); 506 } 507 } 508 509 private void txtDepart_TextChanged(object sender, EventArgs e) 510 { 511 512 } 513 514 private void txtDepart_DoubleClick(object sender, EventArgs e) 515 { 516 FrmSelectDepart frmSelectDepart = new FrmSelectDepart(); 517 if (frmSelectDepart.ShowDialog() == DialogResult.OK) 518 { 519 txtDepart.Text = frmSelectDepart.departId; 520 frmSelectDepart.Close(); 521 } 522 } 523 private void BindLevel1() 524 { 525 //根据客户大类的不同,而联动改变 526 527 string sql = " select code,name,parentid from client_category "; 528 dtLevel1 = DBHelperPg.ExecuteDataTable(sql); 529 dtLevel2 = dtLevel1; 530 531 cbxLevel1.DataSource = dtLevel1; 532 cbxLevel1.DisplayMember = "name"; 533 cbxLevel1.ValueMember = "code"; 534 535 536 cbxLevel2.DataSource = dtLevel1; 537 cbxLevel2.DisplayMember = "name"; 538 cbxLevel2.ValueMember = "code"; 539 } 540 541 private void cbxCategory2_Click(object sender, EventArgs e) 542 { 543 //保存原始值,用来判断后面的下拉框 用不用更新 544 oriCategory2 = cbxCategory2.Text; 545 } 546 547 private void btnStop_Click(object sender, EventArgs e) 548 { 549 550 } 551 552 private void cbxCategory2_SelectedIndexChanged(object sender, EventArgs e) 553 { 554 } 555 556 private void cbxLevel1_SelectedIndexChanged(object sender, EventArgs e) 557 { 558 } 559 560 private void cbxLevel1_Click(object sender, EventArgs e) 561 { 562 oriLevel1 = cbxLevel1.Text; 563 } 564 565 private void cbxLevel1_Leave(object sender, EventArgs e) 566 { 567 568 } 569 570 private void cbxLevel1_SelectionChangeCommitted(object sender, EventArgs e) 571 { 572 string parentId; 573 try 574 { 575 DataRowView drv = (DataRowView)cbxLevel1.SelectedItem; 576 parentId = drv.Row["code"].ToString(); 577 //数据库结构错误,只能多用SQL来弥补 :) :( ,此处因为ID与CODE不分,徘徊良久,是为记; 578 //parentId 其实取的是CODE值; 579 if (parentId == "System.Data.DataRowView") 580 { 581 return; 582 } 583 584 string sql = "select id from client_category where code='" + parentId + "'"; 585 parentId = DBHelperPg.ExecuteScalar(sql).ToString(); 586 string strFilter = "parentid = " + parentId + " "; 587 DataView dv2 = new DataView(dtLevel1); 588 dv2.RowFilter = strFilter; 589 cbxLevel2.DataSource = dv2; 590 cbxLevel2.DisplayMember = "name"; 591 cbxLevel2.ValueMember = "code"; 592 cbxLevel2.Enabled = true; 593 cbxLevel2.Text = ""; 594 } 595 catch 596 { 597 598 } 599 } 600 601 private void cbxCategory2_SelectionChangeCommitted(object sender, EventArgs e) 602 { 603 //根据客户大类的不同,而联动改变 604 string parentId; 605 try 606 { 607 //parentId = cbxCategory2.SelectedValue.ToString(); 608 DataRowView drv = (DataRowView)cbxCategory2.SelectedItem; 609 parentId = drv.Row["id"].ToString(); 610 } 611 catch 612 { 613 //新建时出错,无法处理 614 parentId = "0"; 615 } 616 string strFilter = "parentid = " + parentId; 617 DataView dv = new DataView(dtLevel1); 618 dv.RowFilter = strFilter; 619 cbxLevel1.DataSource = dv; 620 621 cbxLevel1.Enabled = true; 622 cbxLevel1.Text = ""; 623 cbxLevel2.Text = ""; 624 cbxLevel1.Focus(); 625 cbxLevel1.Refresh(); 626 } 627 628 //主类型下拉框,鼠标离开事件 629 private void cbxCategory2_Leave(object sender, EventArgs e) 630 { 631 632 } 633 634 private void cbxCategory2_Enter(object sender, EventArgs e) 635 { 636 oriCategory2 = this.cbxCategory2.Text; 637 } 638 639 //分类1变动事件 640 private void cbxLevel1_SelectedValueChanged(object sender, EventArgs e) 641 { 642 643 644 } 645 646 647 648 //表格自动编号 649 private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) 650 { 651 //自动编号,与数据无关 652 Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, 653 e.RowBounds.Location.Y, 654 dataGridView1.RowHeadersWidth - 4, 655 e.RowBounds.Height); 656 TextRenderer.DrawText(e.Graphics, 657 (e.RowIndex + 1).ToString(), 658 dataGridView1.RowHeadersDefaultCellStyle.Font, 659 rectangle, 660 dataGridView1.RowHeadersDefaultCellStyle.ForeColor, 661 TextFormatFlags.VerticalCenter | TextFormatFlags.Right); 662 } 663 664 private void cbxCategory2_SelectedValueChanged(object sender, EventArgs e) 665 { 666 } 667 668 669 670 671 672 } 673 }
活到老,学到老。