private void NewOrShow() { for (int j = 0; j < districts.Count; j++) { CLB_Districts.Items.Add(districts[j].districtName); } for (int i = 0; i < DT_UserInfo.Rows.Count; i++) { if (DT_UserInfo.Rows[i]["RealName"].ToString().Trim() == Cur_UsrName) { //分过就查询 if (DT_UserInfo.Rows[i]["DistrictName"].ToString().Trim() != "") { //获取districtName,分成数组,一个一个查询index,然后把clb里的选选个画勾 string[] SelectedDistricts = DT_UserInfo.Rows[i]["DistrictName"].ToString().Trim().Split(','); for (int k = 0; k < SelectedDistricts.Count(); k++) { for (int l = 0; l < CLB_Districts.Items.Count; l++) { if (SelectedDistricts[k] == CLB_Districts.Items[l].ToString().Trim()) { CLB_Districts.SetItemChecked(l, true); } } } } } } }
//根据当前选择的用户设置区域是否被选中 private void setClbStatus(string Account) { //全部设为未选 for (int j = 0; j < CLB_Districts.Items.Count; j++) { CLB_Districts.SetItemChecked(j, false); } ; var tempva = (userInfoDatas.Where(x => x.Account == Account).Select(x => x.DistrictName).FirstOrDefault()); if (tempva == null) { } else { string[] usrDistrict = tempva.Split(','); for (int i = 0; i < usrDistrict.Count(); i++) { for (int j = 0; j < CLB_Districts.Items.Count; j++) { if (CLB_Districts.Items[j].ToString() == usrDistrict[i]) { CLB_Districts.SetItemChecked(j, true); break; } } } } }
private void saveUserInfo() { for (int i = 0; i < DT_UserInfo.Rows.Count; i++) { if (DT_UserInfo.Rows[i]["RealName"].ToString().Trim() == Cur_UsrName) { DT_UserInfo.Rows[i]["DistrictCode"] = DT_UserInfo.Rows[i]["Type"]; DT_UserInfo.Rows[i]["DistrictName"] = null; for (int j = 0; j < CLB_Districts.Items.Count; j++) { if (CLB_Districts.GetItemChecked(j)) { DT_UserInfo.Rows[i]["DistrictCode"] += districts[j].districtCode + ","; DT_UserInfo.Rows[i]["DistrictName"] += districts[j].districtName + ","; } } if (DT_UserInfo.Rows[i]["DistrictName"].ToString().Trim().Length > 0) { DT_UserInfo.Rows[i]["DistrictCode"] = DT_UserInfo.Rows[i]["DistrictCode"].ToString().Trim().Substring(1, DT_UserInfo.Rows[i]["DistrictCode"].ToString().Trim().Length - 2); DT_UserInfo.Rows[i]["DistrictName"] = DT_UserInfo.Rows[i]["DistrictName"].ToString().Trim().Substring(0, DT_UserInfo.Rows[i]["DistrictName"].ToString().Trim().Length - 1); } break; } } }
private void CLB_Districts_MouseClick(object sender, MouseEventArgs e) { int index = this.CLB_Districts.IndexFromPoint(e.Location); if (index != CheckedListBox.NoMatches) { CLB_Districts.SetItemChecked(index, !CLB_Districts.GetItemChecked(index)); } else { CLB_Districts.SelectedIndex = -1;//不做任何操作,将ListBox的选中项取消 } }
//保存当前更改项 private void saveUsrChanges() { var user = userInfoDatas.Where(x => x.Account == CurAccount).FirstOrDefault(); userInfoDatas.Remove(user); user.DistrictCode = ""; user.DistrictName = ""; for (int i = 0; i < CLB_Districts.Items.Count; i++) { if (CLB_Districts.GetItemChecked(i)) { var temp = districtInfoDatas.Where(x => x.DistrictName == CLB_Districts.Items[i].ToString()).FirstOrDefault(); user.DistrictCode += temp.DistrictCode + ","; user.DistrictName += temp.DistrictName + ","; } } if (user.DistrictCode.Length > 0) { user.DistrictCode = user.DistrictCode.Substring(0, user.DistrictCode.Length - 1); user.DistrictName = user.DistrictName.Substring(0, user.DistrictName.Length - 1); } userInfoDatas.Add(user); }