dev GridControl 代码自定义下拉框

 

前提需要先把对应列绑定一个值

List<PubType> settleTypeList = null;
IPSettleService ipSettleService = new IPSettleService();
settleTypeList = ipSettleService.GetSettleTypeList();
PrePayUseGrid.AddLookUpEditResposibility<PubType>("RefundType", settleTypeList, "Name", "KeyFlag", true);

 

public void AddLookUpEditResposibility<T>(string columnName, List<T> items, string displayMember, string valueMember, bool onlyShowDisplayColumnInDropDownWindow = false)
{
RepositoryItemLookUpEdit resp = new RepositoryItemLookUpEdit();
resp.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
resp.NullText = string.Empty;

if (!string.IsNullOrWhiteSpace(displayMember))
{
resp.DisplayMember = displayMember;
}

if (!string.IsNullOrWhiteSpace(valueMember))
{
resp.ValueMember = valueMember;
}

resp.DataSource = items;

if (onlyShowDisplayColumnInDropDownWindow)
{
resp.PopulateColumns();
foreach (LookUpColumnInfo colInfo in resp.Columns)
{
if (colInfo.FieldName.Equals(displayMember))
{
colInfo.Visible = true;
}
else
{
colInfo.Visible = false;
}
}
}


foreach (GridColumn col in this.gridView.Columns)
{
if (col.FieldName.Equals(columnName))
{
col.ColumnEdit = resp;
}
}
}

=================================================================================================

private void Frm_Load(object sender, EventArgs e)
{
PrePayUseGrid.CellValueChangedEvent += PrePayUseGrid_CellValueChangedEvent;
PrePayUseGrid.CustomRowCellEditEvent += PrePayUseGrid_CustomRowCellEditEvent;
}

一、测试成功

private void PrePayUseGrid_CustomRowCellEditEvent(object sender, CustomRowCellEditEventArgs e)
{
if (e.Column.FieldName != "RefundType")
return;
DevExpress.XtraGrid.Views.Grid.GridView gv = sender as DevExpress.XtraGrid.Views.Grid.GridView;

DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit repositoryItem = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
e.RepositoryItem = repositoryItem;
var view = PrePayUseGrid.GetFocusedBindingInfo<PatUsablePrepayView>();
PatUsablePrepay model = view.GetInnerData() as PatUsablePrepay;
repositoryItem.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
repositoryItem.NullText = string.Empty;
repositoryItem.DisplayMember = "Name";
repositoryItem.ValueMember = "KeyFlag";
repositoryItem.DataSource =manager.SetPreRefundType(model);

repositoryItem.PopulateColumns();
foreach (LookUpColumnInfo colInfo in repositoryItem.Columns)
{
if (colInfo.FieldName.Equals("Name"))
{
colInfo.Visible = true;
}
else
{
colInfo.Visible = false;
}
}
//foreach (GridColumn col in gv.Columns)
//{
// if (col.FieldName.Equals("RefundType"))
// {
// col.ColumnEdit = repositoryItem;
// }
//}
//gv.Columns["RefundType"].FieldName = "KeyFlag";

repositoryItem.BestFitMode = DevExpress.XtraEditors.Controls.BestFitMode.BestFitResizePopup;
//PrePayUseGrid.gridView.Columns["RefundType"].FieldName = "RefundType";
//从列表中选择项后,绑定一个选值改变事件,以便于把列表中的其他列的值赋与gridView2中需要的列
//repositoryItem.EditValueChanged += (m, n) =>
//{
// var materiaObject = (m as DevExpress.XtraEditors.LookUpEdit).GetSelectedDataRow();
// PrePayUseGrid.gridView.SetFocusedRowCellValue(PrePayUseGrid.gridView.Columns["RefundType"] , materiaObject.GetType().GetProperty("Name").GetValue(materiaObject));
//};
///repositoryItem.EditValueChanged += repositoryItem_EditValueChanged;

//repositoryItem.ParseEditValue += repositoryItem_ParseEditValue;

}

 

private void PrePayUseGrid_CellValueChangedEvent(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
{
if (e.Column == PrePayUseGrid.gridView.Columns["RefundType"])//退费方式
{
List<PatUsablePrepayView> viewList = PrePayUseGrid.GetAllBindingInfoList<PatUsablePrepayView>();
viewList[e.RowHandle].RefundType = e.Value.ToString();
List<PatUsablePrepay> preList = UIManager.ViewInfoModelToBaseInfoModel<PatUsablePrepay, PatUsablePrepayView>(viewList);
billInfo.NewPrepayList = preList;
LoadData(billInfo);
}
}

 

二、测试通过但是不灵敏

void repositoryItem_EditValueChanged(object sender, EventArgs e)
{
DevExpress.XtraEditors.BaseEdit edit = PrePayUseGrid.gridView.ActiveEditor;
var materiaObject = (sender as DevExpress.XtraEditors.LookUpEdit).GetSelectedDataRow();
if (PrePayUseGrid.gridView.FocusedColumn.FieldName == "RefundType")
{
PrePayUseGrid.gridView.SetFocusedRowCellValue(PrePayUseGrid.gridView.Columns["RefundType"], edit.Text);
}
}
void repositoryItem_ParseEditValue(object sender, DevExpress.XtraEditors.Controls.ConvertEditValueEventArgs e)
{
if (e.Value != null)
{
e.Value = e.Value.ToString();
}
else
{
e.Value = "";
}
e.Handled = true;
}

posted on 2021-03-18 09:59  Smile向前  阅读(804)  评论(0编辑  收藏  举报