XtraGrid通过后台代码定义Column列,以及改变行与列的字体、颜色、背景等

18 篇文章 1 订阅
3 篇文章 0 订阅

在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;
                }
            }
        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值