本文實(shí)例展示了DevExpress根據(jù)條件設(shè)置GridControl RepositoryItem是否可編輯的方法。
一般在C#項(xiàng)目的開發(fā)中,并不是每個(gè)RepositoryItem都可以編輯,往往是有條件性的,需要譬如當(dāng)A列等于“AA”的時(shí)候,B列才可編輯,實(shí)現(xiàn)起來在ShowingEditor事件中最為方便,并且加入toolTip提示顯得人性化。
主要功能代碼如下:
private void gvLampConfig_ShowingEditor(object sender, System.ComponentModel.CancelEventArgs e){ GridView _view = sender as GridView; if (_view.FocusedColumn.Name == "colSavePowerGp1")//當(dāng)列等于colSavePowerGp1 { string _type = _view.GetRowCellDisplayText(gvLampConfig.FocusedRowHandle, "OptStatusText_gp1"); if (!_type.Equals("節(jié)能"))//當(dāng)列OptStatusText_gp1的列值不等于OptStatusText_gp1 { e.Cancel = true; ShowToolTip(toolTipController, "提示", "當(dāng)是【調(diào)光狀態(tài)】是節(jié)能模式情況,可以設(shè)置該值!"); } }}public static void ShowToolTip(ToolTipController toolTip, string title, string content){ Point _mousePoint = Control.MousePosition; toolTip.ShowHint(content, title, _mousePoint);}代碼運(yùn)行效果如下:

為了調(diào)高代碼復(fù)用性,方便后續(xù)使用,可以這樣子封裝一下:
/// <summary>/// 設(shè)置RepositoryItem是否可編輯/// 說明:/// 在ShowingEditor事件中使用/// </summary>/// <param name="view">GridView</param>/// <param name="focusedColumnName">需要設(shè)置的列名稱</param>/// <param name="conditonHanlder">判斷委托</param>/// <param name="toolTip">ToolTipController</param>/// <param name="title">當(dāng)條件委托成立的時(shí)候提示標(biāo)題</param>/// <param name="content">當(dāng)條件委托成立的時(shí)候提示內(nèi)容</param>/// <param name="e">CancelEventArgs</param>private void CustomShowingEditorWithToolTip(GridView view, string focusedColumnName, Func<object, bool> conditonHanlder, ToolTipController toolTip, string title, string content, CancelEventArgs e){ if (view.FocusedColumn.Name.Equals(focusedColumnName)) { if (conditonHanlder(view.GetFocusedRow())) { e.Cancel = true; Point _mousePoint = Control.MousePosition; toolTip.ShowHint(content, title, _mousePoint); } }}代碼使用如下:
private void gvLampConfig_ShowingEditor(object sender, System.ComponentModel.CancelEventArgs e){ GridView _view = sender as GridView; CustomShowingEditorWithToolTip(_view, "colSavePowerGp1", arg => ((LampSelfRunCfgParamter)arg).OptStatusText_gp1 != "節(jié)能", toolTipController, "提示", "當(dāng)是【調(diào)光狀態(tài)】是節(jié)能模式情況,可以設(shè)置該值!", e);}希望本文所示代碼能對(duì)大家有所幫助!
新聞熱點(diǎn)
疑難解答
圖片精選