在Winform中,Form_Load中:
private void Form1_Load(object sender, EventArgs e)
{
gridControl1.DataSource = GetData(100000);//20
gridControl1.ForceInitialize();
foreach (GridColumn column in gridView1.Columns)
{
column.OptionsColumn.AllowEdit = false;
if (column.FieldName == "ID")
{
column.Caption = "Direction";
column.DisplayFormat.FormatType = FormatType.Custom;
column.DisplayFormat.Format = new PositionDirectionFormatter();
column.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near;
RepositoryItem ri = new RepositoryItemTextEdit();
gridControl1.RepositoryItems.Add(ri);
column.ColumnEdit = ri;
column.ColumnEdit.ExportMode = ExportMode.DisplayText;
}
}
gridColumn2.FieldName = "Text";
gridColumn3.FieldName = "Info";
GridColumn gridColumn4 = new DevExpress.XtraGrid.Columns.GridColumn()
{
FieldName = "Info",
Caption = "搞飞机",
Visible = true
};
gridColumn4.OptionsColumn.ReadOnly = true;
//需要注意ReadOnly(=true可以选择复制但不可改) 和AllowEdit( = false不可选择不可改)的区别 by Happymagic 2020.4.2
gridColumn2.AppearanceCell.Font = new Font("楷体", 10, FontStyle.Bold);
gridColumn3.AppearanceCell.Font = new Font("Courier New", 12, FontStyle.Italic);
//gridColumn4.AppearanceCell.Font = new Font("Courier New", 12, FontStyle.Italic);
//当定义的gridColumn4未加载进gridView1里的时候,定义ApperanceCell的font是无效的。
gridView1.Columns.Add(gridColumn4);
gridView1.RefreshData();
gridView1.Columns["Info"].AppearanceCell.Font = new Font("Courier New", 12, FontStyle.Italic);
//通过代码定义的gridColumn4在Add进gridView1后,可以自定义显示字体、大小、粗细等。
//gridView1.AddUnboundColumn("Info","搞什么飞机");
//gridView1.RefreshData();
//gridControl1.Refresh();
BindCustomDrawRowIndicator(gridView1);
//GridColumn colName = gridView1.Columns["ID"];
//colName.AppearanceCell.BackColor = Color.Salmon;
//colName.AppearanceCell.Options.UseBackColor = true;
//gridView1.OptionsBehavior.Editable = false;
//Color foreColor = Color.LightCoral;
//Color backColor = Color.LightSkyBlue;
Changing the appearance settings of row cells dynamically
gridView1.RowStyle += (sender, e) => {
// GridView view = sender as GridView;
// //Change selected rows' fore and back colors
// if (view.IsRowSelected(e.RowHandle))
// {
// e.Appearance.ForeColor = foreColor;
// e.Appearance.BackColor = backColor;
// // This property controls whether settings provided by the RowStyle event have a higher priority
// e.HighPriority = true;
// }
};
}
行与列的背景色调整控制可以在gridView1_RowStyle事件中:
private void gridView1_RowStyle(object sender, RowStyleEventArgs e)
{
GridColumn colName = gridView1.Columns["ID"];
colName.AppearanceCell.BackColor = Color.Salmon;
colName.AppearanceCell.Options.UseBackColor = true;
gridView1.OptionsBehavior.Editable = false;
Color foreColor = Color.LightCoral;
Color backColor = Color.LightSkyBlue;
//Changing the appearance settings of row cells dynamically
//gridView1.RowStyle += (sender, e) =>
{
GridView view = sender as GridView;
//Change selected rows' fore and back colors
if (view.IsRowSelected(e.RowHandle))
{
e.Appearance.ForeColor = foreColor;
e.Appearance.BackColor = backColor;
// This property controls whether settings provided by the RowStyle event have a higher priority
e.HighPriority = true;
}
}
}