房租管理小软件(五):控件的使用
1.TextEdit的操作,TextEdit一般设置 Property.Mask 可以控制输入的格式
decimal : 一般用 f2
2.gridlookupEdit,让 数据直接绑定到控件上面,而且有搜索功能
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #region 费用代码 2 this.gridLookUpEdit_FeeCode.Properties.DataSource = from t in dataContext.T1_SY_FeeCode 3 select new 4 { 5 t.AutoID, 6 t.NameCN, 7 PY = dataContext.fun_getPY(t.NameCN) 8 }; 9 this.gridLookUpEdit_FeeCode.Properties.DisplayMember = "NameCN"; 10 this.gridLookUpEdit_FeeCode.Properties.ValueMember = "AutoID"; 11 this.gridLookUpEdit_FeeCode.Properties.View.OptionsView.ShowColumnHeaders = false; 12 this.gridLookUpEdit_FeeCode.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard; 13 this.gridLookUpEdit_FeeCode.Properties.ImmediatePopup = true; 14 this.gridLookUpEdit_FeeCode.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True; 15 16 DevExpress.XtraGrid.Columns.GridColumn T1_SY_FeeCode_AutoID = new DevExpress.XtraGrid.Columns.GridColumn(); 17 T1_SY_FeeCode_AutoID.Caption = "AutoID"; 18 T1_SY_FeeCode_AutoID.FieldName = "AutoID"; 19 T1_SY_FeeCode_AutoID.Name = "T1_SY_FeeCode_AutoID"; 20 T1_SY_FeeCode_AutoID.Visible = true; 21 T1_SY_FeeCode_AutoID.VisibleIndex = 0; 22 T1_SY_FeeCode_AutoID.Width = 122; 23 24 DevExpress.XtraGrid.Columns.GridColumn T1_SY_FeeCode_NameCN = new DevExpress.XtraGrid.Columns.GridColumn(); 25 T1_SY_FeeCode_NameCN.Caption = "NameCN"; 26 T1_SY_FeeCode_NameCN.FieldName = "NameCN"; 27 T1_SY_FeeCode_NameCN.Name = "T1_SY_FeeCode_NameCN"; 28 T1_SY_FeeCode_NameCN.Visible = true; 29 T1_SY_FeeCode_NameCN.VisibleIndex = 0; 30 T1_SY_FeeCode_NameCN.Width = 122; 31 32 33 DevExpress.XtraGrid.Columns.GridColumn T1_SY_FeeCode_PY = new DevExpress.XtraGrid.Columns.GridColumn(); 34 T1_SY_FeeCode_PY .Caption = "PY"; 35 T1_SY_FeeCode_PY .FieldName = "PY"; 36 T1_SY_FeeCode_PY .Name = "T1_SY_FeeCode_PY"; 37 T1_SY_FeeCode_PY .Visible = false; 38 T1_SY_FeeCode_PY .VisibleIndex = 0; 39 T1_SY_FeeCode_PY .Width = 122; 40 41 42 43 this.gridLookUpEdit_FeeCode.Properties.View.Columns.Add(T1_SY_FeeCode_AutoID); 44 this.gridLookUpEdit_FeeCode.Properties.View.Columns.Add(T1_SY_FeeCode_PY); 45 this.gridLookUpEdit_FeeCode.Properties.View.Columns.Add(T1_SY_FeeCode_NameCN); 46 T1_SY_FeeCode_PY.Visible = false; 47 48 this.gridLookUpEdit_FeeCode.EditValueChanging += new DevExpress.XtraEditors.Controls.ChangingEventHandler(delegate(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e) 49 { 50 this.BeginInvoke(new System.Windows.Forms.MethodInvoker(delegate 51 { 52 53 GridLookUpEdit edit = sender as GridLookUpEdit; 54 GridView gridView = edit.Properties.View as GridView; 55 FieldInfo fi = gridView.GetType().GetField("extraFilter", BindingFlags.NonPublic | BindingFlags.Instance); 56 CriteriaOperator[] ca = new CriteriaOperator[gridView.Columns.Count]; 57 58 for (int i = 0; i < gridView.Columns.Count; i++) 59 { 60 BinaryOperator op1 = new BinaryOperator(gridView.Columns[i].FieldName, edit.Text + "%", BinaryOperatorType.Like); 61 ca[i] = op1; 62 } 63 64 string filterCondition = new GroupOperator(GroupOperatorType.Or, ca).ToString(); 65 fi.SetValue(gridView, filterCondition); 66 MethodInfo mi = gridView.GetType().GetMethod("ApplyColumnsFilterEx", BindingFlags.NonPublic | BindingFlags.Instance); 67 mi.Invoke(gridView, null); 68 69 })); 70 } 71 ); 72 this.gridLookUpEdit_FeeCode.KeyDown += new System.Windows.Forms.KeyEventHandler(delegate(object sender, KeyEventArgs e) 73 { 74 GridLookUpEdit edit = sender as GridLookUpEdit; 75 if (e.KeyData.ToString() == "Return" && edit.IsPopupOpen == false && edit.EditValue != "" && edit.EditValue != null) 76 { 77 SendKeys.Send("{Tab}"); 78 } 79 }); 80 81 this.gridLookUpEdit_FeeCode.Properties.ButtonClick += new DevExpress.XtraEditors.Controls.ButtonPressedEventHandler(delegate(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e) 82 { 83 if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Combo) { } 84 85 if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Delete) { 86 (sender as GridLookUpEdit).Focus(); SendKeys.Send("^{DEL}"); } 87 88 }); 89 #endregion
3.在gridControl 中使用内置控件显示(RepositoryItemLookUpEdit)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #region 人员 2 DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit repositoryItemLookUpEdit_User = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit(); 3 this.colT2_JZ_CarryOverStock.ColumnEdit = repositoryItemLookUpEdit_User; 4 repositoryItemLookUpEdit_User.AutoHeight = false; 5 repositoryItemLookUpEdit_User.Name = "repositoryItemLookUpEdit_User"; 6 repositoryItemLookUpEdit_User.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("UserID")); 7 repositoryItemLookUpEdit_User.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("UserName")); 8 repositoryItemLookUpEdit_User.DataSource = from t in dataContext.T1_QX_MySystemUser 9 select new 10 { 11 t.UserID, 12 t.UserName 13 }; 14 repositoryItemLookUpEdit_User.DisplayMember = "UserName"; 15 repositoryItemLookUpEdit_User.ValueMember = "UserID"; 16 repositoryItemLookUpEdit_User.ShowHeader = false; 17 repositoryItemLookUpEdit_User.NullText = ""; 18 #endregion
4.gridcontrol 中为0 则不显示:
this.gridColumn_Amount.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
this.gridColumn_Amount.DisplayFormat.FormatString = "{0:#.00;-#.00;''}";
5.gridcontol 中分行的状态进行控制颜色,选中状态等
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 private void gridView1_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e) 2 { 3 4 try 5 { 6 if (e.Column.FieldName != "Check") return; 7 8 if (e.RowHandle > -1) 9 { 10 Object os = this.dt_ZW.Rows[e.RowHandle]["Status"]; 11 if (os != null) 12 { 13 string s_Status = os.ToString();//状态 14 if (s_Status == "已结" || s_Status == "冲抵") { e.RepositoryItem = repositoryItemCheckEdit2; }//不能选中 15 else 16 { 17 e.RepositoryItem = repositoryItemCheckEdit1; 18 } 19 } 20 } 21 } 22 catch (Exception ex) { } 23 } 24 25 26 27 private void gridView1_RowCellStyle(object sender, RowCellStyleEventArgs e) 28 { 29 GridView view = sender as GridView; 30 Object os = view.GetRowCellDisplayText(e.RowHandle, this.gridColumn_Status); 31 if (os != null) 32 { 33 string s_Status = os.ToString();//状态 34 if (s_Status == "已结") { e.Appearance.ForeColor = SystemColors.GrayText; } 35 if (s_Status == "冲抵") { e.Appearance.ForeColor = Color.Red; } 36 } 37 38 Object FAmount = view.GetRowCellDisplayText(e.RowHandle, this.gridColumn_FAmount); 39 if (FAmount != null && FAmount.ToString() != "") 40 { 41 42 decimal s_FAmount = decimal.Parse(FAmount.ToString());//状态 43 if (s_FAmount < 0 && e.Column.FieldName == "FAmount") { e.Appearance.ForeColor = Color.Red; } 44 45 } 46 47 Object Amount = view.GetRowCellDisplayText(e.RowHandle, this.gridColumn_Amount); 48 if (Amount != null && Amount.ToString() != "") 49 { 50 decimal s_Amount = decimal.Parse(Amount.ToString());//状态 51 if (s_Amount < 0 && e.Column.FieldName == "Amount") { e.Appearance.ForeColor = Color.Red; } 52 53 } 54 55 } 56 57 58 private void gridView1_ShowingEditor(object sender, CancelEventArgs e) 59 { 60 GridView view = sender as GridView; 61 62 string s_Status = view.GetFocusedRowCellDisplayText(this.gridColumn_Status);//状态 63 64 65 if (s_Status == "已结") { e.Cancel = true; } 66 if (s_Status == "冲抵") { e.Cancel = true; } 67 if (view.FocusedColumn.FieldName != "Check") { e.Cancel = true; }//只有check可编辑 68 }