在项目中开发中遇到一个需求, 需要把指定的数据表格列 转化为按钮列~ 用的控件是Dev gridcontrol
刚开始 在项目通过直接设置属性, 把指定列设计成了按钮列. :
把列的ColumnEdit属性设置为RepositoryItemButtonEdit
把TextEditStyle属性设置为HideTextEditor;
把Buttons的Kind属性设置为Glyph;把Button的Caption用于设置文字
实现了效果如图所示.
但是如果要实现,根据需要 某些行 显示按钮,某些行不显示按钮, 用这种方法,就不行.
这个时候就需要在 gridView_CustomRowCellEdit (object sender, CustomRowCellEditEventArgs e)
这个事件里面 对具体的某个单元格 ,进行设置了.
可以通过对 e.RepositoryItem 来赋值 来设置它的控件.
见如下代码:
if (e.Column.FieldName != "From") return;
DevExpress.XtraGrid.Views.Grid.GridView gv = sender as DevExpress.XtraGrid.Views.Grid.GridView;
string fieldName = gv.GetRowCellValue(e.RowHandle, "From").ToString();
switch (fieldName)
{
case "Richard Fisher":
e.RepositoryItem = repositoryItemButtonEdit1;
break;
case "Brad Barnes":
e.RepositoryItem = repositoryItemButtonEdit1;
break;
case "Dave Murrel":
e.RepositoryItem = repositoryItemButtonEdit1;
break;
}
按钮需要响应点击的话还需要设置
在GridControl的设计器中Repository页中的In-place Editor Repository项中
在右边的Repository栏中找到你的ButtonEdit,选它的事件属性页,ButtonClick
同时需要注意的是: 有时注册了单击事件 但是没反应,是因为设置了Editable 的属性为False!