XtraGrid通過後臺代碼定義Column列,以及改變行與列的字體、顏色、背景等

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章