Dev中常用到的一些基础事件!
============================================================
SetControlReadOnly(xtraTabControl1);
//将xtraTabControl1中的控件进行循环遍历,并且控件内容只读
private void SetControlReadOnly(Control control)
{
var pro = control.GetType().GetProperty("ReadOnly");
if(pro!=null)
{
pro.SetValue(control, true, null);
}
foreach(Control c in control.Controls)
{
OperateControls(c);
}
}
============================================================
/// <summary>
/// 自定义显示文本
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
{
if(e.Column.FieldName== "State")
{
switch (e.Value.ToString().Trim())
{
case "0":
e.DisplayText = "状态一";
break;
case "1":
e.DisplayText = "状态二";
break;
case "2":
e.DisplayText = "状态三";
break;
default:
e.DisplayText = "未知";
break;
}
}
}
============================================================
/// <summary>
/// 对某个字段(ValveState)进行不同颜色提示
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
{
for(int i=0;i<gridView1.RowCount;i++)
{
if(e.RowHandle==i && e.Column.FieldName== "ValveState")
{
string state = gridView1.GetRowCellValue(i, "ValveState").ToString();
if(state=="1")
{
e.Appearance.ForeColor = Color.Red;
}
}
}
}
============================================================
/// <summary>
/// GridView没有查询到数据时显示
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void gridView1_CustomDrawEmptyForeground(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e)
{
if (this.gridView1.RowCount != 0) return;
GirdControlDrawEmptyForeground.DrawEmpty(sender, e);
}
/// <summary>
/// 列表显示为空提示
/// </summary>
public static class GirdControlDrawEmptyForeground
{
private const string MESSAGE_INFO = "暂无数据.";
public static void DrawEmpty(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e)
{
Font font = new Font("宋体", 10, FontStyle.Bold);
SolidBrush solid = new SolidBrush(Color.Black);
Rectangle rectangle = new Rectangle(e.Bounds.Left, e.Bounds.Top + 2, e.Bounds.Width, e.Bounds.Height);
e.Graphics.DrawString(MESSAGE_INFO, font, solid, rectangle);
}
}
============================================================
注意:
1..将该列的UnboundType属性设置为bound(默认值)以外的数据类型
2.为该列设置一个窗体内全局唯一的FieldName,注意这个FieldName甚至不能出现在窗体上其它XtraGrid中。
/// <summary>
/// 非数据绑定列的触发事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
{
if (e.IsGetData) //绑定行指示列数据显示文本处理
{
if (e.Column.FieldName == "Cancel") //该显示行
{
var row = e.Row as View_User; //行数据源
if (row != null && row.State == '2') //指定列
{
e.Value = "撤销";
}
else
{
e.Value = "";
}
}
}
}
============================================================
/// <summary>
/// 点击选择选中行的某列数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btm_ok_Click(object sender, EventArgs e)
{
StringBuilder strTemp = new StringBuilder();
strTemp.Append("'");
int[] rows = gridView1.GetSelectedRows(); //获取选中数据行信息
for(int y=0;y<gridView1.RowCount;y++)
{
string ap = gridView1.GetRowCellValue(y, "Name").ToString();
for (int i = 0; i < rows.Count(); i++)
{
if (gridView1.GetSelectedRows()[i] == y)
{
strTemp.Append(ap);
strTemp.Append("','");
}
}
}
MessageBox.Show("选中用户:" + strTemp);
}